내가 예를 들어, 그래서 라인으로 입력 파일 라인을 읽고 다음 출력 파일을 인쇄하려면이 프로그램을 얻기 위해 노력하고 있어요 :입력 및 출력 프로그램
입력 파일이 포함되어
cookies
cake
ice cream
행 1 : 쿠키
2 호선 : I 출력 파일이를 표시 할 케이크를
3 호선 : 아이스크림
어떻게해야할지 모르겠으므로 도움을 얻을 수 있습니다.
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
System.out.print("Enter the input file: ");
String name = in.next();
FileReader file = new FileReader(name);
BufferedReader reader = new BufferedReader(file);
String text = "";
String line = reader.readLine();
while(line != null){
text += line;
line = reader.readLine();
}
reader.close();
System.out.print("Enter the output file: ");
String out = in.next();
FileWriter filew = new FileWriter(out);
BufferedWriter buffw = new BufferedWriter(filew);
buffw.write(text);
buffw.close();
System.out.print("File written!");
in.close();
}
}
코드의 문제점은 무엇인가 :
이내가 좋아하는
StringBuilder
대신 문자열 연결을 사용하여 당신을 제안? – Jens현재로서는 출력을 한 줄로 인쇄하고 언급 한 것처럼 여러 줄로 표시하고 싶습니다. – bacon9
당신은 각 행을 읽고 새로운 줄을 추가하지 않고 문자열로 concating하고 있습니다. 그래서 그것은 합법적 인 것처럼 보입니다. 'readLine'은 줄 바꿈 문자를 소비합니다. – AxelH