2017-03-29 3 views
0

나는 나의 수업 중 하나를 위해 짧은 프로그램을 썼고, 나는 그걸 알아낼 수없는 뭔가가있다. 그래서 나는 파일에 100 개의 무작위 정수를 쓰고, 데이터를 다시 읽고, 정수를 증가 순서대로 인쇄하기로되어 있습니다. 모든 것이 정상적으로 작동하지만 출력 파일에 내 최종 정렬 목록이 표시되지 않습니다. 누구나 볼 수 있습니까?파일의 입/출력

여기 내 코드입니다 :

private final static int NUMBEROFRANDOM = 100; 

public static void main(String[] args) throws IOException { 

    // Creating my file 
    java.io.File file = new java.io.File("Question1.txt"); 

    // If it already exists, print a message and terminate program 
    if (file.exists()) { 
     System.out.println("File already exists."); 
     System.exit(0); 
    } 

    // Creating my PrintWriter object 
    PrintWriter output = new PrintWriter(file); 

    // Creating 100 random numbers between 0 and 100 and printing them on the file 
    for (int i = 0; i < NUMBEROFRANDOM; i++) { 
     int number = (int) (Math.random() * 101); 
     output.print(number + " "); 
    } 

    // Creating my Scanner object 
    Scanner input = new Scanner(file); 

    // Creating my array list to store the sorted list of 100 elements 
    ArrayList<Integer> sortedList = new ArrayList<Integer>(); 

    // Reading the elements from the file and adding them into my array list 
    while (input.hasNext()) { 
     sortedList.add(input.nextInt()); 
    } 

    //Sorting elements from array list 
    Collections.sort(sortedList); 

    // Printing the elements in increasing order 
    for (int i = 0; i < sortedList.size(); i++) { 
     //System.out.println(sortedList.get(i)); 
     output.print(sortedList.get(i)); 
    } 

    // Closing my objects 
    input.close(); 
    output.close(); 

} 

가 대단히 감사합니다, 어떤 도움은 매우 감사합니다!

+1

숫자를 만든 후에 출력을 닫아야합니다. – notyou

+0

누군가가 나를 때리는 것을 본다 - 내 컴퓨터가 너무 오랫동안 내 IDE를 열어 보냈다. :) – notyou

답변

0

출력 스트림을 닫아야 파일에 쓸 수 있습니다. 이 작업은 두 번째로는 올바르게 수행하지만 처음에는 수행하지 않습니다.

추가 :

output.close(); 

후 :

// Creating 100 random numbers between 0 and 100 and printing them on the file 
for (int i = 0; i < NUMBEROFRANDOM; i++) { 
    int number = (int) (Math.random() * 101); 
    output.print(number + " "); 
} 
//Here close the first output stream to get the data to the file. 
output.close(); 
1

귀하의 파일은 값을 인쇄 한 후 데이터를 저장하는 Scanner input = new Scanner(file);

사용 output.flush()를 사용하여 읽을 때 비어 있습니다.

output.close() - 스트림이 닫히고 정렬 된 값을 저장할 수 없습니다.

// Creating 100 random numbers between 0 and 100 and printing them on the file 
for (int i = 0; i < NUMBEROFRANDOM; i++) { 
    int number = (int) (Math.random() * 101); 
    output.print(number + " "); 
} 

output.flush(); 
0

코드의 아래 라인을하기 전에, 당신은 당신의 파일에 내용을 작성 close 또는 flush해야합니다.

output.close(); <--- you need to close here 
Scanner input = new Scanner(file); 

위의 코드 줄까지 파일에 아무 것도 기록되지 않았으므로. 따라서 코드 아래가 쓸모 없게됩니다.

while (input.hasNext()) { // nothing to read 
     sortedList.add(input.nextInt()); //nothing added to List 
    } 

    //Sorting elements from array list 
    Collections.sort(sortedList); //nothing to sort 

    // Printing the elements in increasing order 
    for (int i = 0; i < sortedList.size(); i++) { 

     output.print(sortedList.get(i)); // won't execute this line 
    }