private static final String PATH = "temp/hello2.txt";
public static void main(String[] args) throws IOException {
String writeString = "abc\n가나다";
System.out.println("== Write String ==");
System.out.println(writeString);
Path path = Path.of(PATH);
// 파일에 쓰기
Files.writeString(path, writeString, UTF_8);
// 파일에서 읽기
String readString = Files.readString(path, UTF_8);
System.out.println("== Read String ==");
System.out.println(readString);
}
- readAllLines
private static final String PATH = "temp/hello2.txt";
public static void main(String[] args) throws IOException {
String writeString = "abc\n가나다";
System.out.println("== Write String ==");
System.out.println(writeString);
Path path = Path.of(PATH);
// 파일에 쓰기
Files.writeString(path, writeString, UTF_8);
// 파일에서 읽기
System.out.println("== Read String ==");
List<String> lines = Files.readAllLines(path, UTF_8);
for (int i = 0; i < lines.size(); i++) {
System.out.println((i + 1) + ": " + lines.get(i));
}
}
'개발이 좋아서 > Java가 좋아서' 카테고리의 다른 글
네트워크 - 인터넷 통신 / IP(Internet Protocol) / TCP, UDP / PORT / DNS (0) | 2024.11.27 |
---|---|
파일 복사 최적화 (0) | 2024.11.27 |
Files (0) | 2024.11.27 |
File (0) | 2024.11.27 |
I/O - XML, JSON (0) | 2024.11.27 |