Results 1 to 12 of 12
Thread: Reading From File
- 07-23-2013, 03:29 PM #1
Reading From File
Java Code:File f = new File("F:/" + date + ".txt"); if (!f.exists()) { System.out.println("file not found !"); System.exit(1); } int lines = 0; @SuppressWarnings("resource") Scanner scan = new Scanner(f); while (scan.hasNext()) { System.out.println(scan.nextLine()); lines++; } System.out.println(lines); scan.reset(); int sum = 0; int arr[] = new int[lines]; for (int i = 0; i < lines; i++) { arr[i] = Integer.valueOf(scan.nextLine()).intValue(); sum += arr[i]; } System.out.println(sum);
Last edited by allaudin; 07-23-2013 at 03:42 PM.
- 07-23-2013, 03:41 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Reading From File
Resetting a scanner does not reset the FileInputStream used (as you could've known by reading the API documentation of the Scanner class). I'd use a List<String> for this purpose though, not a fixed size array ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 07-23-2013, 03:56 PM #3
Re: Reading From File
I was just testing .. ok I will use linke list .. now what should I do to sum those numbers ?
- 07-23-2013, 04:05 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Reading From File
If all you want to do is sum those numbers, I shouldn't use arrays nor a List<String>; I'd just keep an int sum= 0; ready and add those numbers to it ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 07-23-2013, 04:07 PM #5
Re: Reading From File
how can I reset the pointer ?
- 07-23-2013, 04:18 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 07-23-2013, 04:30 PM #7
Re: Reading From File
file input pointer to the start of the file ...
- 07-23-2013, 04:41 PM #8
Re: Reading From File
thanks I have done it wiht randomAccessFile ... thanks josAh ....
- 07-23-2013, 04:50 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 07-23-2013, 05:05 PM #10
Re: Reading From File
yes I can ... I was just learning how to read it more than once ..
- 07-23-2013, 05:10 PM #11
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Reading From File
Well simply opening a new FileInputStream also works, although when using a Scanner that would also mean creating a new Scanner. Which functionally makes sense IMO since you want to scan a new file to get fresh data - even if it is the exact same one as the previous time.
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 07-24-2013, 09:34 AM #12
Senior Member
- Join Date
- Jul 2013
- Location
- Wisconsin, USA
- Posts
- 106
- Rep Power
- 0
Re: Reading From File
What if you wanted to actually system.out.println the number of lines (along with those) with a method call? Would you use another class to call getter method code like this:
int numberOfLines = 0;
//Loop through the lines of text, reading each line of text and stop when a
//null value is reached (if there is no more lines in the text file, Java
//returns a value of null):
while((aLine = bf.readLine()) != null){
numberOfLines++;//increment a counter called numberOfLines
}
bf.close();//flush the temporary memory buffer called bf
return numberOfLines;//return the number of lines
}
private String getLines(String numberOfLines){
lines = numberOfLines;
return "File has " + lines;
}//end of getLines method
/*
* Because the getLines method has been set to private, we will need to
* add a non-private getter method to pass the number of lines from the
* private numLines method above.
*
* Because this getter method is not private, the method in the
* FileData.java class will call for the number of lines passed into this
* getter method, rather than directly from the private getLines method:
*/
String giveLines(int aLines){
lines = this.getLines();//The this Java keyword "this"
//means "this class", rather than another class that may have the
//same method name. It avoids any confusion.
return lines;
}
Similar Threads
-
Need help file reading a txt file (Emergency)
By 765891 in forum New To JavaReplies: 6Last Post: 02-28-2013, 01:48 AM -
HashMap with Objects, reading from file, writing to file. (note system)
By Java Dude in forum New To JavaReplies: 0Last Post: 12-15-2012, 02:37 AM -
Reading and Writing the contents of a file to another file
By priyankatxs in forum New To JavaReplies: 9Last Post: 10-20-2009, 11:52 AM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-10-2009, 12:31 AM
Bookmarks