簡體   English   中英

BufferedWriter 不逐行寫入數據

[英]BufferedWriter not writing data line by line

目前正在嘗試從顯示基准測試結果的 System.out.println() 語句編輯我的舊代碼,以將結果寫入 2 個不同的 .txt 文件。 我這樣做是為了創建一個新程序,它使用 JFileChooser 到 select 2.txt 文件之一,並在 JTable 上顯示數據。問題是 BufferedWriter 只將最后一組數據值寫入 10 倍以上,所以我不能在我的 JTable 中顯示正確的結果。

基准排序.java:

/**
 * 
 * BenchmarkSorts is going to generate arrays consisting of random integers the size of which is supplied from SortMain.java.
 * It will then send the randomly generated array to be sorted using the Quick Sort Algorithm both Iteratively and Recursively
 * a total of 50 times each and display the benchmark results to the user.
 */

//=========
//packages
//=========
import java.io.*;
import java.util.Random;

//==============================
//start of class BenchmarkSorts
//==============================
public class BenchmarkSorts {

    //==========
    //variables
    //==========
    private int[] unsortedArray;
    private int iterativeCount = 0;
    private int recursiveCount = 0;
    private long iterativeTime, recursiveTime;
    private int[] iterativeCountLog = new int[50];
    private int[] recursiveCountLog = new int[50];
    private int iCounter = 0;
    private int rCounter = 0;
    private long[] iterativeTimeLog = new long[50];
    private long[] recursiveTimeLog = new long[50];
    private int arraySize;
    
    private FileWriter iFileWriter = new FileWriter("iterativeResults.txt");
    private FileWriter rFileWriter = new FileWriter("recursiveResults.txt");
    
    private BufferedWriter iBuffer = new BufferedWriter(iFileWriter);
    private BufferedWriter rBuffer = new BufferedWriter(rFileWriter);

    //=========
    //invoking
    //=========
    private QuickSort quickSort = new QuickSort();

    //============
    //constructor
    //============
    public BenchmarkSorts(int[] sizes) throws Exception {

        //==========================================================
        //for loop to add the sizes of the arrays to be benchmarked
        //==========================================================
        for(int i = 0; i < sizes.length; i++){
            arraySize = sizes[i];
            @SuppressWarnings("unused")
            BenchmarkSorts bms = new BenchmarkSorts(arraySize);        
        }
    }

    //========================================================
    //method to add random numbers to the array index size,
    //then send them to be sorted iteratively and recursively
    //before being displayed with the benchmark results
    //========================================================
    private BenchmarkSorts(int n) throws Exception {

        //=================================
        //nested for loops to run 50 times
        //creating arrays
        //=================================
        for(int i = 0; i < 50; i++) {
            unsortedArray = new int[n];

            for(int j = 0; j < n; j++) {
                Random randNum = new Random();
                unsortedArray[j] = (randNum.nextInt(1000));
            }
            runSorts();
        }
        displayReport(n);
    }

    //=========================================================
    //method to sort unsortedArray iteratively and recursively
    //while keeping track of count, time(in nano seconds)
    //=========================================================
    public void runSorts() throws Exception {

        //=========================================
        //creating 2 temporary arrays to be sorted
        //=========================================
        int[] tempArray1 = unsortedArray;
        int[] tempArray2 = unsortedArray;

        //============================
        //iterative sort and trackers
        //============================
        quickSort.iterativeSort(tempArray1);
        int returnCount = quickSort.getCount();
        long returnTime = quickSort.getTime();
        iterativeCount = iterativeCount + returnCount;
        iterativeTime = iterativeTime + returnTime;
        iterativeCountLog[iCounter] = returnCount;
        iterativeTimeLog[iCounter] = returnTime;
        iCounter++;

        //============================
        //recursive sort and trackers
        //============================
        quickSort.recursiveSort(tempArray2);
        returnCount = quickSort.getCount();
        returnTime = quickSort.getTime();
        recursiveCount = recursiveCount + returnCount;
        recursiveTime = recursiveTime + returnTime;
        recursiveCountLog[rCounter] = recursiveCount;
        recursiveTimeLog[rCounter] = recursiveTime;
        rCounter++;
    }

