简体   繁体   中英

How do I read a .csv file in Java with some cells containing multiple lines?

I am trying to read a .csv file in a Java program. The file has some cells which contain multiple lines.

I am on a linux OS, so I tried removing the line breaks with the following:

awk -v RS="" '{gsub (/\\n/,"")}1' cleanPaperAuthor.csv > cleanPaperAuthor1.csv

That DID result in the multi-line data in the cell being displayed all on one line. But when I attempted to read in the file in java, the reader still thought that it had encountered the end of the line in the middle of the cell data.

So I tried awk -v RS="" '{gsub (/\\r/,"")}1' cleanPaperAuthor1.csv > cleanPaperAuthor2.csv

That resulted in ALL data in the .csv file being put on one line.

So then I tried awk -v RS="" '{gsub (/\\r\\n/,"")}1' cleanPaperAuthor.csv > cleanPaperAuthor3.csv.

I'm not sure yet if that worked - I am still in the process of opening the file.

I know there is a CSVReader class out there, but I would really like to figure out what I can do without having to deal with getting that set up and changing my code. Anyone out there have any ideas? I'm completely befuddled at this point.

Using a CSV parser is extremely easy; both the setup and the API. And, in addition to handling the values that span multiple lines it can take care of things like commas in quoted elements and parsing just the values inside the quotes "" etc. for you. Plus, you can use the library to serialize your text back to CSV as well.

Here's an example with OpenCSV to read a line of csv values.

    String input = "value1, \"value2\", \"value3, 1234\", \"value4\n"
            + "value5\n"
            + "value6\"";

    try (CSVReader reader = new CSVReader(new StringReader(input))) {
        String [] tokens;
        while ((tokens = reader.readNext()) != null) {
            System.out.println(Arrays.toString(tokens));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

Output : ("value3, 1234" is one value.)

[value1, value2, value3, 1234, value4
value5
value6]

Just make sure to add Apache Commons Lang 3.x jar to your classpath .

        String UPLOADED_FOLDER = "/home/Rahul/Developement/Rahul/personal/uploadedfile/";
        try {

            // ** get the file and store at to that location **

            byte[] bytes = file.getBytes();
            Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
            Files.write(path, bytes);

            redirectAttributes.addFlashAttribute("You successfully uploaded '" + file.getOriginalFilename() + "'");

        } catch (IOException e) {
            e.printStackTrace();
        }

        try {

            String fileName = file.getOriginalFilename();

            System.out.println("/home/Rahul/Developement/Rahul/personal/uploadedfile/" + fileName);

            String filePath = new File("/home/Rahul/Developement/Rahul/personal/uploadedfile/")
                    .getAbsolutePath();

            boolean check = true;

            File file1 = new File("/home/Rahul/Developement/Rahul/personal/uploadedfile/" + fileName);

            System.out.println(file1.exists());

            // TO CHECK FILE IS CSV OR NOT
            if (fileName.endsWith(".csv")) {

                check = true;

                System.out.println("extension");

                if (!fileName.isEmpty()) {

                    // *** to read the file from the location
                    // **("/home/Rahul/Developement/Rahul/personal/uploadedfile/")**

                    BufferedReader br = new BufferedReader(new FileReader(
                            "/home/Rahul/Developement/Rahul/personal/uploadedfile/" + fileName));

                    InputStream is = new FileInputStream(
                            "/home/Rahul/Developement/Rahul/personal/uploadedfile/" + fileName);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM