Results 1 to 8 of 8
- 12-26-2009, 12:12 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
- 12-26-2009, 12:19 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
- 12-26-2009, 12:45 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
Can you give me an example?
- 12-26-2009, 12:56 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
I tried to use this code but it didn't work :S
BufferedReader inputFile = new BufferedReader(new FileReader(myFilename));
String value = inputFile.readLine();
- 12-26-2009, 12:59 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
- 12-26-2009, 03:08 PM #6
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);
- 12-26-2009, 03:18 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 12-26-2009, 03:54 PM #8
Similar Threads
-
Converting excel file to text file
By saranya_v11 in forum New To JavaReplies: 2Last Post: 10-14-2009, 04:01 PM -
converting java class file to exe file
By satheeshtech in forum Advanced JavaReplies: 5Last Post: 07-18-2009, 11:47 PM -
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM -
Converting text file(.txt) to JPG file(.jpg) in java
By javadeveloper in forum Advanced JavaReplies: 0Last Post: 11-09-2007, 04:22 PM -
Read a string from a txt file
By cachi in forum Java AppletsReplies: 1Last Post: 08-07-2007, 07:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks