Results 1 to 6 of 6
Thread: text file into a string?
- 09-12-2010, 06:26 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
text file into a string?
Hi,
I need to turn a .txt file into a string? so I can then loop through and get the names out and their corresponding test scores. This is what i currently have, I need it to be in a string instead of printing out, once I got the string I'll work on writing the loops. Thanks.
Java Code:import java.io.*; import java.util.*; public class ReadTextFileExample { public static void main(String[] args) { //Prompt user for filename System.out.println("Please type file name and press Enter: "); BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in)); String filename = null; try { filename = kbd.readLine(); } catch (IOException e) { System.out.println("Filename was not correct."); System.exit(0); } File file = new File("filename"); StringBuffer contents = new StringBuffer(); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(filename)); String text = null; // repeat until all lines are read while ((text = reader.readLine()) != null) { contents.append(text).append(System.getProperty("line.separator")); } } catch (FileNotFoundException e) { System.out.println("The filename entered was not found, program will now terminate."); } catch (IOException e) { System.out.println("The filename entered was not found, program will now terminate."); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { System.out.println("The filename entered was not found, program will now terminate."); } } System.out.println(contents.toString()); } }
- 09-12-2010, 06:29 AM #2
You actually already have the contents as a string.
Notice this line:
Java Code:System.out.println(contents.toString());
That's what prints out the contents. Here, you can do anything with contents (which is a StringBuffer variable--read here for the API).
Did you get this code from elsewhere? If so you should make sure you understand the purpose of each variable and block once using it.
- 09-12-2010, 07:16 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
Thanks for the reading material, if I were to write
would newStr then be a string with the contents of the .txt file that I could work with from there?Java Code:String newStr = contents.toString();
- 09-12-2010, 07:53 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 09-12-2010, 07:54 AM #5
- 09-12-2010, 07:57 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
get string from a text file
By Kruptein in forum New To JavaReplies: 11Last Post: 08-14-2009, 04:45 PM -
how to store string in text file
By santhosh_el in forum AWT / SwingReplies: 2Last Post: 04-03-2009, 06:21 AM -
How to write a string middle of a text file?
By loggen in forum New To JavaReplies: 5Last Post: 12-19-2008, 08:48 AM -
Searching a string from a text file using Swing Buttons
By pradeep1_mca@yahoo.com in forum AWT / SwingReplies: 2Last Post: 09-15-2008, 09:50 AM -
Searching a String from Text file using Swings .
By pradeep1_mca@yahoo.com in forum AWT / SwingReplies: 4Last Post: 09-09-2008, 05:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks