Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I read 3 csv files and display them in order in the console? (JAVA)

I need to read 3 csv files in java.
So this part I’ve managed but I’d like to optimise my code for the next part.

I’d like to be able to sort in the same way as in the Excel file in the console.

In fact my final objective is to take the first file, check the questions in it to see if they exist. Then, once I’ve chosen the question, I’ll have 2 files that look the same, so I’ll have to compare them and see if there are any discrepancies.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

That’s why I’d like to know how to sort at the start.

Thanks in advance.

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

>Solution :

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
            }
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading