해당 위치의 파일에 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);
}
}
}
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);
}
}
}