I know this question has been probably asked a million times but I need a proper explanation cause I see the code, but I don't understand all the exceptions, throws, try, and catch stuff. I think I might need to learn exceptions first.
Also, IDK if I should use scanner or BufferReader or whatever you call it. I think bufferReader is faster but scanner is simpler. That's all I know. Please show/give me a proper explanation of at least of method to read txt files.
15 replies to this topic
#1
Posted 14 November 2011 - 10:04 PM
|
|
|
#2
Posted 15 November 2011 - 11:45 AM
Just for later use I suggest that you time it yourself. It is always better that way. Also before you go grab your stopwatch I suggest you use your coding skills to get the time in the program before you start it and the time after you finish.
Lets create a method to read the file and return it as a String.
________________________________________
------------ANSWER QUESTIONS-------------------
________________________________________
I don't understand all the exceptions, throws, try, and catch stuff
Exceptions are easy. Check out the following resource to learn more about it. If that does not satisfy you remember you can always google for more information.
Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
Please show/give me a proper explanation of at least of method to read txt files.
Read the //comments
Lets create a method to read the file and return it as a String.
public String readFile(String location) {
String data = "";
try {
FileReader fr = new FileReader(location); // Opens the file
BufferedReader br = new BufferedReader(fr); //Sets up a buffered stream so that large files can be read
String line = null;
while((line = br.readLine()) != null) { // Reads each line while 'line' is not null
data += line;
data += (char) 10; // Starts a new line, look at ASCII chart to see what 10 is
}
br.close(); // closes the file so other programs can read it
} catch (Exception e) {
e.printStackTrace(); // if the files does not exist or if the file is locked then it will let you know by giving an error
}
return data;
}
________________________________________
------------ANSWER QUESTIONS-------------------
________________________________________
I don't understand all the exceptions, throws, try, and catch stuff
Exceptions are easy. Check out the following resource to learn more about it. If that does not satisfy you remember you can always google for more information.
Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
Please show/give me a proper explanation of at least of method to read txt files.
Read the //comments
#3
Posted 15 November 2011 - 03:55 PM
Is (char)10 valid as a newline in all operating systems?
Depending on the data I'm reading, I'll switch from BufferedReader to Scanner.
When I want to quickly read different types of data (ints, doubles, longs, tokens, etc) I'll use Scanner for the convenience parsing methods given with this class.
When I'm reading other data, like longer strings, or a string composed of 1 or 2 other values, I'll use a BufferedReader and just parse the data myself.
Using a BufferedReader is also faster than a Scanner(probably due to buffer size?).
Now I'm wondering if it'll be any faster if you wrap a BufferedReader inside of Scanner
Depending on the data I'm reading, I'll switch from BufferedReader to Scanner.
When I want to quickly read different types of data (ints, doubles, longs, tokens, etc) I'll use Scanner for the convenience parsing methods given with this class.
When I'm reading other data, like longer strings, or a string composed of 1 or 2 other values, I'll use a BufferedReader and just parse the data myself.
Using a BufferedReader is also faster than a Scanner(probably due to buffer size?).
Now I'm wondering if it'll be any faster if you wrap a BufferedReader inside of Scanner
#4
Posted 15 November 2011 - 05:43 PM
In the simplest sense you can think of the exceptions like this. You are about to do 'something' with compilable code that can possibly go wrong and crash your program or the results may be undefined. So you need to put the code in a try block. Now if the 'something' goes wrong, then what? The catch block steps in here to do whatever analysis and recovery you need to do. You can use the finally block to do cleanup work, like closing connections, etc.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:
#5
Posted 15 November 2011 - 07:10 PM
lethalwire said:
Is (char)10 valid as a newline in all operating systems?
Depending on the data I'm reading, I'll switch from BufferedReader to Scanner.
When I want to quickly read different types of data (ints, doubles, longs, tokens, etc) I'll use Scanner for the convenience parsing methods given with this class.
When I'm reading other data, like longer strings, or a string composed of 1 or 2 other values, I'll use a BufferedReader and just parse the data myself.
Using a BufferedReader is also faster than a Scanner(probably due to buffer size?).
Now I'm wondering if it'll be any faster if you wrap a BufferedReader inside of Scanner
Depending on the data I'm reading, I'll switch from BufferedReader to Scanner.
When I want to quickly read different types of data (ints, doubles, longs, tokens, etc) I'll use Scanner for the convenience parsing methods given with this class.
When I'm reading other data, like longer strings, or a string composed of 1 or 2 other values, I'll use a BufferedReader and just parse the data myself.
Using a BufferedReader is also faster than a Scanner(probably due to buffer size?).
Now I'm wondering if it'll be any faster if you wrap a BufferedReader inside of Scanner
(char) 10 should be valid or they are using a really messed up OS... it is based of the ASCII chart and has worked on linux / windows for me... it's not like it is a file system or something
If you are curious then test it and don't just ask
#6
Posted 15 November 2011 - 07:52 PM
I asked because windows systems read a line separator as a carriage return followed by a new line.
I know on linux and mac systems it's different, but I'm not sure on the details.
Something like this could be more suitable:
I don't currently have a linux machine set up. It was a simple question, so no need for the doucheness.
I know on linux and mac systems it's different, but I'm not sure on the details.
Something like this could be more suitable:
final static String NEW_LINE = System.getProperty("line.separator");
Quote
If you are curious then test it and don't just ask
#7
Posted 15 November 2011 - 08:04 PM
lethalwire said:
I don't currently have a linux machine set up. It was a simple question, so no need for the doucheness.
Was not being a douche. If you test it yourself then you know for a fact if it works or not. Just trying to encourage you to give it a try rather than just accept other people's answers.
You can still ask and most likely I will respond with something other than "google it"... unless it is something that you could have just googled.
#8
Posted 15 November 2011 - 08:15 PM
Eieio said:
Was not being a douche. If you test it yourself then you know for a fact if it works or not. Just trying to encourage you to give it a try rather than just accept other people's answers.
You can still ask and most likely I will respond with something other than "google it"... unless it is something that you could have just googled.
You can still ask and most likely I will respond with something other than "google it"... unless it is something that you could have just googled.
Right...
#9
Posted 15 November 2011 - 09:21 PM
Thanks, Eieio, Lethal, and fread. +rep for all.
Lethal, how can I parse data when using the BufferedReader?
Edit: For the example Eieio gave me for reading the files, how did you use the != operator instead of !(line.equals(null)) in the WHILE loop to compare the strings?
I tried changing to to:
Lethal, how can I parse data when using the BufferedReader?
Edit: For the example Eieio gave me for reading the files, how did you use the != operator instead of !(line.equals(null)) in the WHILE loop to compare the strings?
I tried changing to to:
while((line = br.readLine()).equalsIgnoreCase("Stop")) { // Reads each line while 'line' is not equal to "STOP"
But then it read nothing at all. What is wrong?
Edited by An Alien, 15 November 2011 - 10:07 PM.
#10
Posted 15 November 2011 - 10:48 PM
while((line = br.readLine()).equalsIgnoreCase("Stop")) { // Reads each line while 'line' is [B]not [/B]equal to "STOP"
Where's the "not" part in the code? :p
#11
Posted 16 November 2011 - 04:02 AM
At the end of it all you should make a strong effort to understand the IO Section of the tutorials. The sections are divided logically, so it not to hard to follow, and you dont need to learn all right away. Focus on what you need to do and you will know which sections you need to read, and Google up some examples and study the code. Basic IO is something you will almost always need to know to write anything useful.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:
#12
Posted 16 November 2011 - 09:53 AM
An Alien said:
Thanks, Eieio, Lethal, and fread. +rep for all.
Lethal, how can I parse data when using the BufferedReader?
Edit: For the example Eieio gave me for reading the files, how did you use the != operator instead of !(line.equals(null)) in the WHILE loop to compare the strings?
I tried changing to to:
Lethal, how can I parse data when using the BufferedReader?
Edit: For the example Eieio gave me for reading the files, how did you use the != operator instead of !(line.equals(null)) in the WHILE loop to compare the strings?
I tried changing to to:
while((line = br.readLine()).equalsIgnoreCase("Stop")) { // Reads each line while 'line' is not equal to "STOP"
But then it read nothing at all. What is wrong?Pretty sure wim DC was right...
Although I do suggest this...
while((line = br.readLine()) != null && !line.matches("STOP") {
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









