将布尔值设置为 true,但文件写入器仍会覆盖文件,有什么建议吗?

我创建了一个简单的程序,它使用不同的算法对输入文件中的整数进行排序。我还使用文件写入器将结果输出到另一个文件。不幸的是,无论我如何更改代码,文件都会被覆盖。有什么建议吗?


一直在谷歌上寻找答案,并尝试改变我输入语法的方式,但没有任何效果。


重要的位:


设置作家


try {


        FileWriter fileWriter = new FileWriter ("Sorted output.txt");


        //BufferedWriter bufferedWriter = new BufferedWriter (fileWriter);


        PrintWriter out = new PrintWriter (new FileWriter("Sorted output.txt", true));

输出到文件


 out.println("User's own data set sorted using bubble sort.");

                      out.println(unsortedArray + Arrays.deepToString(FileOne));

                      out.println("Sorted Array looks like this:" + Arrays.toString(intArrayBubble));

                      out.println(timeToSort + bubbleSortIs + bubbleTime + "ms");

它工作正常,但是它在 do while 循环中使用,带有嵌套的 if 语句,并且每个语句都覆盖另一个。


其余代码以防万一需要 - 更新 - 仍然无法正常工作


import java.io.*;

import java.util.*;

import java.util.concurrent.TimeUnit;


public class PDD_Sorting {


    public static void main (String [] pArgs) 

    {


        //Array for a file

        String[] FileOne;

        FileOne = new String[0];




        int optionOne = 1,

            optionTwo = 2,

            optionThree = 3,

            secondaryOptionOne = 1,

            secondaryOptionTwo = 2,

            secondaryOptionThree = 3,

            userSelection,

            subUserSelection;


        String     unsortedArray = "Unsorted array is: ",

                   bubbleSort = "Sorted array using bubble sort: ",

                   selectionSort = "Sorted array using selection sort: ",

                   insertionSort = "Sorted array using insertion sort: ",

                   timeToSort = "Time needed to sort this array using ",

                   bubbleSortIs = "bubble sort is ",

                   selectionSortIs = "selection sort is ",

                   insertionSortIs = "insertion sort is ",


文件不断被覆盖,我该如何阻止它并将其添加到文件中?



蛊毒传说
浏览 64回答 2
2回答

拉莫斯之舞

你不需要第一个FileWriter fileWriter = new FileWriter("Sorted output.txt");;这实际上是创建/覆盖文件,之后您的 PrintWriter 再次打开它以进行附加。所以,只要改变// ... omitting beginningtry {    FileWriter fileWriter = new FileWriter ("Sorted output.txt");    //BufferedWriter bufferedWriter = new BufferedWriter (fileWriter);    PrintWriter out = new PrintWriter (new FileWriter("Sorted output.txt", true));    do { // ... omitting rest至// ... omitting beginningtry {    //BufferedWriter bufferedWriter = new BufferedWriter (fileWriter);    PrintWriter out = new PrintWriter (new FileWriter("Sorted output.txt", true));    do { // ... omitting rest

江户川乱折腾

将 out.close() 移出循环&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Please enter a valid option i.e. 1,2 or 3");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tInput.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.flush();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* THIS&nbsp; -> out.close(); <- THIS */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //tInput.close();&nbsp; &nbsp; &nbsp; &nbsp; }while (userSelection != optionThree);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;out.close();&nbsp; &nbsp; &nbsp; &nbsp; }我试过你的代码,你的问题不是文件被覆盖,而是你在第一次迭代中关闭了输出流。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java