2016-09-19 38 views
0

需要幫助寫入和接收文本文件 它似乎幾乎所有的方式,但它說,沒有文件存在,在這一點上,它應該創建一個,然後開始寫入它。它說它沒有找到一個,然後它就結束了。我不知道爲什麼都會失敗。問題與文本文件創建?

package sorting; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.Random; 

public class Sorting { 

    private static int[] oneToFiftyThou = new int[50000]; 
    private static int[] fiftyThouToOne = new int[50000]; 
    private static int[] randomFiftyThou = new int[50000]; 

    public static void main(String[] args) { 
     if(args.length>0) { 
      if(args[0].equalsIgnoreCase("init")) { 
       // initialize the 3 files 
       // 1-50000 file1 
       // 50000-1 file2 
       // random 50000 file3 
       initializeFiles(); 
       writeFiles(); 
      } 
     } else { 
      readFilestoArray(); 
      System.out.println(""+oneToFiftyThou[0] + " - " + 
          oneToFiftyThou[oneToFiftyThou.length-1]); 
      System.out.println(""+fiftyThouToOne[0] + " - " + 
          fiftyThouToOne[fiftyThouToOne.length-1]); 
      System.out.println(""+randomFiftyThou[0] + " - " + 
          randomFiftyThou[randomFiftyThou.length-1]); 
      intInsertionSort(oneToFiftyThou); 
      intInsertionSort(fiftyThouToOne); 
      intInsertionSort(randomFiftyThou); 
     } 

    } 

    private static void initializeFiles() { 
     //Array one 
     for(int i=1; i<oneToFiftyThou.length+1; i++) { 
      oneToFiftyThou[i-1] = i; 
     } 
     //Array two 
     for(int i=50000; i>0; i--) { 
      fiftyThouToOne[fiftyThouToOne.length-(i)] = i; 
     } 
     //Array Three Random. Copy Array one into a new Array and shuffle. 
     System.arraycopy(oneToFiftyThou, 0, randomFiftyThou, 0, 
           randomFiftyThou.length); 
     Random random = new Random(); 
     for(int i=randomFiftyThou.length-1; i>0; i--) { 
      int index = random.nextInt(i+1); 
      //Swap the values 
      int value = randomFiftyThou[index]; 
      randomFiftyThou[index] = randomFiftyThou[i]; 
      randomFiftyThou[i] = value; 
     } 
    } 

    public static void writeFiles() { 

     ArrayList<int[]> arrayList = new ArrayList<int[]>(); 
     arrayList.add(oneToFiftyThou); 
     arrayList.add(fiftyThouToOne); 
     arrayList.add(randomFiftyThou); 

     int fileIter = 1; 

     for(Iterator<int[]> iter = arrayList.iterator(); 
            iter.hasNext();) { 
      int[] array = iter.next(); 
      try { 
       File file = new File("file"+fileIter+".txt"); 

       //check for file, create it if it doesn't exist 
       if(!file.exists()) { 
        file.createNewFile(); 
       } 

       FileWriter fileWriter = new FileWriter(file); 
       BufferedWriter bufferWriter = new BufferedWriter 
                    (fileWriter); 
       for(int i = 0; i<array.length; i++) { 
        bufferWriter.write(""+array[i]); 
        if(i!=array.length-1) { 
         bufferWriter.newLine(); 
        } 
       } 
       bufferWriter.close(); 
       fileIter++; 
      }catch(IOException ioe) { 
       ioe.printStackTrace(); 
       System.exit(-1); 
      } 
     } 
    } 

    public static void readFilestoArray() { 
     ArrayList<int[]> arrayList = new ArrayList<int[]>(); 
     arrayList.add(oneToFiftyThou); 
     arrayList.add(fiftyThouToOne); 
     arrayList.add(randomFiftyThou); 

     int fileIter = 1; 

     for(Iterator<int[]> iter = arrayList.iterator(); 
            iter.hasNext();) { 
      int[] array = iter.next(); 
      try { 
       File file = new File("file"+fileIter+".txt"); 

       //check for file, exit with error if file doesn't exist 
       if(!file.exists()) { 
        System.out.println("file doesn't exist " 
                  + file.getName()); 
        System.exit(-1); 
       } 

       FileReader fileReader = new FileReader(file); 
       BufferedReader bufferReader = new BufferedReader 
                    (fileReader); 
       for(int i = 0; i<array.length; i++) { 
        array[i] = Integer.parseInt 
                (bufferReader.readLine()); 
       } 
       bufferReader.close(); 
       fileIter++; 
      }catch(IOException ioe) { 
       ioe.printStackTrace(); 
       System.exit(-1); 
      } 
     } 
    } 

    private static void intInsertionSort(int[] intArray) { 
     int comparisonCount = 0; 
     long startTime = System.currentTimeMillis(); 
     for(int i=1; i<intArray.length;i++) { 
      int tempValue = intArray[i]; 
      int j = 0; 
      for(j=i-1; j>=0 && tempValue<intArray[j];j--){ 
       comparisonCount++; 
       intArray[j+1] = intArray[j]; 
      } 
      intArray[j+1] = tempValue; 
     } 
     long endTime=System.currentTimeMillis(); 
     System.out.println("Comparison Count = " + comparisonCount 
          + " running time (in millis) = " + 
            (endTime-startTime)); 
    } 
} 
+0

您在'readFilestoArray'方法中的條件'if(!file.exists())'看起來可疑 –

+0

您到目前爲止解決此問題的嘗試有哪些? https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –

回答

0

好吧,適合我。執行它在控制檯這樣:

java Sorting init

然後執行另一次:

java Sorting

完美的作品。如果你在Eclipse中去運行配置>參數,並把init那裏。

如果有人用init參數調用該程序,那麼點在您的主要方法中,如果是,那麼您創建這些文件並寫入它們,如果沒有 - 您正在讀取它們。您可能在沒有init的情況下調用,但文件尚未存在,這就是爲什麼它不起作用。