Search in sources :

Example 31 with CSVReader

use of com.opencsv.CSVReader in project chuidiang-ejemplos by chuidiang.

the class MainWithOpenCsv method main.

public static void main(String[] args) {
    CSVReader reader = null;
    try {
        reader = new CSVReader(new FileReader("files/Libro2.csv"), SEPARATOR, QUOTE);
        String[] nextLine = null;
        while ((nextLine = reader.readNext()) != null) {
            System.out.println(Arrays.toString(nextLine));
        }
    } catch (Exception e) {
        System.err.println("Error!! " + e.getMessage());
    } finally {
        if (null != reader) {
            try {
                reader.close();
            } catch (IOException e) {
                System.err.println("Error closing file!! " + e.getMessage());
            }
        }
    }
}
Also used : CSVReader(com.opencsv.CSVReader) FileReader(java.io.FileReader) IOException(java.io.IOException) IOException(java.io.IOException)

Example 32 with CSVReader

use of com.opencsv.CSVReader in project airpal by airbnb.

the class ResultsPreviewResource method getFilePreview.

private Response getFilePreview(URI fileURI, int numLines) {
    String fileName = getFilename(fileURI);
    final File file = fileStore.get(fileName);
    try {
        if (file == null) {
            throw new FileNotFoundException(fileName + " could not be found");
        }
        try (final CSVReader reader = new CSVReader(new FileReader(file))) {
            return getPreviewFromCSV(reader, numLines);
        } catch (IOException e) {
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
        }
    } catch (FileNotFoundException e) {
        log.warn(e.getMessage());
        return Response.status(Response.Status.NOT_FOUND).build();
    }
}
Also used : CSVReader(com.opencsv.CSVReader) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File)

Example 33 with CSVReader

use of com.opencsv.CSVReader in project perun by CESNET.

the class ExtSourceCSV method csvParsing.

private List<Map<String, String>> csvParsing(String query, int maxResults) throws InternalErrorException, FileNotFoundException, IOException {
    List<Map<String, String>> subjects = new ArrayList<Map<String, String>>();
    FileReader fileReader = new FileReader(file);
    if (fileReader == null) {
        throw new FileNotFoundException("File was not found!");
    }
    CSVReader reader = new CSVReader(fileReader);
    header = reader.readNext();
    if (header == null) {
        throw new RuntimeException("No header in csv file");
    }
    String[] row;
    while ((row = reader.readNext()) != null) {
        if (header.length != row.length) {
            throw new RuntimeException("Csv file is not valid - some rows have different number of columns from the header row.");
        }
        if (compareRowToQuery(row, query)) {
            Map<String, String> map = convertLineToMap(row);
            if (map != null) {
                subjects.add(map);
            }
            if (maxResults > 0) {
                if (subjects.size() >= maxResults) {
                    break;
                }
            }
        }
    }
    return subjects;
}
Also used : CSVReader(com.opencsv.CSVReader) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) HashMap(java.util.HashMap) Map(java.util.Map)

Example 34 with CSVReader

use of com.opencsv.CSVReader in project pictureapp by EyeSeeTea.

the class PopulateDB method addVisibleQuestions.

public static void addVisibleQuestions(Context context, List<Question> questions) throws IOException {
    //Reset inner references
    CSVReader reader = new CSVReader(new InputStreamReader(context.openFileInput(QUESTIONS_CSV)), SEPARATOR, QUOTECHAR);
    String[] line;
    while ((line = reader.readNext()) != null) {
        for (Question question : questions) {
            if (question.getUid().equals(line[5])) {
                question.setVisible(Integer.valueOf(line[14]));
                question.save();
                break;
            }
        }
    }
    reader.close();
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(com.opencsv.CSVReader) Question(org.eyeseetea.malariacare.data.database.model.Question)

Example 35 with CSVReader

use of com.opencsv.CSVReader in project pictureapp by EyeSeeTea.

the class PopulateDB method addTotalQuestions.

public static void addTotalQuestions(Context context, List<Question> questions) throws IOException {
    //Reset inner references
    CSVReader reader = new CSVReader(new InputStreamReader(context.openFileInput(QUESTIONS_CSV)), SEPARATOR, QUOTECHAR);
    String[] line;
    while ((line = reader.readNext()) != null) {
        for (Question question : questions) {
            if (question.getUid().equals(line[5])) {
                question.setTotalQuestions(Integer.valueOf(line[13]));
                question.save();
                break;
            }
        }
    }
    reader.close();
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(com.opencsv.CSVReader) Question(org.eyeseetea.malariacare.data.database.model.Question)

Aggregations

CSVReader (com.opencsv.CSVReader)57 InputStreamReader (java.io.InputStreamReader)50 ArrayList (java.util.ArrayList)23 HashMap (java.util.HashMap)18 Question (org.eyeseetea.malariacare.data.database.model.Question)11 QuestionOption (org.eyeseetea.malariacare.data.database.model.QuestionOption)9 Match (org.eyeseetea.malariacare.data.database.model.Match)8 Option (org.eyeseetea.malariacare.data.database.model.Option)8 Answer (org.eyeseetea.malariacare.data.database.model.Answer)6 OptionAttribute (org.eyeseetea.malariacare.data.database.model.OptionAttribute)6 TreatmentMatch (org.eyeseetea.malariacare.data.database.model.TreatmentMatch)6 FileReader (java.io.FileReader)5 IOException (java.io.IOException)5 QuestionRelation (org.eyeseetea.malariacare.data.database.model.QuestionRelation)5 Treatment (org.eyeseetea.malariacare.data.database.model.Treatment)5 Drug (org.eyeseetea.malariacare.data.database.model.Drug)4 Organisation (org.eyeseetea.malariacare.data.database.model.Organisation)4 StringKey (org.eyeseetea.malariacare.data.database.model.StringKey)4 PopulateRow.populateMatch (org.eyeseetea.malariacare.data.database.utils.populatedb.PopulateRow.populateMatch)4 File (java.io.File)3