Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- group by
- mysql
- rsync
- spring
- 명령어
- 리눅스
- 트랜잭션
- DBMS
- Linux
- mssql
- PostgreSQL
- analytics4
- 오블완
- docker
- SQL
- IntelliJ
- 호이스팅
- 추상클래스
- github
- MariaDB
- oracle
- 티스토리챌린지
- pandas
- Python
- git
- 자바
- MongoDB
- Javascript
- JPA
- java
Archives
- Today
- Total
hanker
JAVA - 파일 입출력 방식 (Java Input/Output) 본문
반응형
JAVA 에서 파일 입력, 출력하는 방법에 대해서 알아보자
1. 파일 쓰기 방식 (BufferedWriter / Files.write)
BufferedWriter를 사용해서 파일 쓰기
- BufferedWriter는 버퍼링된 파일 쓰기 기능을 제공한다. 즉, 데이터를 한번에 메모리로 모은 후 파일에 쓰기 때문에 성능이 좋다.
- 여러 줄을 연속해서 쓰거나, 파일을 자주 열고 다는 경우에 적합
예제.
public static void main(String[] args) {
// 파일 경로 설정
String filePath = "D:\\hanker\\test.txt";
// BufferedWriter를 사용한 파일에 쓰기
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
writer.write("Hanker\n");
writer.write("BufferedWriter 사용 예제");
} catch (IOException e) {
e.printStackTrace();
}
}

텍스트 파일에 잘 입력된다.
Files.write를 사용한 파일 쓰기
- Files.write()는 데이터를 한 번에 파일에 쓴다. 작은 양의 데이터를 쓸 때 매우 간편하게 사용가능하다.
- 파일 경로와 내용을 바이트 배열로 전달하며, 간결하게 쓰기 작업을 수행한다.
예제.
public static void main(String[] args) {
// 파일 경로 설정
String filePath = "D:\\hanker\\test.txt";
// Files.write를 사용한 파일에 쓰기
try {
Files.write(Paths.get(filePath), "Hanker\nFiles.write() 사용예제".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}

반응형
2. 파일 읽기 방식 (BufferedReader / Files.readAllLines())
BufferedReader를 사용한 파일 읽기
- BufferedReader는 버퍼링된 파일 읽기 기능을 제공한다.
- 파일에서 데이터를 한 줄씩 읽으며, 읽은 데이터를 처리할 수 있다.
예제.

public static void main(String[] args) {
// 파일 경로 설정
String filePath = "D:\\hanker\\test.txt";
// BufferedReader를 사용한 파일 읽기
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
int i = 1;
while ((line = reader.readLine()) != null) {
System.out.println("BufferedReader 읽기(Line " + i + "): " + line);
i++;
}
} catch (IOException e) {
e.printStackTrace();
}
}

Files.readAllLines를 사용한 파일 읽기
- Files.readAllLines()는 파일의 모든 내용을 한 번에 읽어 들이고, 각 줄을 리스트 형태로 반환한다.
- 간단히 파일을 읽어서 모든 줄을 처리해야 하는 경우 사용하기 좋다.
예제.

public static void main(String[] args) {
// 파일 경로 설정
String filePath = "D:\\hanker\\test.txt";
// Files.readAllLines를 사용한 파일 읽기
try {
List<String> lines = Files.readAllLines(Paths.get(filePath));
for (int i = 0; i < lines.size(); i++) {
System.out.println("Files.readAllLines 읽기(Line " + (i+1) + "): " + lines.get(i));
}
} catch (IOException e) {
e.printStackTrace();
}
}

정리
BufferedReader/BufferedWriter는 대용량 파일 처리에 적합
Files.write()와 Files.readAllLines()는 작은 파일을 간단히 처리할 때 유용
반응형
'JAVA' 카테고리의 다른 글
JAVA - JDK, JRE, JVM의 차이점은 무엇일까? (0) | 2024.11.24 |
---|---|
JAVA - 자바의 주요 특징은 무엇일까? (0) | 2024.11.23 |
JAVA - int to String ↔ String to int (정수 ↔ 문자열로 변경) (1) | 2024.11.12 |
JAVA - 문자열이 특정 문자로 시작하는지 확인하기 : startsWidth() 사용법 (0) | 2024.11.11 |
java.lang.UnsupportedOperationException 에 대해서 (1) | 2024.11.09 |