자바 - 텍스트 파일 내용을 읽어서 줄번호로 파일 쓰기
글쓴이: onerat / 작성시간: 수, 2018/09/05 - 10:59오후
텍스트 파일에서 줄바꿈은 \n을 통해서 알 수 있고 줄번호를 여기서 어떻게 붙이는 건가요..ㅠㅡㅠ
정말 간단한 거 같은데 너무 초보인지라 어떻게 하는지 모르겠습니다 도와주세요..~
import java.io.*;
public class WriterExample {
public static void main(String args[])
{
FileWriter writer = null;
try{
writer = new FileWriter("output.txt");
char arr[] = {'손','흥','민'};
for(int cnt = 0; cnt < arr.length; cnt++)
writer.write(arr[cnt]);
}
catch(IOException ioe)
{
System.out.println("파일을 출력할 수 없습니다.");
}
finally{
try{
writer.close();
}
catch(Exception e)
{
}
}
}
}Forums:


참고해보세요.
https://ide.goorm.io
https://www.tutorialspoint.com/compile_java_online.php
https://blog.naver.com/ykwon89/221205971161
https://book.naver.com/search/search.nhn?sm=sta_hty.book&sug=&where=nexearch&query=java
input.txt
package project; import java.io.*; import java.util.StringTokenizer; public class Main { public static void main(String [] argv) throws IOException { //read File input = new File("input.txt"); FileReader fr = new FileReader(input); BufferedReader br = new BufferedReader(fr); //write File output = new File("output.txt"); FileWriter fw = new FileWriter(output); BufferedWriter bw = new BufferedWriter(fw); String text = ""; while(true) { String str = br.readLine(); if(str == null) { break; } text += str; System.out.println("파일에서 읽은 데이터 : "); System.out.println(str); bw.write(str+"\n"); } //예제 출처 //http://hunit.tistory.com/166 StringTokenizer str = new StringTokenizer(text, "\t"); int countTokens = str.countTokens(); for(int i=0; i<countTokens; i++) { String data = str.nextToken(); System.out.print(i+" 나눈 데이터 : "); System.out.println(data); } br.close(); fr.close(); bw.close(); fw.close(); } }----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.
매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.
각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com
감사합니다:)
감사합니다:)
댓글 달기