자바 File, Files

오세훈·2023년 7월 26일
0

java

목록 보기
14/16

File

Files

해당 위치의 파일에 byte 형식으로 저장

package sec2;

// nio는 네트워크 io
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

// Files를 이용한 파일 입출력
public class FilesEx1 {
    public static void main(String[] args) {
        Path path = Paths.get("D:\\chunjae_frontend\\java\\ch14\\out6.txt");
        try {
            Files.write(path, "안\"녕하세요".getBytes());
            System.out.println(new String(Files.readAllBytes(path)));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

FileReader

Byte 단위로 데이터를 읽어온다.

package sec3;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/*
파일을 읽어오는 방법
FileReader, BufferedReader, Scanner, Files

FileReader는 Byte 단위로 데이터를 읽어온다.
 */
public class FileReaderEx1 {
    public static void main(String[] args) throws IOException {
        FileReader fr = null;
        fr = new FileReader("out3.txt");
        int rd;
        while((rd = fr.read()) != -1) {
            System.out.print((char) rd);
        }

    }
}
profile
자바 풀 스택 주니어 개발자

0개의 댓글