Results 1 to 5 of 5
Thread: Help reading file
- 09-16-2008, 08:29 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
Help reading file
I need to read an input file and print the first and last record and then run calculations on the last record. I think I know how to read the file (about 2000 records) and count the records, but how do you speficy the first or last record? I am new to this so any help is appreciated.
- 09-16-2008, 10:27 PM #2
Append all records into the array,if you know the size of the records amount,or if you don't know,with the List,so then you will know the last and the first record
- 09-16-2008, 11:24 PM #3
firstAndLast.txtJava Code:import java.io.*; public class FirstAndLast { public static void main(String[] args) throws IOException { File file = new File("firstAndLast.txt"); BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(file))); int lineCount = 0; String lastLine = null; String line; while((line = br.readLine()) != null) { if(lineCount == 0) { System.out.printf("count = %d first line = %s%n", lineCount, line); } lastLine = line; lineCount++; } br.close(); // Finished reading file, "lastLine" has the last line. System.out.printf("count = %d lastLine = %s%n", lineCount, lastLine); } }
Java Code:one two three four five six seven
- 09-17-2008, 12:11 AM #4
The first record should be the easy one to save.
Then in a loop read the rest of the file and save the last record read before reading the next one. When you get the EOF indicator, the last record saved will be the last record in the file.
That's the description for the code that hardwired gave you.
- 09-17-2008, 03:28 AM #5
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
reading csv file help
By fritz1474 in forum New To JavaReplies: 5Last Post: 09-04-2008, 08:41 PM -
Right use of file reading ?
By jurka in forum New To JavaReplies: 3Last Post: 08-27-2008, 08:16 PM -
Reading a file
By mew in forum New To JavaReplies: 2Last Post: 12-30-2007, 12:23 PM -
Reading a file for use
By peachyco in forum New To JavaReplies: 2Last Post: 11-27-2007, 03:49 AM -
Reading from a file
By leebee in forum New To JavaReplies: 1Last Post: 07-23-2007, 12:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks