Let's use Java New FileIO! (Introduction, for beginners)

Trigger

I teach Java at school, so I made it from the materials at that time.

Explanation of words

For the time being, explain the words and phrases.

What is FileIO?

You can do I (Input / Input) and O (Output / Output) of the file system!

What is a 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).

Introduction

What can you do with FileIO?

For files and directories, you can do everything we do (probably) in the OS, such as delete, create, exist,…, write, read, etc. !!

So how do you do that in Java?

→ 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.

Let's use NewIO

Getting started with NewIO

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

Get Path (Example)… File location!

Path path = Paths.get(“C:\\”, “a”);

Examples of using Files

Example of using Files ... Perform operations on files / directories.

Files.exsit(path);

Reading and writing to files

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

What is Buffer?

Data that is temporarily placed in the memory space. Also known as tmp / temp.

BufferedWriter / BufferedReader (instance) generation

Old way

Maybe I don't use it much (in NewIO).

BufferedWriter bw = new BufferedWriter(Writer);
BufferedReader br = new BufferedReader(Reader);

Generation of BufferedWriter / BufferedReader using NewIO

BufferedWriter bw = Files.newBufferedWriter(path);

Use of BufferedWriter / BufferedReader

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

Use of BufferedWriter / BufferedReader (improvement) ~ try-catch-resouce!

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

How to start a new line? (Use of write ())

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

Task

  1. Write a string to the file
  2. Multi-line version of "1." (using List)
  3. Read the file
  4. Multi-line version of "3." (line expression using List), all lines may be used, or any range of lines may be used.

column

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).

Reference / Reference

-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

Let's use Java New FileIO! (Introduction, for beginners)
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
[Introduction to Java] Basics of java arithmetic (for beginners)
Introduction to Java for beginners Basic knowledge of Java language ①
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
[For super beginners] DBUnit super introduction
Java for beginners, data hiding
[For super beginners] Maven super introduction
Java application for beginners: stream
New grammar for Java 12 Switch statements
[For beginners] Summary of java constructor
Rock-paper-scissors game for beginners in Java
Java for beginners, expressions and operators 1
[For beginners] Run Selenium in Java
Java for beginners, expressions and operators 2
[For super beginners] Mirage SQL super introduction
java (use class type for field)
[For Java beginners] About exception handling
Classes and instances Java for beginners
Introduction to java for the first time # 2
Learn Java with "So What" [For beginners]
[For beginners] Difference between Java and Kotlin
Introduction of New Generation Java Programming Guide (Java 10)
Learning for the first time java [Introduction]
[Java] Introduction
Introduction of New Generation Java Programming Guide (Java 11)
Let's use Swift Package Manager (SwiftPM) ~ Introduction ~
Introduction of New Generation Java Programming Guide (Java 12)
[For beginners] Introduction to Java Basic knowledge of Java language ③ Array, selection structure, iteration structure
Book introduction: Spring Boot Recommended reference book for beginners!
[Java] What should I use for writing files?
Use Java7 try-with-resources statement for Cursor close processing
Pleiades Eclipse 2020-03 release ~ Java 14 Let's try new features!
Let's keep this in mind What's new in Java 9
Kantai Collection Java # 1 Classes and Objects [For Beginners]
A collection of simple questions for Java beginners
Use Java external library for the time being
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]
[For super beginners] How to use autofocus: true
Let's study Java
For JAVA learning (2018-03-16-01)
Let's use jcmd
2017 IDE for Java
java1.8 new features
[Java] Introduction to Java
[Java] Use Collectors.collectingAndThen
Introduction to java
Java for statement
[Java] Let's create a mod for Minecraft 1.14.4 [0. Basic file]
[Java] Let's create a mod for Minecraft 1.14.4 [4. Add tools]
[Java] Let's create a mod for Minecraft 1.14.4 [5. Add armor]
[For beginners] Let's be able to coat like Swift!
[For beginners] Quickly understand the basics of Java 8 Lambda
[Java] Let's create a mod for Minecraft 1.14.4 [Extra edition]
[Java] Let's create a mod for Minecraft 1.14.4 [7. Add progress]
[Java] Let's create a mod for Minecraft 1.14.4 [6. Add recipe]
[For beginners] How to operate Stream API after Java 8
[Java] Let's create a mod for Minecraft 1.16.1 [Add item]
[Java] Let's create a mod for Minecraft 1.16.1 [Basic file]