Results 1 to 7 of 7
Thread: printing array
- 02-11-2009, 05:10 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 39
- Rep Power
- 0
-
array from a file? Do you mean that you read the file into an array (or possibly better, a collection) of String?
If so, wouldn't a for-loop seem like it would work well here?
- 02-11-2009, 05:24 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 39
- Rep Power
- 0
Would you have an example of that?
- 02-11-2009, 05:35 PM #4
A lot of questions...
Which of the following:
- Print contents from a File?
- Print contents from an array?
- Pass contents from a file to an array and then print it?
- Pass contents form an array to a file and then print it?
- None of the above
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-11-2009, 05:37 PM #5
Member
- Join Date
- Dec 2008
- Posts
- 39
- Rep Power
- 0
i have a file("employee.txt") that has a list of employees and all i want to is print the list of employees and im not sure how to go about doing that.
- 02-11-2009, 06:10 PM #6
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
google search "java read from file" + i'm feeling lucky took me here:
Java Practices -> Reading and writing text files
- 02-12-2009, 02:01 PM #7
Here's something I found on how to read a file as a String. Works great for me:
That reads the entire file as a String.. Not each word as a String in an Array..Java Code:private String readFileAsString(String filePath) throws IOException { StringBuilder fileData = new StringBuilder(1000); BufferedReader reader = new BufferedReader( new FileReader(filePath)); char[] buf = new char[1024]; int numRead = 0; while((numRead=reader.read(buf)) != -1){ fileData.append(buf, 0, numRead); } fileData.trimToSize(); reader.close(); return fileData.toString(); }
to get each word in array, you could do this:
I think that would work. I'm not the greatest at regular expressions, someone correct my split() if I'm wrong. Then use the following syntax to print the array:Java Code:ClassName cs = new ClassName(); String sample = cs.readFileAsString(sample.txt); String[] sampleArray = sample.split(" ");
Hope that helped.Java Code:for(int i = 0; i < sampleArray's length; i increments) { print out sampleArray[i]; }Tell me if you want a cool Java logo avatar like mine and I'll make you one.
Similar Threads
-
Help in Printing
By kirly in forum Advanced JavaReplies: 3Last Post: 10-03-2011, 03:40 PM -
Printing Byte Array
By suchismitasuchi in forum New To JavaReplies: 3Last Post: 01-19-2009, 10:58 AM -
Printing Out Every Other Letter
By Agent in forum New To JavaReplies: 4Last Post: 11-20-2008, 12:43 AM -
[SOLVED] Help with arrays, printing highest and lowest value of the array.
By Sophiie in forum New To JavaReplies: 21Last Post: 11-05-2008, 02:31 PM -
Printing Example
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks