【问题标题】:I cannot write to my .CSV file我无法写入我的 .CSV 文件
【发布时间】:2016-11-09 06:03:31
【问题描述】:

我的猜测是,我编写的代码不适用于 .CSV 文件,而只能用于 .txt。

我的代码的目的是从 field1 获取用户输入,并检查我的 .CSV 文件以查看文件中是否存在用户输入的实例。如果有,则将其替换为来自 field2 的用户输入。

这适用于我的 .txt 文件,但不适用于我的 .CSV 文件。

这是按下按钮(保存按钮)激活的代码:

try{
    // Input the file location into Path variable 'p'
    //Cannot write to CSV files
    //Path p = Paths.get("C:\\Users\\myname\\Documents\\Stock Take Program\\tiger.csv");

    Path p = Paths.get("C:\\Users\\myname\\Desktop\\test.txt");

    //Read the whole file to a ArrayList
    List<String> fileContent = new ArrayList<>(Files.readAllLines(p));

    //Converting user input from editSerialField to a string
    String strSerial = editSerialField.getText();
    //Converting user input from editLocationField to a string
    String strLocation = editLocationField.getText();

    //This structure looks for a duplicate in the text file, if so, replaces it with the user input from editLocationField.
    for (int i = 0; i < fileContent.size(); i++)
    {
        if (fileContent.get(i).equals(strSerial))
        {
            fileContent.set(i, strLocation);
        }
        break;
    }

    // write the new String with the replaced line OVER the same file
    Files.write(p, fileContent);

    }catch(IOException e)
    {
        e.printStackTrace();
    }

我的问题是,如何更新我的代码以使用用户输入更新和替换 .CSV 文件的内容,就像它适用于我的 .txt 文件一样。

写入文本文件时,仅替换第一行,但写入 .CSV 文件时,不替换任何内容。

我是否应该以不同的方式编写代码来替换 .CSV 文件中的文本。

非常感谢任何帮助,谢谢。

【问题讨论】:

  • 如果 csv 文件位于路径中没有空格的位置会怎样?
  • 这没什么区别。
  • 我对你的问题感到困惑,正如标题所说的as the file cannot be found
  • 它在错误输出中说,由于某种原因无法找到 .csv 文件。但最重要的是,我怀疑这是因为我无法使用我的代码读取/写入 .csv 文件。
  • 显示错误堆栈跟踪

标签: java file csv arraylist path


【解决方案1】:

我是个白痴。我的 '.CSV' 文件实际上以文本文件的形式命名为 tiger.csv。我现在保存了一个实际的 CSV 版本,它现在可以工作了。

哈哈,谢谢大家的帮助。

【讨论】:

  • 但是,它根本没有写入 .CSV 文件。我无法替换任何东西。我现在已经更新了我的问题以及随之而来的问题。
【解决方案2】:

可能应该在另一个问题中,但与仅在第一行起作用的更改有关的问题是由于break 被调用并在第一轮中缩短循环。将其放在if 块内。

for (int i = 0; i < fileContent.size(); i++)
{
    if (fileContent.get(i).equals(strSerial))
    {
        fileContent.set(i, strLocation);
        break;
    }
}

如果您希望它能够更新多行,或者完全关闭它。

【讨论】:

    【解决方案3】:

    HTH,

    public static void main(String[] args) throws FileNotFoundException {
    
                String UserEntredValu="Karnataka";
                String csvFile = "C:/Users/GOOGLE/Desktop/sample/temp.csv";
                String line = "";
                String cvsSplitBy = ",";
                PrintWriter pw = null;
                pw = new PrintWriter(new File(csvFile));
                try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
    
                    while ((line = br.readLine()) != null) {
    
                        // use comma as separator
                        String[] country = line.split(cvsSplitBy);
    
                       for( int i = 0; i < country.length - 1; i++)
                       {
                           String element = country[i];
    
                           if(element.contains(UserEntredValu)){
                               String newEle=element.replace(element, "NEW INDIA");
                               pw.write(newEle);
                               System.out.println("done!");
                               pw.close();
                           }
                       }
                 }
    
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多