Results 1 to 7 of 7
Thread: Saving txt document question
- 12-28-2011, 03:41 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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:
It saves like this:Java Code:Line 1 Line 2 Line 3
The code is as follows, if anyone can help it would be much appreciated:Java Code:Line 1Line 2Line 3
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(); }
-
Re: Saving txt document question
Add a "\n" at the end of each line, that should make a new-line.
- 12-28-2011, 04:01 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
Re: Saving txt document question
Well I did that for the open file and changed it to this:
I am not sure where to putJava 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; }in the save method...Java Code:+ "\r\n"
-
Re: Saving txt document question
Your txt.getText() method is the key here. Where is this method?
- 12-28-2011, 04:16 AM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 12-28-2011, 04:29 AM #6
Member
- Join Date
- Dec 2011
- Posts
- 3
- Rep Power
- 0
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?
- 12-28-2011, 04:39 AM #7
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:
Then, when you write outString the way you did in your original post, it should contain the breaks.Java Code:String outString = ""; for(String s : list){ outString += s + "\r\n"; }
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
-
How to Convert Excel document to word document?
By sudheer.v47 in forum Advanced JavaReplies: 1Last Post: 10-07-2011, 12:32 PM -
Saving word document from iframe
By anil@netedgecomputing.com in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 05-15-2008, 11:49 AM -
Need help with Document interface
By cbalu in forum AWT / SwingReplies: 1Last Post: 11-30-2007, 11:03 PM -
Parsed Document
By nick211001 in forum New To JavaReplies: 1Last Post: 07-29-2007, 01:53 AM -
add a xml document
By Jack in forum XMLReplies: 2Last Post: 07-04-2007, 09:21 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks