Videos
I dare say the file is in the src directory - but I suspect that's not the current working directory of the program. To check this, run this code:
System.out.println(new File(".").getAbsolutePath());
Options:
- Specify an absolute filename
- Specify a relative filename which takes into account where you're running this
- Bundle the file as a resource and use
Class.getResourceAsStreamor similar
Note that this has nothing to do with BufferedReader - you're reading the text from System.in with no problems.
It's looking in a path different than your source directory. Try specifying a full path like c:\input.txt (but don't forget to move your file there!) to see what I mean.
Yes, your conclusion is correct subclasses of Reader and Writer are for reading/writing text content. InputStream / OutputStream are for binary content. If you take a look at the documentation:
Reader- Abstract class for reading character streams
InputStream- Abstract class is the superclass of all classes representing an input stream of bytes.
FileReader (and indeed anything extending Reader) is indeed for text. From the documentation of Reader:
Abstract class for reading character streams.
(Emphasis mine.) Look at the API and you'll see it's all to do with text - char instead of byte all over the place.
InputStream and OutputStream are for binary data, such as mp4 files.
Personally I would avoid FileReader altogether though, as it always uses the system default character encoding - at least before Java 11. Instead, use InputStreamReader around a FileInputStream... but only when you want to deal with text. (Alternatively, use Files.newBufferedReader.)
As an aside, that's a very inefficient way of copying from an input to an output... use the overloads of read and write which read into or write from a buffer - either a byte[] or a char[]. Otherwise you're calling read and write for every single byte/character in the file.
You should also close IO streams in finally blocks so they're closed even if an exception is thrown while you're processing them.