我需要在Java中读取3个CSV文件. 所以这一部分我已经做到了,但我想为下一部分优化我的代码.

我希望能够以与控制台中的Excel文件相同的方式进行排序.

事实上,我的最终目标是拿第一个文件,判断其中的问题,看看它们是否存在.然后,一旦我 Select 了问题,我将有两个看起来相同的文件,所以我必须比较它们,看看是否有任何差异.

这就是为什么我想知道如何在开始时进行排序.

先谢谢你.

package readAndPrint;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class read3file {

    public static void main(String[] args) {
        // Définir les fichiers et leur emplacements
        String sPathDirectory ="C:\\Users\\...\\Downloads\\";
        String pathFileQR = sPathDirectory + "QR.csv";
        String pathFilePivot = sPathDirectory + "PivotSegmentQuestion.csv";
        String pathFileNCL = sPathDirectory + "PivotSegmentQuestionNCL.csv";
        
        
        //Lire et afficher QR.csv
        readAndPrintFile(pathFileQR);
        
        //Lire et afficher PivotSegmentQuestion.csv
        readAndPrintFile(pathFilePivot);
        
        //Lire et afficher PivotSegmentQuestionNCL.csv
        readAndPrintFile(pathFileNCL);
        }
    
        //Methode pour lire les fichiers et les afficher
        private static void readAndPrintFile(String filePath) {
            String line = "";
            
            try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
                while ((line = br.readLine()) !=null)
                    System.out.println(line);
                
                System.out.println("Fichier trouvé: " + filePath); //Confirmation 
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.out.println("Fichier introuvable :" + filePath); //Affichage message erreur
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

推荐答案

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadCSVFiles {

    public static void main(String[] args) {
        // Specify the paths to your CSV files
        String[] filePaths = {"file1.csv", "file2.csv", "file3.csv"};

        try {
            // Loop through each file
            for (String filePath : filePaths) {
                System.out.println("Reading file: " + filePath);
                readAndDisplayCSV(filePath);
                System.out.println(); // Add a newline between files
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void readAndDisplayCSV(String filePath) throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // Split the CSV values and display them
                String[] values = line.split(",");
                for (String value : values) {
                    System.out.print(value + "\t");
                }
                System.out.println(); // Move to the next line
            }
        }
    }
}

Java相关问答推荐

将偶数元素移动到数组的前面,同时保持相对顺序

Mat. n_Delete()和Mat. n_release的区别

Oracle DUAL表上使用DDL时jOOQ问题的解析'

我无法将附件发送到NetBeans之外

基本时态运算的ISO-8601周数据表示法

在Java中,如何按一个属性升序,然后按另一个属性降序对对象列表进行排序?

基于调车场算法的科学计算器

无法了解Java线程所消耗的时间

从Spring5迁移到Spring6:无法在雅加达包中找到类

Java流传输一个列表并创建单个对象

Com.google.firebase.database.DatabaseException:无法将类型为java.lang.Boolean的值转换为字符串.这是关于什么的?

有没有更快的方法在N个容器中删除重复项?

如何在不删除Java中已有内容的情况下覆盖文件内容?

使用Jackson库反序列化json

我可以在@Cacheable中使用枚举吗

将stringBuilder + forloop转换为stream + map

在Java中使用StorageReference将数据从Firebase存储添加到数组列表

为什么当我输入变量而不是直接输入字符串时,我的方法不起作用?

Swagger.io OpenApi v3.0 声明默认媒体类型

Xml Reader 将 BMP 外部的字符解析为代理项对,这会导致无效的 xml