Ralph Cook wrote:I'm afraid file I/O in java is bound to be a bit daunting for a new programmer. It is definitely a multi-layered affair.
The javadoc for Scanner says "A simple text scanner which can parse primitive types and strings using regular expressions." So you might suspect that you don't need it, because you aren't parsing anything. There's bound to be something simpler, you might say.
FileReader is another possibility, but has no reading methods of its own, and its superclass InputStreamReader has nothing for reading a line. I would regard it as an unnecessary pain in the neck to have to handle reading until end-of-line, etc., surely somewhere in the java standard classes is something to do that.
For your purposes, I think BufferedReader has the method you need, readLine(). It's only drawback is a lack of a constructor that takes a filename.
So you do that with the FileReader, which extends InputStreamReader, which extends Reader, which can be passed to the BufferedReader constructor.
BufferedReader reader = new BufferedReader(new FileReader("Data.txt"));
After that you can execute reader.readLine() to get the next line from Data.txt.
Now. Your current code loops until there is no more input from your Scanner object. So there isn't likely to be a line to print out, in that case. Your current code might work if you just read one line with Scanner and return. But I thought I'd throw in a little about choosing a class to use to read the file while we were at it.
rc
Deep Purohit wrote:Your program looks ok.
Are you sure there is some content inside the data.txt file?
Try to give absolute path (D:\movies\There_Will_Be_Blood.avi) of the file and reading it.
Now for your frustration part, you can invest just 5 minutes of your time reading:
Scanner, FileReader, BufferedReader
And you'll know that Scanner might just solve the purpose.
Frustration is just the final step of doing something right. So if you are frustrated your are almost there.
Update: Well, rc has explained everything you will need to know. So now you have option of choosing any of it Scanner/BufferedReader.
.
**P.S. By the way how do I indent my codes here in the forum(Like from the first post, with numberings beside)? Sorry this is my first time here in the forum and I don't want to break any rules.
Emmanuel de Ternay wrote:Scanner is not the right class to read files. You should use instead FileReader associated with BufferedReader, as Ralph Cook said.
Anyway, your method should work. I think the problem is that for each iteration, you replace the content variable with the new line, loosing the previous inserted lines in the variable. And what you see is the last line of your file, maybe a blank line.
Try to change line 22 by:
It should work.
Emmanuel de Ternay wrote:Put the Data.txt file outside of the src directory (in the project directory if you use eclipse) and you should be able to read the file. I made the test and I could read the file.
If you want to keep it in src directory, you have to put "src/Data.txt" as filename.
Aces Quacck wrote:I tried that too. All those didn't work. I've been working from early afternoon till now...
Ralph Cook wrote:
Aces Quacck wrote:I tried that too. All those didn't work. I've been working from early afternoon till now...
Saying "it didn't work" is never enough. Obviously things have changed since you last posted the code. Perhaps you should post it again.
rc
Maneesh Godbole wrote:Is this the real location C:\Users\Aces\workspace\Year2Sem1\Practical2\src\Data.txt
or this one C:\\Users\Aces\workspace\Year2Sem1\Practical2\src\Data.txt
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |