在删除Java中的列后,如何更换CSV文件

发布于 2025-02-13 23:00:44 字数 362 浏览 3 评论 0原文

我是Java的新手。我成功地从本地文件位置读取了我的CSV文件,并且能够确定为我的要求删除哪个列。但是,我无法删除所需的列,并将文件写入我的本地文件夹。有没有办法解决此问题?我使用以下代码:

    CSVReader reader = new CSVReader(new FileReader(fileName));
    String [] nextLine;
    while ((nextLine = reader.readNext()) != null) {
        System.out.println(nextLine[15]);
    }

我要做的就是删除具有索引15的列,并将文件写入我的本地文件夹中的CSV文件。

I am new to Java. I was successfully able to read my CSV file from my local file location and was able to identify which column needed to be deleted for my requirements. However, I was not able to delete the required column and write the file into my local folder. Is there a way to resolve this issue? I have used the following code:

    CSVReader reader = new CSVReader(new FileReader(fileName));
    String [] nextLine;
    while ((nextLine = reader.readNext()) != null) {
        System.out.println(nextLine[15]);
    }

All I would like to do is to remove the column having index 15 and write the file as a CSV file in my local folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

長街聽風 2025-02-20 23:00:44

我假设您正在使用OpenCSV库。

为了使您的代码工作,您必须解决2个问题:

  • 您需要一个作者将修改后的CSV写入。 OpenCSV为此提供了CSVWriter类。
  • 您需要将行(当前是字符串数组)转换为列表以删除元素,然后将其转换回数组以匹配csvwriter.writeenext方法所期望的内容。

这是一些执行此操作的代码:

CSVReader reader = new CSVReader(new FileReader(fileName));
CSVWriter writer = new CSVWriter(new FileWriter(outFileName));
String[] origLine;
while ((origLine = reader.readNext()) != null) {
    List<String> lineList = new ArrayList<>(Arrays.asList(origLine));
    lineList.remove(15);
    String[] newLine = lineList.toArray(new String[lineList.size()]);
    writer.writeNext(newLine, true);
}
writer.close();
reader.close();

一些其他说明:

  • 如果要以生产能力使用,则代码可能需要更多的错误处理等。
  • 列表java中的索引从0开始,因此删除[15]实际上从文件中删除了第16列。
  • 该代码将其输出写入单独的文件。尝试使用相同的文件名进行输入和输出将不起作用

I'm assuming you're using the OpenCSV library.

In order to make your code work, you have to fix 2 issues:

  • You need a writer to write your modified CSV to. OpenCSV provides a CSVWriter class for this purpose.
  • You need to convert your line (which is currently a String array) into a list to be able to remove an element, then convert it back into an array to match what the CSVWriter.writeNext method expects.

Here's some code that does this:

CSVReader reader = new CSVReader(new FileReader(fileName));
CSVWriter writer = new CSVWriter(new FileWriter(outFileName));
String[] origLine;
while ((origLine = reader.readNext()) != null) {
    List<String> lineList = new ArrayList<>(Arrays.asList(origLine));
    lineList.remove(15);
    String[] newLine = lineList.toArray(new String[lineList.size()]);
    writer.writeNext(newLine, true);
}
writer.close();
reader.close();

Some additional remarks:

  • The code probably needs a bit more error handling etc if it's to be used in a production capacity.
  • List indices in Java start at 0, so remove[15] actually removes the 16th column from the file.
  • The code writes its output to a separate file. Trying to use the same file name for input and output will not work.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文