Java is full of methods and classes partially overlapping. I find that you very quickly move on from using Scanner, and just use (Buffered)Reader to read everything as Strings: it's just more powerful. Scanner is skipping nextLine() after using next() or nextFoo()?) and it's not-as-easy-as-you'd-think to do things like validation of user entering a number instead of a general string. "Enter your name, enter your favorite color") but it has sharp edges that catch many beginners out (e.g. Scanner is a pretty poor class, honestly: it's used frequently in basic programs (e.g. Scanner is basically a tokenizer, although there's also the much older StringTokenizer. ![]() This is just building on top of the functionality of other things, basically by reading internally from a Reader. Scanner is a high-level class which allows you to read data from a String (or, generally, a Readable, which is implemented by Reader and BufferedReader, for example), but getting that data as a type other than String - for example, you could read 1 2.0 true as an int, double and boolean respectively, without having to do the parsing yourself. To me, the canonical example of what BufferedReader allow you to do over and above a Reader is to read a whole line at once. It also provides convenience methods to get a String instead of a char. It is generally more efficient to read lots at once rather than just one, assuming you're going to need more than one. If you want to read an InputStream as chars, you use an InputStreamReader, for which you specify a CharSet, which allows the bytes to be converted to chars correctly.Ī BufferedReader is a Reader that buffers its input - it reads many bytes from the source at once, rather than one at a time. If you want to read chars, you use a Reader. If you're going to be reading them from a file, you use a FileInputStream but not all sequences of bytes come from files, for example, ByteArrayInputStream allows you to read a sequence of bytes from a byte but because it's an InputStream, it can be used in exactly the same way as if it came from a file. If you want to read bytes, you use an InputStream. Strings are sequences of chars, not bytes. (Correspondingly, Writers write chars OutputStreams write bytes). Readers read chars InputStreams read bytes. I'm not confused about this but.why? I mean, why are there different methods of doing the same thing? What's the significance? And which method is the most efficient way of dealing with file inputs and outputs? Again, like the InputStream and OutputStream, these classes are abstract super classes and have their own subclasses to perform read and write operations. ![]() These classes have their respective subclasses like FileInputStream, FileOutputStream, etc.įurther into my research I came across the Reader and Writer classes which are used to read data from and write data to files. So I did a bit of research and found out that the classes that lie under the hood to perform these operations are the InputStream and OutputStream. Then I asked him what's the difference between using a BufferedReader and a Scanner to read data from a file. When I was examining his code, I noticed that he had used a BufferedReader and BufferedWriter - a method of reading and writing to files that I have not used before. Like this: tryįileWriter writer = new FileWriter("Foo.txt") Ī few days ago I was at a meeting with my instructor. So far I've been using Scanners to read data from text files.Įxample: File file = new File("path oile") Īnd used FileWriters to write data into text files.
0 Comments
Leave a Reply. |