Base class | Input stream | Output stream |
---|---|---|
Character stream | Reader | Writer |
Binary stream | InputStream | OutputStream |
** File open **
** Use the newBufferedWriter method of the BufferedWriter class ** (Buffer is a memory area that temporarily stores character string data)
Append mode with StandardOpenOption.APPEND
specified in the argument
** File output **
** write method **
Save the data in Buffer and output it as a file when it is accumulated
** Newline method ** delimited line breaks (\ n
for Unix, \ r \ n
for Windows)
You can write a list of strings to a file with the Files.write method.
* Files.write(Paths.get("./data.log"),list,StandardCharsets.UTF_8);
** File close **
Use the try-with-resources
syntax
The object created by try is ** automatically destroyed when it exits the block **! (It is okay if the close method is not called due to an error in the middle)
//Execution time./data.Record in log
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
//import java.nio.file.StandardOpenOption;
import java.time.LocalDateTime;
public class Main {
public static void main(String[] args) {
try (var writer = Files.newBufferedWriter(Paths.get("./data.log"))) {
//File addition
//try (var writer = Files.newBufferedWriter(Paths.get("./data.log"), StandardOpenOption.APPEND)) {
writer.write(LocalDateTime.now().toString());
//When unifying line feed characters
//write(LocalDateTime.now().toString()+"\r\n");
writer.newLine();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
try (var reader = Files.newBufferedReader(Paths.get("./sample.txt"))) {
var line = "";
while ((line = reader.readLine()) != null) {
//The contents of the file
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//input.Read png with BufferedInputStream
//Output the result with BufferedOutputStream.Output to png
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try (
var in = new BufferedInputStream(
new FileInputStream("./input.png "));
var out = new BufferedOutputStream(
new FileOutputStream("./output.png "))) {
var data = -1;
while ((data = in.read()) != -1) {
out.write((byte) data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
** Serialize **: Convert structured data like: to byte array
By serializing the object to a byte array, the object can be saved in the DB and passed over the network.
** Serializable class conditions **
Serializable interface implementation
public class Article implements Serializable {
Declaration only, no method = ** Marker interface **
Instance fields are basic types and serializable types
Serialization version declaration
** Specify the class version with serialVersionUID **
InvalidClassException when using different versions of class between serialize / deserialize
Has a no-argument constructor when the base class cannot be serialized
When the base class does not implement the Serializable interface and only the derived class can be serialized, call the base class constructor to deserialize the derived class.
** Serialize the object with the ObjectOutputStream class **
File output with FileOutputStream (file)
as an argument
Pass the object to the writeObject method (create article.ser)
** Deserialize with ObjectInputStream class **
Pass the file to be read as an argument and read it with the readObject method
The return value is an object
//article.Article class on ser(Serializable class)Save
//Reads the byte array from the file and outputs the contents of the deserialized public key cryptographic object
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Main {
public static void main(String[] args) {
final var file = "./article.ser";
try (var out = new ObjectOutputStream(new FileOutputStream(file))) {
out.writeObject(new Article("Java 11 changes and new API",
"https://codezine.jp/article/corner/751", false));
} catch (IOException e) {
e.printStackTrace();
}
try (var in = new ObjectInputStream(new FileInputStream(file))) {
var a = (Article)in.readObject();
System.out.println(a);
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
}
Recommended Posts