1 답변
-
Java 7
Files 클래스 쓰면 쉬워요.
try { Files.write(Paths.get("myfile.txt"), "the text".getBytes(), StandardOpenOption.APPEND); }catch (IOException e) { //exception handling left as an exercise for the reader }
근데 님이 똑같은 파일에 여러번 쓰면 파일 열고 닫고 많이해서 느리니까
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true)))) { out.println("the text"); //more code out.println("more text"); //more code }catch (IOException e) { //exception handling left as an exercise for the reader }
이렇게 하세요.
FileWriter
의 두번째 인자는 파일에 추가할건지 여부를 말하고요. 여러번 쓸거면BufferedWriter
를 추천합니다.PrintWriter
는println
에 접근권한을 주는데 이건 님이 많이써본 System.out.그거랑 똑같아요.자바 옛날 버전에선
try { PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true))); out.println("the text"); out.close(); } catch (IOException e) { //exception handling left as an exercise for the reader }
댓글 입력