Try this to read a file:
BufferedReader reader = null;
try {
File file = new File("sample-file.dat");
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Answer from Matthias Herlitzius on Stack OverflowJava - Reading from a Socket using a BufferedReader object
How does BufferedReader work? And lots of other questions about inputs to the system
How do I read a string in BufferedReader for use as a variable? - Processing 2.x and 3.x Forum
Using while loop and BufferedReader.
Videos
Try this to read a file:
BufferedReader reader = null;
try {
File file = new File("sample-file.dat");
reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
As far as i understand fr is the object of your FileReadExample class. So it is obvious it will not have any method like fr.readLine() if you dont create one yourself.
secondly, i think a correct constructor of the BufferedReader class will help you do your task.
String str;
BufferedReader buffread = new BufferedReader(new FileReader(new File("file.dat")));
str = buffread.readLine();
.
.
buffread.close();
this should help you.
I'm looking at this: https://github.com/MathBunny/ccc-solutions/blob/master/Senior%202015/S2/Main.java
And I really can't make sense of the BufferedReader thing. It's the first time I've seen it, I gave it a search but couldn't find anything that explains the fundamentals.
These are the lines that (I think) involve the bufferedreader:
BufferedReader in;
in = new BufferedReader (new InputStreamReader (System.in));
String input = in.readLine();
Not sure if they make sense without context, if not you could look at the link since that's where it's all from.
As you can see there are lots of questions here, I'm not expecting anyone to answer all of them (although I would love you if you did), but even if you can/want to answer just one that'd still be great! All help is appreciated!
-Questions:-
-
On that first line, what is the "in" in "BufferedReader in;"? I've never seen "in" before and I couldn't find a single thing on it after searching it up.
-
The "new" makes me think that BufferedReader is some type of object, but what's up with the argument it's creating the object with? It creates the BufferedReader object with another new object called new InputStreamReader (System.in)? I really have no clue what's going on, other than the fact that System.in is just reading input.
-
Speaking of System.in, I thought reading input required a scanner object? Or is this another way to do it entirely? How does it work? Does it only read ints? Can it be set to a variable?
-
What in the world is an input stream? Apparently it's an "abstract class is the superclass of all classes representing an input stream of bytes", but fuck if I know what that means. Can someone explain it in English?
-
I tried putting just these three lines and running them (using Netbeans IDE) and it didn't show any errors, but when I ran the program it said some exception must be caught. So I have to use a try statement? Not that I even know what a try statement is really, but it seems weird that bufferedreader will always give an error no matter what...
Note: I've been using MOOC to learn Java and I'm 2/3 of the way through and I have never seen any of these things even once. In any case, I have a trillion other questions but this is a start. Again, any help whatsoever is greatly appreciated! I really need all the help I can get!