【问题标题】:Appending file results in overwrite (Java)附加文件导致覆盖(Java)
【发布时间】:2012-04-03 20:25:56
【问题描述】:

所以我正在创建一个 CSV 文件,并且每次发生操作时我都想将数据写入文件。我遇到的问题是第二次输入时会覆盖数据。如何将数据添加到文件末尾?

 public boolean save_to_csv(){

    //check if directory exists, if not create the folder
    File folder = new File(Environment.getExternalStorageDirectory() + "/HKA_CAL");
    //Environment.getExternalStorageDirectory() get the location of external storage
    boolean success = true;
    if(!folder.exists())
    {
       success = folder.mkdir();
    }         
    if (success) 
    { 
        //success is true if folder has successfully been created
        //now we can create/check if the file exists

        File stored_hka = new File(Environment.getExternalStorageDirectory()+"/HKA_CAL/Stored_values.csv");
        boolean file_existed=true;

        try{
            if(!stored_hka.exists()){
                stored_hka.createNewFile();
                file_existed=false;
            }
            FileOutputStream fOut = new FileOutputStream(stored_hka);
            OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
            if(!file_existed){
                //if the file did not exist we need to write the titles of the csv
                myOutWriter.append("Calibration Tracking\r\n");
                myOutWriter.append(",ZERO1,,Zero2,,cal1,,cal2,,CALIBRATION FACTORS\r\n");
                myOutWriter.append("Date,Stab,Read,Stab,Read,Stab,Read,Stab,Read,Unit S/N,F Zero,F Offset,F Factor\r\n");
            }
            myOutWriter.append("Date"
                    +","+get_step3_stab()+","+get_step3_read()
                    +","+get_step6_stab()+","+get_step6_read()
                    +","+get_step8_stab()+","+get_step8_read()
                    +","+get_step11_stab()+","+get_step11_read()
                    +","+get_sn_num()+","+get_f_zero()
                    +","+get_f_offset()+","+get_f_factor()+"\r\n"
                    );
            myOutWriter.close();
            fOut.close();
        }
        catch(Exception e){
            return false;
        }




        return true;
    }
    else 
    {
        return false;
    }



}

【问题讨论】:

    标签: java file new-operator


    【解决方案1】:

    而不是做

    new FileOutputStream(stored_hka);
    

    做

    new FileOutputStream(stored_hka, true);
    

    这将以追加模式打开文件stored_hka,而不是覆盖内容。有关更多信息,请参阅FileOutputStream(String name, boolean append) 的 javadoc

    【讨论】:

    • @yawnobleix 它也“似乎”同意 Javadoc。
    【解决方案2】:

    当您构造 FileWriter 或 FileOutputStream 时,有一个构造函数参数允许您将其置于附加模式:

    new FileOutputStream( "/path/to/file", true )
    

    【讨论】:

      【解决方案3】:

      改变

      FileOutputStream fOut = new FileOutputStream(stored_hka);
      

      到

      FileOutputStream fOut = new FileOutputStream(stored_hka, true);
      

      【讨论】:

        【解决方案4】:

        使用包含用于追加的布尔值的 FileOutputStream constructor

        【讨论】:

          【解决方案5】:
          FileInputStream("valid path of file", true);
          

          它将以附加模式打开一个文件。布尔值表示是否要以附加模式打开文件。

          【讨论】:

            猜你喜欢
            • 2012-08-22
            • 1970-01-01
            • 2016-04-04
            • 1970-01-01
            • 1970-01-01
            • 2012-06-18
            • 2020-04-28
            • 1970-01-01
            • 2015-04-29
            相关资源
            最近更新 更多