Skip to main content
edited tags
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481
deleted 26 characters in body
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

Sorry it is so basice but hereHere is my class to read a csv file and I was wondering if there is any improvements that I should do. I feel there are some performance improvements or in exception handling because I just took IO exceptions.

package com.mypackage.utils;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.enums.CSVReaderNullFieldIndicator;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;

public class FileReader {

    private String path;
    private CSVReader reader;
    private List<String> linesList;
    private Set<List<String>> splitLinesSet;

    public FileReader(String path) {
        this.path = path;
    }

    public boolean readFile() {

        this.splitLinesSet = new HashSet<>();

        try {
            File file = Paths.get(path).toFile();
            reader = new CSVReaderBuilder(new java.io.FileReader(file))
                    .withCSVParser(new CSVParserBuilder()
                            .withSeparator('|')
                            .withFieldAsNull(CSVReaderNullFieldIndicator.NEITHER).build())
                    .build();
            read();
            reader.close();
        } catch (IOException e) {
            System.err.println("Error in reading CSV: " + e.getMessage());
            return false;
        }

        return true;
    }

    public Set<List<String>> getSplitLinesSet() {
        return splitLinesSet;
    }

    private void read() throws IOException {
        String[] lines;
        while ((lines = reader.readNext()) != null) {
            linesList = new ArrayList<>();
            Collections.addAll(linesList, lines);
            splitLinesSet.add(linesList);
        }
    }
}

Sorry it is so basice but here is my class to read a csv file and I was wondering if there is any improvements that I should do. I feel there are some performance improvements or in exception handling because I just took IO exceptions.

package com.mypackage.utils;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.enums.CSVReaderNullFieldIndicator;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;

public class FileReader {

    private String path;
    private CSVReader reader;
    private List<String> linesList;
    private Set<List<String>> splitLinesSet;

    public FileReader(String path) {
        this.path = path;
    }

    public boolean readFile() {

        this.splitLinesSet = new HashSet<>();

        try {
            File file = Paths.get(path).toFile();
            reader = new CSVReaderBuilder(new java.io.FileReader(file))
                    .withCSVParser(new CSVParserBuilder()
                            .withSeparator('|')
                            .withFieldAsNull(CSVReaderNullFieldIndicator.NEITHER).build())
                    .build();
            read();
            reader.close();
        } catch (IOException e) {
            System.err.println("Error in reading CSV: " + e.getMessage());
            return false;
        }

        return true;
    }

    public Set<List<String>> getSplitLinesSet() {
        return splitLinesSet;
    }

    private void read() throws IOException {
        String[] lines;
        while ((lines = reader.readNext()) != null) {
            linesList = new ArrayList<>();
            Collections.addAll(linesList, lines);
            splitLinesSet.add(linesList);
        }
    }
}

Here is my class to read a csv file and I was wondering if there is any improvements that I should do. I feel there are some performance improvements or in exception handling because I just took IO exceptions.

package com.mypackage.utils;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.enums.CSVReaderNullFieldIndicator;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;

public class FileReader {

    private String path;
    private CSVReader reader;
    private List<String> linesList;
    private Set<List<String>> splitLinesSet;

    public FileReader(String path) {
        this.path = path;
    }

    public boolean readFile() {

        this.splitLinesSet = new HashSet<>();

        try {
            File file = Paths.get(path).toFile();
            reader = new CSVReaderBuilder(new java.io.FileReader(file))
                    .withCSVParser(new CSVParserBuilder()
                            .withSeparator('|')
                            .withFieldAsNull(CSVReaderNullFieldIndicator.NEITHER).build())
                    .build();
            read();
            reader.close();
        } catch (IOException e) {
            System.err.println("Error in reading CSV: " + e.getMessage());
            return false;
        }

        return true;
    }

    public Set<List<String>> getSplitLinesSet() {
        return splitLinesSet;
    }

    private void read() throws IOException {
        String[] lines;
        while ((lines = reader.readNext()) != null) {
            linesList = new ArrayList<>();
            Collections.addAll(linesList, lines);
            splitLinesSet.add(linesList);
        }
    }
}
Source Link
PHA
  • 185
  • 1
  • 5

java class to read csv files

Sorry it is so basice but here is my class to read a csv file and I was wondering if there is any improvements that I should do. I feel there are some performance improvements or in exception handling because I just took IO exceptions.

package com.mypackage.utils;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.enums.CSVReaderNullFieldIndicator;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;

public class FileReader {

    private String path;
    private CSVReader reader;
    private List<String> linesList;
    private Set<List<String>> splitLinesSet;

    public FileReader(String path) {
        this.path = path;
    }

    public boolean readFile() {

        this.splitLinesSet = new HashSet<>();

        try {
            File file = Paths.get(path).toFile();
            reader = new CSVReaderBuilder(new java.io.FileReader(file))
                    .withCSVParser(new CSVParserBuilder()
                            .withSeparator('|')
                            .withFieldAsNull(CSVReaderNullFieldIndicator.NEITHER).build())
                    .build();
            read();
            reader.close();
        } catch (IOException e) {
            System.err.println("Error in reading CSV: " + e.getMessage());
            return false;
        }

        return true;
    }

    public Set<List<String>> getSplitLinesSet() {
        return splitLinesSet;
    }

    private void read() throws IOException {
        String[] lines;
        while ((lines = reader.readNext()) != null) {
            linesList = new ArrayList<>();
            Collections.addAll(linesList, lines);
            splitLinesSet.add(linesList);
        }
    }
}