Results 1 to 8 of 8
  1. #1
    kostinio is offline Member
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default Read a file and converting this file into a string

    Hello all,

    In my code i created a text file which has 3 lines.. i want to read this file and to store these 3 lines in one string.. Does anyone have any idea how to do it?

    Thanks in advance :)

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

    Default

    Use a StringBuilder and convert it to a String at the end when you have read your entire file; it's cheaper than just appending Strings to Strings. If you are reading your file line by line don't forget to append \n characters to your StringBuilder each time you have read a line.

    kind regards,

    Jos

  3. #3
    kostinio is offline Member
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default

    Can you give me an example?

  4. #4
    kostinio is offline Member
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default

    I tried to use this code but it didn't work :S

    BufferedReader inputFile = new BufferedReader(new FileReader(myFilename));
    String value = inputFile.readLine();

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

    Default

    Quote Originally Posted by kostinio View Post
    I tried to use this code but it didn't work :S

    BufferedReader inputFile = new BufferedReader(new FileReader(myFilename));
    String value = inputFile.readLine();
    That snippet of code just reads one line; read the API documentation for that BufferedReader class and see what happens if no more lines can be read from the file (that's the end-of-file condition). You'll need a loop as long as you can read lines. Append those lines (a String) to the StringBuffer and at the end convert that buffer to a String. Oh, and don't forget to close that file.

    kind regards,

    Jos

  6. #6
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    Hm,

    how about you open a FileInputStream, create a buffer of the size of the file and read the whole document in to that buffer. Afterwards create a string from that buffer.

    E.g.
    Java Code:
    File file = new File("myFile");
    FileInputStream fis = new FileInputStream(file);
    char[] cbuff = new char[(int) file.length()];
    fis.read(cbuff);
    fis.close();
    String mySuperString = new String(cbuff);

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

    Default

    Quote Originally Posted by AndreB View Post
    Hm,

    how about you open a FileInputStream, create a buffer of the size of the file and read the whole document in to that buffer. Afterwards create a string from that buffer.

    E.g.
    Java Code:
    File file = new File("myFile");
    FileInputStream fis = new FileInputStream(file);
    char[] cbuff = new char[(int) file.length()];
    fis.read(cbuff);
    fis.close();
    String mySuperString = new String(cbuff);
    Better make that a byte[] instead of a char[] (InputStreams read bytes, not chars).

    kind regards,

    Jos

  8. #8
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

Similar Threads

  1. Converting excel file to text file
    By saranya_v11 in forum New To Java
    Replies: 3
    Last Post: 07-05-2013, 10:22 AM
  2. converting java class file to exe file
    By satheeshtech in forum Advanced Java
    Replies: 5
    Last Post: 07-18-2009, 11:47 PM
  3. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  4. Converting text file(.txt) to JPG file(.jpg) in java
    By javadeveloper in forum Advanced Java
    Replies: 0
    Last Post: 11-09-2007, 04:22 PM
  5. Read a string from a txt file
    By cachi in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 07:50 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
  •