I teach Java at school, so I made it from the materials at that time.
For the time being, explain the words and phrases.
You can do I (Input / Input) and O (Output / Output) of the file system!
A file system is a world created by a file that stores data in the OS and a directory that manages it collectively (Directory, Folder).
For files and directories, you can do everything we do (probably) in the OS, such as delete, create, exist,…, write, read, etc. !!
→ NewIO, a Java standard library, is recommended!
In the past, it was mainly composed of a class called File (java.io series), but now it is convenient and optimized, so it uses a package called New I / O (java.nio series). What we do is the mainstream in the future.
In NewIO, the following two (three) classes generally have a basic role.
Path:A class that converts a String to represent the path of a file
Paths:Class for creating Path
Files:Class that actually operates files
Get Path (Example)… File location!
Path path = Paths.get(“C:\\”, “a”);
Example of using Files ... Perform operations on files / directories.
Files.exsit(path);
Basically, you should use the following two classes. (Although most of the other classes are basically old, please note that they are often used in other people's programs even if they are old.)
BufferedWriter:A class that writes to a file efficiently using a buffer
BufferedReader:A class that efficiently reads a file using a buffer
Data that is temporarily placed in the memory space. Also known as tmp / temp.
Maybe I don't use it much (in NewIO).
BufferedWriter bw = new BufferedWriter(Writer);
BufferedReader br = new BufferedReader(Reader);
BufferedWriter bw = Files.newBufferedWriter(path);
bw.write(String);
String line = br.readLine();
try {
BufferedWriter bw = Files.newBufferedWriter(path);
bw.write(“A”);
bw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
In addition, an exception may occur in close, and exception handling for close is also necessary. Therefore, it is necessary to do as follows.
try {
BufferedWriter bw = Files.newBufferedWriter(path);
bw.write(“A”);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
Fucking long
It's very long to write like before. Therefore, the try-catch-resouce syntax was added from Java 7, and it became possible to write concisely as follows.
try (BufferedWriter bw = Files.newBufferedWriter(path)) {
bw.write(“A”);
}
very good. Reference: https://qiita.com/Takmiy/items/a0f65c58b407dbc0ca99
Just calling br.write (text) will not break the line,
br.write("AA");
br.write("BB");
↓
AABB
It will be something like.
To start a newline, add a newline character at the end of the line string.
bw.write(text + System.lineSeparator());
About the usual syntax we've used so far ...
System.out.println(“Hello world!”);
If you understand FileIO, you will think that it looks like FileIO.
What do you mean? It will be.
In fact, System.out (out) is an OutputStream! That is. (That is, for output)
For the time being, Stream is an abstraction of FileIO. (maybe) Similarly, there is also System.in, which is an InputStream. (That is, for input)
However, this is natural when you think about it, and writing characters on the console is not much different from writing characters on a file (I thought it was interesting to have this kind of commonality).
-Oracle official commentary pages -Rewrite the code of java.io.File using java.nio.Path and java.nio.Files -Organize Java file I / O related classes / interfaces
Recommended Posts