Results 1 to 10 of 10
  1. #1
    Pinky4Free is offline Member
    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    Default How do i read multiple lines from a file into a string?

    ive tried many different things and this is the only thing that has worked with reading one line from a file so far...

    Java Code:
    try{
        // Open the file that is the first 
        // command line parameter
        FileInputStream fstream = new FileInputStream("./Saves/Body.sav");
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   {
        // Print the content on the console
        System.out.println(strLine);
        w1.Body = strLine;
        }
        //Close the input stream
        in.close();
          }catch (Exception e){//Catch exception if any
        System.err.println("Error: " + e.getMessage());
        }
    I am trying to create a load function so i can load text from a file onto a string onto a jTextArea... Without any sort of openfiledialog

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How do i read multiple lines from a file into a string?

    In general you read multiple lines and concatenate them.

    Java Code:
    String str = "";
    while line = ...
        str += line;
    System.out.println(str);
    The StringBuilder and StringBuffer classes are made for this:

    Java Code:
    StringBuilder buf = new StringBuilder();
    while line = ...
        buf.append(line);
    System.out.println(buf.toString());
    // or do anything else you like with the string buf.toString()
    The use of StringBuilder might help avoid the overhead associated with string concatenation if it happens lots of times.

    I am trying to create a load function so i can load text from a file onto a string onto a jTextArea
    This is more specific.

    You could try something like the second method above, since JTextArea also has an append() method. But if the problem is exactly as you state it there is a far better way (which again avoids the time/memory overheads associated with lots of appending): use the read() method that JTextArea shares with other text components.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: How do i read multiple lines from a file into a string?

    A JTextArea can read its new content from any Reader object ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Pinky4Free is offline Member
    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    Default Re: How do i read multiple lines from a file into a string?

    Quote Originally Posted by pbrockway2 View Post
    In general you read multiple lines and concatenate them.

    Java Code:
    String str = "";
    while line = ...
        str += line;
    System.out.println(str);
    The StringBuilder and StringBuffer classes are made for this:

    Java Code:
    StringBuilder buf = new StringBuilder();
    while line = ...
        buf.append(line);
    System.out.println(buf.toString());
    // or do anything else you like with the string buf.toString()
    The use of StringBuilder might help avoid the overhead associated with string concatenation if it happens lots of times.



    This is more specific.

    You could try something like the second method above, since JTextArea also has an append() method. But if the problem is exactly as you state it there is a far better way (which again avoids the time/memory overheads associated with lots of appending): use the read() method that JTextArea shares with other text components.
    I dont know how to use a StringBuilder or anything... can you maybe post some tested and working code for what i am trying to do?
    I created a save function to save what's in the jTextArea and in the Body.sav file everyline saved correctly, i dont understand why it is such a pain to read it onto the jTextArea and the String...

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: How do i read multiple lines from a file into a string?

    I bet you didn't read the API documentation for the JTextArea class: it can read its content from any Reader you supply; I'm repeating myself because I wrote this already in reply #3.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Pinky4Free is offline Member
    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    Default Re: How do i read multiple lines from a file into a string?

    I'm simply asking for some sample code...

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: How do i read multiple lines from a file into a string?

    Quote Originally Posted by Pinky4Free View Post
    I'm simply asking for some sample code...
    Java Code:
    Reader r= ...
    yourArea.read(r, "some description");
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Pinky4Free is offline Member
    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    Default Re: How do i read multiple lines from a file into a string?

    Quote Originally Posted by JosAH View Post
    Java Code:
    Reader r= ...
    yourArea.read(r, "some description");
    kind regards,

    Jos
    Thank you for replying..
    i tried
    Java Code:
    BufferedReader reader = new BufferedReader(new StringReader(w1.Body));
        	try {
    			g.jTextArea1.read(reader, w1.Body);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    But it gives me this long line of red text in the console... leading to this "g.jTextArea1.read(reader, w1.Body);"

  9. #9
    Pinky4Free is offline Member
    Join Date
    Nov 2012
    Posts
    15
    Rep Power
    0

    Default Re: How do i read multiple lines from a file into a string?

    I figured it out finally, if anyone else has this problem check out this forums.. I can display a .txt into my JTextArea but it shows 1 appended line | DaniWeb

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How do i read multiple lines from a file into a string?

    That link illustrates using string concatenation to build up the component's text content. Ie, the first method I mentioned in reply #2 (And one answer to the question as you posed it in the thread's title.)

    Using a StringBuffer or StringBuilder will avoid the overhead involved in string concatenation. If The API link I gave wasn't sufficient, Oracle's Tutorial has a page on The StringBuilder Class.

    However, as mentioned in #2, and repeated a couple of times by Jos, JTextArea has a read() method. And it should be used.

    ---

    For some reason Oracle's Tutorial doesn't seem to mention this aspect of text components. (that I can see). But examples showing the straightforward reading of text into a text component abound on the internet: for example at stackoverflow.

    ---

    If you decide on the better implementation, but have trouble with the code, post what you're trying. (And, as an aside, you mention in #4 that you wrote a method to save the text content: JTextArea ha a symmetrical write() method which also makes this a one liner.)
    Last edited by pbrockway2; 11-04-2012 at 09:12 PM.

Similar Threads

  1. Replies: 6
    Last Post: 09-28-2012, 01:21 PM
  2. Splitting an string of number into multiple lines
    By Juukamen in forum New To Java
    Replies: 5
    Last Post: 04-09-2012, 10:30 AM
  3. Writing multiple lines to text file at once
    By Plex in forum New To Java
    Replies: 14
    Last Post: 11-08-2010, 09:17 PM
  4. Deleting multiple lines of text from a file
    By barman in forum New To Java
    Replies: 4
    Last Post: 06-29-2010, 09:12 AM
  5. Replies: 3
    Last Post: 12-17-2007, 02:35 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
  •