    //===================================================
    //method to display averages and standard deviations
    //===================================================
    public void displayReport(int arraySize) throws IOException {   

        //==========
        //variables
        //==========
        double iterativeAverageCount = 0;
        double iterativeAverageTime = 0;
        double recursiveAverageCount = 0;
        double recursiveAverageTime = 0;
        double iterativeSDCount = 0;
        double iterativeSDTime = 0;
        double recursiveSDCount = 0;
        double recursiveSDTime = 0;

        //========================================================
        //averaging the trackers for both iterative and recursive
        //========================================================
        iterativeAverageCount = iterativeCount / 50;
        iterativeAverageTime = iterativeTime / 50;
        recursiveAverageCount = recursiveCount / 50;
        recursiveAverageTime = recursiveTime / 50;

        //==========================================
        //for loop to calculate standard deviations
        //==========================================
        for(int i = 0; i < 50; i++) {
            iterativeSDCount = iterativeSDCount + Math.pow((iterativeCountLog[i] - iterativeAverageCount), 2);
            iterativeSDTime = iterativeSDTime + Math.pow((iterativeTimeLog[i] - iterativeAverageTime), 2);
            recursiveSDCount = recursiveSDCount + Math.pow((recursiveCountLog[i] - recursiveAverageCount), 2);
            recursiveSDTime = recursiveSDTime + Math.pow((recursiveTimeLog[i] - recursiveAverageTime), 2);
        }

        //====================
        //standard deviations
        //====================
        iterativeSDCount = Math.pow(iterativeSDCount, .5) / arraySize;
        iterativeSDTime = Math.pow(iterativeSDTime, .5) / arraySize;
        recursiveSDCount = Math.pow(recursiveSDCount, .5) / arraySize;
        recursiveSDTime = Math.pow(recursiveSDTime, .5) / arraySize;

        //====================================================
        //for loops to BufferedWrite data to respective files
        //====================================================
        for (int i = 0; i < 10; i++)    {
            iBuffer.write(arraySize + " " + iterativeAverageCount +  " " + iterativeSDCount + " " + iterativeAverageTime + " " + iterativeSDTime);
            iBuffer.newLine();
        }
        for (int i = 0; i < 10; i++)    {
            rBuffer.write(arraySize + " " + recursiveAverageCount +  " " + recursiveSDCount + " " + recursiveAverageTime + " " + recursiveSDTime);
            rBuffer.newLine();
        }
        iBuffer.close();
        rBuffer.close();
        
        //==========================================
        //old output that I want to write to a file
        //==========================================
        System.out.println("Iterative Quick Sort Results: " + "\nData Set Size (n): " + arraySize +
                ", Average Critical Operation Count: " + iterativeAverageCount + ", Standard Deviation of Count: " +
                iterativeSDCount + ", Average Execution Time: " + iterativeAverageTime + ", Standard Deviation of Time: " +
                iterativeSDTime);

        System.out.println("Recursive Quick Sort Results: " + "\nData Set Size (n): " + arraySize +
                ", Average Critical Operation Count: " + recursiveAverageCount + ", Standard Deviation of Count: " +
                recursiveSDCount + ", Average Execution Time: " + recursiveAverageTime + ", Standard Deviation of Time: " +
                recursiveSDTime);
    }
}

這是舊的 output 使用 System.out.println() 語句:

Iterative Quick Sort Results: 
Data Set Size (n): 100, Average Critical Operation Count: 66.0, Standard Deviation of Count: 0.13490737563232041, Average Execution Time: 11528.0, Standard Deviation of Time: 394.80891580611495
Recursive Quick Sort Results: 
Data Set Size (n): 100, Average Critical Operation Count: 99.0, Standard Deviation of Count: 199.0490957025427, Average Execution Time: 36124.0, Standard Deviation of Time: 78349.10253729777
Iterative Quick Sort Results: 
Data Set Size (n): 200, Average Critical Operation Count: 133.0, Standard Deviation of Count: 0.11224972160321825, Average Execution Time: 27364.0, Standard Deviation of Time: 544.5295033329232
Recursive Quick Sort Results: 
Data Set Size (n): 200, Average Critical Operation Count: 199.0, Standard Deviation of Count: 200.05439416568686, Average Execution Time: 29262.0, Standard Deviation of Time: 27784.950723818103
...
...
Iterative Quick Sort Results: 
Data Set Size (n): 12000, Average Critical Operation Count: 11000.0, Standard Deviation of Count: 2.6352313834736497E-4, Average Execution Time: 720028.0, Standard Deviation of Time: 69.97877063001957
Recursive Quick Sort Results: 
Data Set Size (n): 12000, Average Critical Operation Count: 11999.0, Standard Deviation of Count: 201.04293765444527, Average Execution Time: 6.034408E7, Standard Deviation of Time: 1011471.3254720564

如您所見,它為 [100...12000] 中的所有 arraySize 生成了正確的輸出(我切掉了中間部分以縮短總長度)。

生成的 2.txt 文件只顯示最后一個 arraySize [12000] 10x,我不明白為什么

迭代結果.txt:

12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957

遞歸結果.txt:

12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564

它看起來通常很困惑。

//========================================================
//method to add random numbers to the array index size,
//then send them to be sorted iteratively and recursively
//before being displayed with the benchmark results
//========================================================
private BenchmarkSorts(int n)  

那是一個構造函數,而不是一個方法,所以每次使用它都會構造 (,) 一個新的 BenchmarkSorts 實例。 每個都有自己的一組實例變量,而且您顯然知道它是一個構造函數,因為要使用它您編寫了new BenchmarkSorts(...) - 這意味着使用第一個構造函數創建一個 object 最終會創建許多使用第二個構造函數,沒有一個實例被真正使用。 回想起來,通過抑制它來“修復”未使用變量警告可能不是一個好主意。

這可能是您的錯誤的來源。 每個實例都會創建新的 BufferedWriters,每個 BufferedWriters 都會覆蓋同一個文件。

還,

int[] tempArray1 = unsortedArray;
int[] tempArray2 = unsortedArray;

你仍然只有一個數組。

您的第一個解決方法應該是將第二個構造函數變成一個方法。 然后擔心你是否真的想要 1 個數組的 2 個副本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM