HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🌚
[New] 우기팀
/
득윤
득윤
/
🗃️
Java File IO Sample Examples
🗃️

Java File IO Sample Examples

 
IO 패키지
→ java.io
→ FileReader, FileWriter, FileInputStream, FileOutputStream
 

NIO 패키지
→ java.nio.file
→ Path - represents the path to actual file or directory you are willing to work
→ Paths - Path의 유틸리티 클래스
notion image
 
sample code
Path p = Paths.get("D:\\path\\to\\new\\dir"); Path path = Files.createDicrectory(p);
디렉토리 생성 코드
 
Path path = Paths.get("DataSet/empty-poem.txt"); Path donePath = Files.createFile(path);
빈 파일 작성
 
Path path = Paths.get("DataSet/poem.txt"); Path donePath = Files.createFile(path); String poem = "하늘을 우러러\n 한점 부끄럼 없거늘\n..."; Files.write(donePath, poem.getBytes(StandardCharsets.UTF_8));
바이트 시 쓰기
 
Path path = Paths.get("DataSet/poem.txt"); List<String> lines = Files.readAllLines(path);
파일의 모든 라인 읽기