如何读取3个csv文件并在控制台中按顺序显示?(JAVA)

9avjhtql  于 5个月前  发布在  Java
关注(0)|答案(1)|浏览(79)

我需要在java中读取3个csv文件。所以这一部分我已经完成了,但我想为下一部分优化我的代码。
我希望能够以与控制台中Excel文件相同的方式进行排序。
实际上我的最终目的是拿第一个文件,检查其中的问题,看看它们是否存在。然后,一旦我选择了问题,我将有两个文件看起来相同,所以我必须比较它们,看看是否有任何差异。
这就是为什么我想知道如何在开始时排序。
先谢了。

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();
            }
        }
}

字符串

u5i3ibmn

u5i3ibmn1#

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
            }
        }
    }
}

字符串

相关问题