0

I'm trying to remove fist line from the CSV file after reading it. Code is working properly but it's adding "" to my data when it's rewriting. for an example:

  • before write to csv file: 100,COMRADE,CAMPBELL
  • after write to csv file: "100","COMRADE","CAMPBELL"

    Here is the code.

========================================================================

public void deleteLineFromCsv() throws IOException {
        List<PatientsTabData> list = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new FileReader(new File("src/main/resources/patients.csv")));
        String line = reader.readLine();
        while(line != null) {
            String[] split = line.split(",");
            list.add(new PatientsTabData().patientId(Integer.parseInt(split[0])).patName(split[1]).patLastName(split[2]));
            line = reader.readLine();
        }
        list.remove(0);
        CSVWriter writer = new CSVWriter(new FileWriter(new File("src/main/resources/patients.csv")));
        List<String[]> newlist = list.stream().map(item -> new String[]
                {String.valueOf(item.getPatientId()), item.getPatName(), item.getPatLastName()})
                .collect(Collectors.toList());
        writer.writeAll(newlist);
        writer.close();
    }

Library:

        <dependency>
            <groupId>net.sf.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>2.3</version>
        </dependency>
6
  • please add the library name of csv writer. Commented Mar 18, 2019 at 12:19
  • added to the question Commented Mar 18, 2019 at 12:20
  • If your requirement is really just that: removing the first line from a file, then I would wouldnt use a csv reader/writer here. Just read the file line by line with a normal buffered reader, and omit writing the first line. Commented Mar 18, 2019 at 12:54
  • @GhostCat Any example please? Commented Mar 18, 2019 at 12:56
  • Please step back. I really dont understand what exactly you intend to achieve. I mean: what is the actual purpose of your code? What exactly goes in and is expected to happen? What data needs to be in the file, and "in memory" after the method ran? Commented Mar 18, 2019 at 12:57

3 Answers 3

1

You can use another constructor when creating your writer. Change

CSVWriter writer = new CSVWriter(new FileWriter(new File("src/main/resources/patients.csv")));

to

CSVWriter writer = new CSVWriter(new FileWriter(new File("src/main/resources/patients.csv")),',',CSVWriter.NO_QUOTE_CHARACTER);
Sign up to request clarification or add additional context in comments.

Comments

0

this is the way to convert List to comma Seprated Single String.

List<String[]> oldList= . . . ;
String requiredString = oldList
.stream()
    .map(Arrays::asList)
    .flatMap(Collection::stream)
    .collect(Collectors.joining(","));

5 Comments

what about this part (item -> new String[] {String.valueOf(item.getPatientId()), item.getPatName(), item.getPatLastName()}) ?
@SanjX see my updated answer.. If you have List<String[]> then it converts in String comma seprated without "". perform above operation on your newList.
it won't work like that. Because I'm using write.writeAll(); writeAll (java.util.List<java.lang.String[]>) in CSVWriter cannot be applied to (java.lang.String)
@SanjX what is your title. Read again. I answered it according to your title.
then how this person understood it correct :) stackoverflow.com/a/55222066/7758011 never mind thanks!
0

Your CSV writer is writing each element as string in the csv file. This can be by design in library as it will keep any comma in your data in the string and later will not add extra column if your data has comma as well.

"name","class","house, street, city","value"

See the documentation of your library and check if it provides the option to not treat every element as string.

But my opinion is to keep the strings in quotes to avoid data descripency during reads.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.