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

标签 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 数据示例

object_name

A.tif

B.tif

C.tif

D.tif

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

例如

A.tif | this is A

我应该用什么来制作这样的东西?

最佳答案

如果您想创建一个新文件并在其中包含相同的行和注释,您可以这样做(使用 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();
        }
    }
}

关于java - 读取、更新然后在 java 中创建另一个 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52942015/

相关文章:

java - Glaledlists:如何创建一个包含原始列表项+关联的派生项的转换列表

C++ 从 CSV 文件中读取一列数据

java - 如何在 PostgreSQL 中出现第一个空格时拆分字符串

java - 从 jsp 页面访问 javabean

java - 文件存在,但在 toast 中它是空的

postgresql - 在 postgis 中导入包含多个几何图形的 csv 文件

javascript - meteor 读数 csv 文件 Papa Parse

java - JScrollBar 中“工作”区域的大小

python - 将多个相似的 CSV 文件连接到一个大数据框中

date - 将 csv 中的列转换为日期 powershell