[java.io] Summary of Java string input (InputStream, Reader, Scanner)

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.

java.io reference

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();

Diagram showing the relationship of usage

java.io4.png

The law of naming

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

[java.io] Summary of Java string input (InputStream, Reader, Scanner)
Acquisition of input contents using Scanner (Java)
Summary of Java support 2018
java Scanner loop input
[Java11] Stream Summary -Advantages of Stream-
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
[Java] Summary of control syntax
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
[For beginners] Summary of java constructor
Various methods of Java String class
Summary of [Java silver study] package
[Java] Input to stdin of Process
[Java] Correct comparison of String type
Summary of object-oriented programming using Java
[Java Silver] Summary of access modifier points
Story of paiza.jp [Java standard input solution]
[java] Summary of how to handle char
Summary of changes other than JEP of Java10
[Java] Personal summary of conditional statements (basic)
[Java] [Maven3] Summary of how to use Maven3
[Java] Comparison of String type character strings
Java Summary of frequently searched type conversions
Summary of Java Math.random and import (Calendar)
Java string
The story of low-level string comparison in Java
[java] Summary of how to handle character strings
Summary of Java environment settings for myself [mac]
[Java] Personal summary of classes and methods (basic)
[Java] Summary of how to abbreviate lambda expressions
[Java] Get the length of the surrogate pair string
[Java] The confusing part of String and StringBuilder
[Note] Java: Measures the speed of string concatenation