读取,更新然后在 java 中创建另一个 csv 文件

卡尔文·阿迪维纳塔

如何更新文件然后用更新的数据创建一个新的 csv 文件?

我已经阅读了 csv 文件

public static void main(String[] args) throws FileNotFoundException {
    // TODO code application logic here
    String fileName ="lain.csv";
    File file=new File(fileName);
    Scanner input = new Scanner(file);
    
    while(input.hasNext()){
        String data = input.nextLine();
        System.out.println(data);
    }
    input.close();
}

这是示例 csv 数据

对象名称

tif

B.tif

tif

D.tif

我想在每一行中添加一个注释,用“|” 在数据和评论之间

例如

A.tif | 这是一个

我应该用什么来做这样的?

德哈尔

如果你想创建一个新文件并在那里有相同的行和注释,你可以这样做(java.nio用于文件系统访问和 java 8 迭代):

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        // the path to your source file as a String
        String fileLocation = "Y:\\our\\path\\to\\lain.csv";
        // the path to your source file as a Path object (java.nio)
        Path filePath = Paths.get(fileLocation);
        // a list of Strings for the new lines, those with a comment separated by |
        List<String> updatedLines = new ArrayList<String>();

        try {
            // read all the lines of the csv source file into a list
            List<String> lines = Files.readAllLines(filePath, StandardCharsets.ISO_8859_1);

            System.out.println("————————————————————————————————————————————————————————————————");

            // print each line just to see if everything is properly read (the java 8 way)
            lines.forEach(line -> {
                System.out.println(line);
            });

            System.out.println("————————————————————————————————————————————————————————————————");

            // add a comment to each line and store it in the updatedLines 
            lines.forEach(line -> {
                /*
                 * TODO add some line-depending comment creation logic here,
                 * this just adds "a comment" to every line
                 */
                updatedLines.add(line + "|" + "a comment");
            });

            // print the updated lines
            updatedLines.forEach(updatedLine -> {
                System.out.println(updatedLine);
            });

            System.out.println("————————————————————————————————————————————————————————————————");

            // create a new file
            String updatedFileLocation = "Y:\\our\\path\\to\\lain_updated.csv";
            Path updatedFilePath = Paths.get(updatedFileLocation);
            Files.createFile(updatedFilePath);

            // write the updated lines to a new csv file
            Files.write(updatedFilePath, updatedLines,
                    StandardOpenOption.TRUNCATE_EXISTING);

            // final check: read the new file and print its content:
            Files.readAllLines(updatedFilePath).forEach(writtenUpdatedLine -> {
                System.out.println(writtenUpdatedLine);
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在一个jsp文件中创建Java函数,然后从另一个jsp文件中调用它

从csv文件读取特定的列,然后使用python写入另一个CSV

在CSV文件中应用diff()函数,然后导出到另一个CSV文件

读取csv文件,并仅在另一个csv文件中添加新条目

从一个csv读取数据并创建另一个已修改的csv文件

使用 spark 中的另一个 csv 文件更新 csv 文件

仅将一个 CSV 文件中的两列更新到另一个 CSV 文件

在另一个 csv 文件中搜索一个 CSV 文件并更新第一个 csv 中的列值

根据来自另一个文件的zip合并更新csv文件中的城市

在csv文件中搜索列词,并用另一个值java替换它

从 Jupiter Notebook (python) 中的另一个文件夹读取 CSV

从python中的另一个文件夹读取几个csv

如何从csv文件/ Oracle中的另一个表更新大表

使用另一个 csv 文件中的更新信息更新现有的 csv 文件

Java从另一个jar读取json文件

从一个Java文件读取并写入另一个Java文件

通过删除PERL中的某些列,从另一个CSV文件创建一个CSV文件

从一个读取并写入另一个 csv 文件 - PHP

从另一个创建CSV文件

AWS Lambda:如何在S3存储桶中读取CSV文件,然后将其上传到另一个S3存储桶?

将所有.csv文件从Google存储桶中读取到一个大熊猫df中,然后另存为.csv到另一个存储桶中

读取 .csv 的内容并使用 python 将其写入另一个 .csv 文件

比较两个大的csv文件,然后用python编写另一个

Python:读取CSV文件并写入另一个文本文件

更新程序以在 python 中创建一个 CSV 文件

从文件中读取一列,然后附加另一个文件的特定列

如何使用特定的输出格式将矩阵(从一个文件读取)写入python中的另一个csv文件

如何使用熊猫从csv读取行,执行VLOOKUP操作并将结果保存到另一个文件中?

如何遍历 csv 文件并根据另一个文件的值更新值