Recently, I started writing Java in class. I didn't understand the relationship between File, InputStream, FileReader, and Scanner, so I will summarize them.
This article assumes Java SE 1.8. However, this article is a summary of older java.io, not java.nio.
There is no exception handling in the code, but you need to be able to receive a FileNotFoundException when opening a file and an IOException when reading or writing a file (or throw it to a method above).
There is a summary diagram at the bottom of the page.
https://docs.oracle.com/javase/jp/8/docs/api/java/io/package-summary.html
File, FileDescriptor
File file = new File("file.txt"); //Can be created from a file name
InputStream Class for reading bytes
--java.io.InputStream abstract class --java.io.FileInputStream: Class for building InputStream from files --java.io.FilterInputStream: Class for holding InputStream --java.io.BufferedInputStream: Buffer the InputStream and make it appear as an InputStream.
// System.in is an instance of InputStream
InputStream sytemin = System.in;
//Create a stream from a file
InputStream fileStream = new FileInputStream("file.txt");
//Buffer the stream
InputStream bufferedStream = new BufferedInputStream(fileStream);
bufferedStream.close();
Reader Classes with'Reader'at the end of the name, such as InputStream'Reader', are classes for reading string streams.
--java.io.Reader abstract class --java.io.InputStreamReader: InputStream => Bridging to Reader. You can specify the character set --java.io.FileReader: Simple class (Reader is built with File, FileDescriptor, fileName. Character encoding cannot be specified.) --java.io.BufferedReader: Buffer the Reader class
//Create a byte stream from a file
InputStream fileStream1 = new FileInputStream("file1.txt");
//Create a string stream from a byte stream
Reader reader1 = new InputStreamReader(fileStream1);
//Create a byte stream from a file
InputStream fileStream2 = new FileInputStream("file2.txt");
//Create a string stream. Specifying string encoding
Reader reader2 = new InputStreamReader(fileStream2, "utf-8");
//Create a Reader directly from a file using a simple class
Reader reader3 = new FileReader("file3.txt");
//Buffer the reader
BufferedReader bufferedReader3 = new BufferedReader(reader3);
//BufferedReader reads a line of text readLine()There is
String line = bufferedReader3.readLine();
reader1.close();
reader2.close();
bufferedReader3.close();
java.util.Scanner A scanner that retrieves primitive types such as int, long, boolean, and strings from text. It can be constructed from the File class and InputStream class. You can also specify the string encoding. Delimiter matching can be specified with a regular expression. For more information, https://docs.oracle.com/javase/jp/8/docs/api/java/util/Scanner.html
// System.Read a number from in
Scanner sc1 = new Scanner(System.in);
int i = sc1.nextInt();
//Build from File
Scanner sc2 = new Scanner(new File("file.txt"));
// Scanner sc2 = new Scanner("file.txt"));I can not do such a thing.
//If you do this"file.txt"I try to read the character string.
//Change delimiter to comma
sc2.useDelimiter(",");
//Display one line for each delimiter
while (sc2.hasNext())
{
String str = sc2.next();
System.out.println(str);
}
// sc1.close();
sc2.close();
name of the class | Disassembly | meaning |
---|---|---|
FileInputStream | File-InputStream | Convert File to InputStream. |
InputStreamReader | InputStream-Reader | Convert InputStream to Reader. |
FileReader | File-Reader | Convert File to Reader. |
Recommended Posts