Results 1 to 5 of 5
- 10-10-2010, 07:31 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 32
- Rep Power
- 0
Writing text to a file has no effect? (blank .txt)
I want to be able to name my file whatever I want and save it whereever, so I use this:
Java Code:JFileChooser fc = new JFileChooser(); fc.showSaveDialog(fc); File file = fc.getSelectedFile();
Java Code:FileOutputStream fos; DataOutputStream dos; JFileChooser fc = new JFileChooser(); fc.showSaveDialog(fc); File file = fc.getSelectedFile(); try { for(int i=0; i<MAX_ROWS;i++){ for (int j=0; j<MAX_COLUMNS; j++){ out.write(i + " " + j); if(j=MAX_COLUMNS-1) //want this to be a space but I can't check if it's right or not out.write("\n"); } } } catch (IOException e) { e.printStackTrace(); }
The thing I have now creates a file, but it's blank.
- 10-10-2010, 08:18 AM #2
Go through those tutorials some more, you seem to be unaware of the very fundamentals of Java (or any object oriented language).
Declaring variables of type FileOutputStream and DataOutputStream and then writing to out (whatever that may be) isn't going to automagically read your mind and reproduce the output in a text file.
Your nested for loops are also badly formed: if you want to take an action when the inner loop reaches its sentinel value, that can be outside the inner loop but inside the outer loop.Java Code:for(int i = 0; i < MAX_ROWS; i++){ for (int j = 0; j < MAX_COLUMNS; j++){ out.write(i + " " + j); } out.write("\n"); }
Code Conventions for the Java(TM) Programming Language: Contents
dbLast edited by DarrylBurke; 10-10-2010 at 08:20 AM.
- 10-10-2010, 10:04 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 32
- Rep Power
- 0
The HTML [code] tags don't have any formatting, and copying and pasting does not keep the original format...
I'll have to keep that in mind next time as it obviously ruins the presentation of the problem and invites disdain.
Of course I did make the idiotic mistake of leaving out some crucial details. I had tried a lot of implementation and marked a lot of things out with "//". I wanted to keep out the distractions. The (un)initialization of "out" for example would have been marked red and have been an obvious place of error. I did not expect the program to "read my mind".
The fact that the loss of formatting required backspacing and spacing one space at a time within html made me impatient.
BTW, this is within a actionListener for a GUI.
Java Code:JFileChooser fc = new JFileChooser(); fc.showSaveDialog(fc); File file = fc.getSelectedFile(); try { FileWriter fstream = new FileWriter(file); BufferedWriter out = new BufferedWriter(fstream); for(int i=0; i<MAX_ROWS;i++){ for (int j=0; j<MAX_COLUMNS; j++){ out.write( i ); /*this is not what I really want to write but the .txt file should show 00...00 11...11 22...22 if it succeeds */ System.out.println( "(" + i + "," + "j" + ")" ); } out.write("\n"); } out.close(); } catch (IOException e) { e.printStackTrace(); } }
-Is my use of JFileChoose correct? When I showDialog() and getSelectedFile() does file become the file which I have created? (Does the file extension matter? I've tried .txt and just a name) If it is not correct, what would be the proper method?
-A buffered writer writes characters exactly yes? It does not need a byte conversion? Is my implementation correct?
-What could be the problem with saving blank .txt files? I inserted a System.out.println within the inner loop and it's prints out successfully, which means either out.write() is not working, or it's writing to nothing.(a la the first question) I do not think the problem could come from a source outside the given section.
Thanks for the tip about the loop, I would've never though of it.Last edited by ryuzog; 10-10-2010 at 10:11 AM.
- 10-10-2010, 10:26 AM #4
Read the API for BufferedWriter more carefully. The write method you're invoking is
Java Code:write(int c)
Depending on the value of MAX_ROWS, you may be writing only non-printing characters. And depending on the editor, it may not read past the zero character at the start of the file.
The int value 0 does not correspond to the char '0'. Try changing your write line toJava Code:out.write(i + '0');
SSCCE : Java Glossary
db
- 10-10-2010, 10:57 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 32
- Rep Power
- 0
As a beginner I find the API intimidating =(
I have figured out the problem though. I mainly read the description given by my IDE when I write out. and after the "." a list of stuff comes up.
The description for out.write however is overloaded with int, char, char[], string.. etc.. it says "writes * to file".
Only String is written directly though and everything else seems to be in some other format, I'm guessing bytes.
I added a Integer.parse *** and it's working.
**As you said, it's writing out a character ~ _ ~ The description "writes int to file" is somewhat misleading. I'll try to make a sscce next time, but in this case there was a whole gui and stuff.Last edited by ryuzog; 10-10-2010 at 11:00 AM.
Similar Threads
-
Remove a Blank Line from Text file
By nitinverma in forum CLDC and MIDPReplies: 8Last Post: 06-15-2010, 06:12 AM -
Im writing to a file and i want to skip lines while writing to a text file.
By Broden_McDonald in forum New To JavaReplies: 1Last Post: 02-27-2010, 01:29 AM -
writing to text file problem
By blumdiggity in forum NetworkingReplies: 1Last Post: 02-26-2010, 02:43 PM -
Text effect: rotation and transparent
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:49 PM -
writing text to file
By notwist in forum New To JavaReplies: 3Last Post: 04-25-2008, 04:20 AM
Bookmarks