Results 1 to 7 of 7
  1. #1
    zephluck is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Talking Saving txt document question

    Hi guys,

    I have a small problem. The problem is that my code (below) is not saving txt files correctly. When it should save in the following format:

    Java Code:
    Line 1
    Line 2
    Line 3
    It saves like this:

    Java Code:
    Line 1Line 2Line 3
    The code is as follows, if anyone can help it would be much appreciated:

    Java Code:
    //Save File Method
        private void saveFile() {
            if (filepath == null) { saveFileAs(); return; }
            try {
                BufferedWriter out = new BufferedWriter(
                                     new FileWriter(filepath));
               out.write(txt.getText());
               out.close();
            }
            
            catch (Exception e) {
                JOptionPane.showMessageDialog(this, e.getMessage());
            }
        }
        
        //Save File As Method
        private void saveFileAs() {
            if (dFile.showSaveDialog(this) !=
                JFileChooser.APPROVE_OPTION) return;
            filepath = dFile.getSelectedFile().getPath();
            saveFile();
        }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Saving txt document question

    Add a "\n" at the end of each line, that should make a new-line.

  3. #3
    zephluck is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: Saving txt document question

    Well I did that for the open file and changed it to this:

    Java Code:
        private void open(String file) throws Exception {
            BufferedReader in = new BufferedReader(
                                    new FileReader(file));
            while (in.ready())
             txt.append(in.readLine() + "\r\n");
            in.close();
            filepath = file;
        }
    I am not sure where to put
    Java Code:
    + "\r\n"
    in the save method...

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Saving txt document question

    Your txt.getText() method is the key here. Where is this method?

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: Saving txt document question

    Don't reinvent the wheel!

    All text components implement a write(...) method. Use that method instead of trying to create your own.

    And the suggestion to add "\n" at the end of every line is not appropriate for all platforms.

  6. #6
    zephluck is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: Saving txt document question

    Could anyone possibly post up an example of how to save it another way, possibly by line instead of all at once?

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Saving txt document question

    It sounds like your txt.getText() method returns the lines without the line breaks. No matter how you write the file, it will all be on one line if getText() returns all the text as a blob.

    For example, if you were looping through a list and wanted to collect the values, you could do something like:

    Java Code:
    String outString = "";
    for(String s : list){
        outString += s + "\r\n";
    }
    Then, when you write outString the way you did in your original post, it should contain the breaks.

    You could write the output one line at a time too, with a PrintWriter and println(), but then your code would be less modular (since your file writing and text-getting would be combined).

    Writing all at once is fine if the data you're writing is properly formatted to begin with! Good luck :D

Similar Threads

  1. How to Convert Excel document to word document?
    By sudheer.v47 in forum Advanced Java
    Replies: 3
    Last Post: 05-09-2014, 09:26 AM
  2. Saving word document from iframe
    By anil@netedgecomputing.com in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-15-2008, 11:49 AM
  3. Need help with Document interface
    By cbalu in forum AWT / Swing
    Replies: 1
    Last Post: 11-30-2007, 11:03 PM
  4. Parsed Document
    By nick211001 in forum New To Java
    Replies: 1
    Last Post: 07-29-2007, 01:53 AM
  5. add a xml document
    By Jack in forum XML
    Replies: 2
    Last Post: 07-04-2007, 09:21 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
  •