Results 1 to 5 of 5
  1. #1
    ryuzog is offline Member
    Join Date
    Jan 2010
    Posts
    32
    Rep Power
    0

    Default 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();
    I think that this creates a file and getSelectedFile then chooses it, so now I just ned to write stuff to "file".

    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();
                                 }
    Almost every tutorial I see seems to present a new method of file writing...some with output streams, some with buffered streams, buffered writers etc...It's confusing...

    The thing I have now creates a file, but it's blank.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    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");
    }
    Last but by no means least, your code formatting is atrocious. Please go through and apply the coding conventions.
    Code Conventions for the Java(TM) Programming Language: Contents

    db
    Last edited by DarrylBurke; 10-10-2010 at 09:20 AM.

  3. #3
    ryuzog is offline Member
    Join Date
    Jan 2010
    Posts
    32
    Rep Power
    0

    Default

    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();
                                 }
                                  
                        }
    I think the main problem is the combining of choosing and creating a local file and writing to it. Most tutorials deal with "save" within the context of GUI's, while I/O have huge chapters dedicated to them. Here are some specific questions.

    -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 11:11 AM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    Read the API for BufferedWriter more carefully. The write method you're invoking is
    Java Code:
    write(int c)
    where c is the int specifying a character to be written.

    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 to
    Java Code:
    out.write(i + '0');
    If that doesn't solve your problem, consider posting a SSCCE to get better help sooner.
    SSCCE : Java Glossary

    db

  5. #5
    ryuzog is offline Member
    Join Date
    Jan 2010
    Posts
    32
    Rep Power
    0

    Default

    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 12:00 PM.

Similar Threads

  1. Remove a Blank Line from Text file
    By nitinverma in forum CLDC and MIDP
    Replies: 8
    Last Post: 06-15-2010, 07:12 AM
  2. Replies: 1
    Last Post: 02-27-2010, 02:29 AM
  3. writing to text file problem
    By blumdiggity in forum Networking
    Replies: 1
    Last Post: 02-26-2010, 03:43 PM
  4. Text effect: rotation and transparent
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 09:49 PM
  5. writing text to file
    By notwist in forum New To Java
    Replies: 3
    Last Post: 04-25-2008, 05:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •