Results 1 to 9 of 9
  1. #1
    AwesomeStorm is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default Saving JTextArea to .txt

    Here's the code I'm using:
    Java Code:
    		FileWriter writer = new FileWriter(file);
    		writer.write(data);
    		writer.flush();
    		writer.close();
    I've done research for a couple hours now and have tried different methods, like PrintWriter, BufferedWriter, etc, but they all end up doing the same thing. The code I've provided above is only useful for causing an IOException, but the other ones I made saved to a file just fine, but everything was on a single line (except that the textarea has multiline text).

    I want to save the contents of my JTextArea to a file, but not all on the same damn line >_>

    Also:
    PrintWriter.println() is irrevelant since I'm not printing a line at a time

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by AwesomeStorm View Post
    Also:
    PrintWriter.println() is irrevelant since I'm not printing a line at a time
    Maybe you should be.

  3. #3
    AwesomeStorm is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    Maybe you should be.
    I tried by getting the number of lines and making a for loop that prints the lines one at a time (given the beginning char and the length of the line) but that didn't work either.

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by AwesomeStorm View Post
    I tried by getting the number of lines and making a for loop that prints the lines one at a time (given the beginning char and the length of the line) but that didn't work either.
    Hm, this works for me, so I'm guessing you're doing it wrong, but without code have no idea why. Why not create a very small program that does nothing but displays text in a JTextArea and then saves it on press of a button, and see what happens. If the program works, then great, scale it up to your application. If not, then post it here and let us play with it and see why it is or isn't working.

  5. #5
    AwesomeStorm is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    Java Code:
    try{
    					
    	File saveScript = saveDialog.getSelectedFile();
    	BufferedWriter buffWriter = new BufferedWriter(new FileWriter(saveScript));
    					
    	int lines = txtScript.getLineCount();
    	for(int i = 0; i <= lines; i++) {
    						
    	int start = txtScript.getLineStartOffset(i);
    	int end = txtScript.getLineEndOffset(i);
    	int length = end - start;
    						
    	buffWriter.write(txtScript.getText(), start, length);
            buffWriter.close();
    						
    }
    					
    					
    } catch (IOException err){
    					
    	consolePrint("IOException while saving file");
    					
    } catch (BadLocationException err2){
    					
    	consolePrint("BadLocationException while saving file");
    					
    }
    consolePrint() is just a method I made to display errors in the frame.

    So with my above code the file gets created fine (with the FileChooser) but there's nothing in it, no text.
    Last edited by AwesomeStorm; 05-29-2010 at 12:17 AM.

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    How about getting the String from the JTextArea, splitting it into an array of separate lines with String.split("\\n"), then using a PrintWriter object and a for loop save each line of the String array into the file by calling println on the PrintWriter passing in each line.

  7. #7
    AwesomeStorm is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    How about getting the String from the JTextArea, splitting it into an array of separate lines with String.split("\\n"), then using a PrintWriter object and a for loop save each line of the String array into the file by calling println on the PrintWriter passing in each line.
    I ended up fixing it, but not in the way you told me lol :P
    It was really just a dumb mistake. Thanks though :)

  8. #8
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    I'm glad you've got this fixed, but in the interests of noobs who run across this thread a year or two from now, can you post your repaired code?

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

    Default

    Just use the write(...) method of JTextArea, then you don't have to worry about writing looping code.

Similar Threads

  1. JTextArea on PopUp -JTextArea isn't editable
    By Richy76 in forum AWT / Swing
    Replies: 3
    Last Post: 02-01-2010, 07:51 PM
  2. Saving the index??
    By vividcooper in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 10:20 PM
  3. Saving JFrame
    By jralexander in forum New To Java
    Replies: 7
    Last Post: 12-03-2008, 09:19 PM
  4. Saving changes done through a program
    By xcallmejudasx in forum New To Java
    Replies: 0
    Last Post: 12-02-2008, 04:53 PM
  5. Saving data...?
    By easyRyder in forum New To Java
    Replies: 8
    Last Post: 07-15-2008, 03:14 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
  •