2014-12-08 4 views
0

필자의 현재 프로그램이 파일로 저장되도록 요청되어 프로그램이 끝나더라도 나중에 계속할 수 있습니다. 그러나 나는 자바에 익숙하지 않다. 그래서 누군가가 나를 도울 수 있다면 정말 고마워 할 것이다. :) 이것은 내가 처리 할 수있는 코드이다. 그러나 단지 빈 파일을 생성한다.이 문제에 대해 어떻게 생각 하는가?파일 입출력

아래는 전체 프로그램 코딩이 아니며, 관련 코드를 복사 한 것입니다.

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.PrintWriter; 
import java.io.FileWriter; 


public static void main (String[] param) throws FileNotFoundException 
{ 
try 
    { 
    fileinputoutput(); 
    } 
    catch (FileNotFoundException e) 
    { 
    System.exit(0); 
    } 
} // END main 



public static void fileinputoutput() throws FileNotFoundException 
{ 

    File input = new File("input.txt"); 
    File output = new File("loveletter.txt"); 
    PrintWriter outputFile = new PrintWriter(output); 

    Scanner check = new Scanner(System.in); 

    String filename = check.nextLine(); 
    File inputfile = new File(filename); 

    Scanner newfile = new Scanner(inputfile); 
    newfile.close(); 

    while(newfile.hasNext()) 
    { 

    String write = newfile.nextLine(); 
    System.out.println(write); 

    } 

    outputFile.close(); 

} 
+0

나는이 문제를 이해하지 못한다. 명백하고 명확해야합니다. –

+2

'''System.out''' 만 출력하고 있다는 것을 알고 있습니까? – mezzodrinker

+0

@ flashdrive2049 답변을 올리면 투표 할 수 있습니다. –

답변

1

답변은 매우 분명합니다. 빈 파일 만 가져 오는 이유는 무엇이든 할 수 있기 전에 outputFile을 통해 아무 것도 쓰지 않고 newfile을 닫지 않기 때문입니다. 다음과 같이 시도해야합니다.

public static void fileinputoutput() throws FileNotFoundException { 
    File input = new File("input.txt"); // What do you use this variable for? It's never used in the code fragment you posted 
    File output = new File("loveletter.txt"); 
    PrintWriter outputFile = new FileWriter(output); // I'd personally use FileWriter here 
    Scanner check = new Scanner(System.in); 
    String filename = check.nextLine(); 
    File inputFile = new File(filename); 
    Scanner newfile = new Scanner(inputFile); 

    while(newfile.hasNext()) { 
     String write = newfile.nextLine(); 
     outputFile.println(write); // You used to only output something to the console here 
    } 

    outputFile.close(); 
    newfile.close(); 
    check.close(); 
}