Let's classify IO from several points of view
__Advantages of using high level stream __
There are more than 40 IO related classes in java, InputStream / Reader and OutputStream / Writer are all base classes.
Classification | byte input stream | byte output stream | char input stream | char output stream |
---|---|---|---|---|
Abstract basis | InputStream | OutputStream | Reader | Writer |
File IO | FileInputStream | FileOutputStream | FileReader | FileWriter |
Array IO | ByteArrayInputStream | ByteArrayOutputStream | CharArrayReader | CharArrayWriter |
Inter-thread IO | PipedInputStream | PipedOutputStream | PipedReader | PipedWriter |
String IO | StringReader | StringWriter | ||
Buffer function | BufferdInputStream | BufferdOutputStream | BufferdReader | BufferdWriter |
byte⇒char operation conversion | InputStreamReader | OutputStreamWriter | ||
Object IO | ObjectInputStream | ObjectOutputStream | ||
Abstract basis? | FilterInputStream | FilterOutputStream | FilterReader | FilterWriter |
PrintStream | PrintWriter | |||
pushback! | PushbackInputStream | PushbackReader | ||
Special guy | DataInputStream | DataOutputStream |
FileInputStream/FileOutputStream, FileReader/FileWriter Since it is a direct disk access, it is a low level stream.
file-input.txt
abcd1\r\n
Ah
FileInputStream
try (FileInputStream fi = new FileInputStream("./FileIOSample/file-input.txt")) {
//4 byte buffer
byte[] buff = new byte[4];
//Number of bytes read
int hasRead = 0;
//Read up to buff
while ((hasRead = fi.read(buff)) > 0) {
//Convert the read byte to a character string and output
System.out.println(new String(buff, 0, hasRead));
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
output
abcd
1
�
���
�
abcd: First 4byte output 1 \ r \ n.: The first half (1 byte) of "A" was taken, so the characters are garbled! ���: The latter half of "A" (1 byte) + 3 bytes, so garbled characters!
__ Conclusion __: Acquires the specified bytes at a time, and in the case of multi-byte characters, the characters may be garbled.
FileReader
try (FileReader fr = new FileReader("./FileIOSample/file-input.txt")) {
//4 char buffer
char[] buff = new char[4];
//Number of characters read
int hasRead = 0;
//Read up to buff
while ((hasRead = fr.read(buff)) > 0) {
//Convert the read byte to a character string and output
System.out.println(new String(buff, 0, hasRead));
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
output
abcd
1
Ah
Say
abcd: 4-character output 1 \ r \ n A: 4 character output Say: Output the last 2 characters
__ Conclusion __:
FileOutputStream/FileWriter
FileInputStream
try (
FileInputStream fi = new FileInputStream("./FileIOSample/file-input.txt")) {
FileOutputStream fo = new FileOutputStream("./FileIOSample/file-output-1.txt");
//4 byte buffer
byte[] buff = new byte[4];
//Number of units read(Number of bytes)
int hasRead = 0;
while ((hasRead = fi.read(buff)) > 0) {
//Write as much as you read
fo.write(buff, 0, hasRead);
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
try (
FileReader fr = new FileReader("./FileIOSample/file-input.txt");
FileWriter fo = new FileWriter("./FileIOSample/file-output-2.txt")) {
//4 char buffer
char[] buff = new char[4];
//Number of units read(Number of bytes)
int hasRead = 0;
while ((hasRead = fr.read(buff)) > 0) {
//Write as much as you read
fo.write(buff, 0, hasRead);
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
file-output-1.txt
abcd1
Ah
file-output-2.txt
abcd1
Ah
__ Conclusion __: Both byte and char operations can be output normally.
When a character string is output
FileWriter fo = new FileWriter("./FileIOSample/file-output-3.txt")) {
fo.write("Line 1");
fo.write(System.lineSeparator());
PrintStream/PrintWrite
python
try (
FileOutputStream fo = new FileOutputStream("./FileIOSample/file-output-4.txt");
PrintStream ps = new PrintStream(fo)){
ps.println(1);
ps.println("aa");
ps.println("AIUEO");
ps.println(new String("a ah"));
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
try (
FileOutputStream fo = new FileOutputStream("./FileIOSample/file-output-5.txt");
PrintWriter ps = new PrintWriter(fo)){
ps.println(1);
ps.println("aa");
ps.println("AIUEO");
ps.println(new String("a ah"));
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
file-output-4,5.txt
1
aa
AIUEO
aaa
Summary:
StringReader,StringWriter
StringReader,StringWriter
String src = "AIUEO" + System.lineSeparator()
+ "abcde" + System.lineSeparator()
+ "3rd line";
+
try (StringReader sr = new StringReader(src)) {
char[] buff = new char[4];
int hasRead = 0;
while ((hasRead = sr.read(buff)) > 0) {
System.out.print(new String(buff, 0, hasRead));
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
try (StringWriter sr = new StringWriter()) {
sr.write("123");
sr.write("Ah");
System.out.println(sr.toString());
} catch (IOException e) {
e.printStackTrace();
}
output
AIUEO
abcde
3rd line
123
InputStreamReader/OutputStreamWriter InputStreamReader / OutputStreamWriter converts byte stream to char stream.
InputStreamReader,BufferedReader
try (InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(reader)) {
String line;
System.out.print("input:");
while ((line = bufferedReader.readLine()) != null) {
if (line.equals("exit")) {
System.exit(0);
}
System.out.println("output:" + line);
System.out.print("input:");
}
} catch (IOException e) {
e.printStackTrace();
}
PushbackInputStream/PushbackReader
A buffer for returning is prepared, and it can be returned to the buffer by the following method. unread(int b) unread(byte[] | char[] buff) unread(byte[] | char[] buff, int off, int len)
push-back.txt
123456789
push-back-stream
int pushbackBuff = 8;
try (
PushbackReader pr = new PushbackReader(new FileReader("push-back.txt"), pushbackBuff)) {
int hasRead = 0;
int size = 1;
char[] buff = new char[size];
while ( (hasRead = pr.read(buff)) != -1) {
System.out.println(new String(buff, 0, hasRead));
// push back
pr.unread(buff, 0, hasRead);
buff = new char[++size];
}
} catch (IOException ex) {
System.out.println();
}
output
1
12
123
1234
12345
123456
1234567
12345678
123456789
Processing image
1
into buff from the file1
into push-back-buff1
enters buff from push-back-buff2
enters buff from the file12
into push-back-buff12
enters buff from push-back-buff first.3
enters buff from the fileRepeat this! !!
ObjectInputStream/ObjectOutputStream A stream for serializing and deserializing objects. In order to serialize, you need to implement Serializable / Externalizable.
Model to use
import java.io.Serializable;
public class Person implements Serializable{
public String name;
public int age;
public Person(String name, int age) {
System.out.println("Create person.");
this.name = name;
this.age = age;
}
}
public class Student implements Serializable{
public String name;
public int age;
public Student(String name, int age) {
System.out.println("Create student.");
this.name = name;
this.age = age;
}
}
public class Teacher implements Serializable{
public String name;
public int age;
public Student student;
public Teacher(String name, int age, Student student) {
System.out.println("Create teacher.");
this.name = name;
this.age = age;
this.student = student;
}
}
Single object in/out
System.out.println("------Serialize------");
try (
//Output destination(low level stream)
FileOutputStream fo = new FileOutputStream("object-out-1.data");
//Object output story(high level stream)
ObjectOutputStream out = new ObjectOutputStream(fo)) {
//Object output
out.writeObject(new Person("alex", 30));
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
System.out.println("------Deserialize------");
try (
// (low level stream)
FileInputStream fi = new FileInputStream("object-out-1.data");
//Object output story(high level stream)
ObjectInputStream in = new ObjectInputStream(fi)) {
Person p = (Person) in.readObject();
//Object output
System.out.println(p.name);
System.out.println(p.age);
} catch (IOException | ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
output
#------Serialize------
Create person.
#------Deserialize------
alex
30
Summary:
Create person.
is not output, no constructor is required when deserializing.//Seriousize
out.writeObject(new Person("alex", 30));
out.writeObject(new Student("king", 20));
//Desilia size
Person p = (Person) in.readObject();
Student s = (Student) in.readObject();
Summary:
//Seriousize
out.writeObject(new Teacher("alex", 30, new Student("king", 20)));
// writing aborted; java.io.NotSerializableException: io.serializable.Student
Teacher t = (Teacher) in.readObject();
Summary:
python
Student s = new Student("king", 20);
//Seriousize
out.writeObject(new Teacher("alex", 30, s));
out.writeObject(new Teacher("bill", 40, s));
//Desilia size
Teacher alex = (Teacher) in.readObject();
Teacher bill = (Teacher) in.readObject();
System.out.println(alex.student == bill.student); // true
Summary: Even if the same reference is deserialized, only one object will be referenced as before serialization. Serialized image:
serialVersionUID This is to confirm that the classes at the time of serialization and deserialization are the same. If the front and back are different, "Exception in thread" main "java.io.InvalidClassException" will occur.
java represents standard entry and exit in System.in and System.out, defaulting to keyboard and display.
Standard output redirection
try (FileOutputStream fileOutputStream = new FileOutputStream("strandard-output.txt");
PrintStream print = new PrintStream(fileOutputStream)) {
System.setOut(print);
System.out.println("aaaaabbbbb");
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
NIO : https://qiita.com/liguofeng29/items/c827af3d62f219e17755 NIO2 : https://qiita.com/liguofeng29/items/3d0ba350a1547630727b
Recommended Posts