ECDICT icon indicating copy to clipboard operation
ECDICT copied to clipboard

Add a sample code for readiing csv file by java

Open Zhziyao opened this issue 1 year ago • 0 comments

/**

  • this sample code is not for new fish
  • 1st u need to add dependency to ur maven project
  •      <dependency>
    
  •         <groupId>com.opencsv</groupId>
    
  •         <artifactId>opencsv</artifactId>
    
  •         <version>5.6</version>
    
  •     </dependency>
    
  • 2nd u need replace the file path with yours
  • 3rd copy the sample code to ur project and run test and
  • u can see the data of words, then u can write the data to mysql db by yourself

*/ public class DictionaryDataInitService {

public static void main(String[] args) {
    try {
        ArrayList<String[]> csvList = new ArrayList<String[]>();
        Reader reader = Files.newBufferedReader(Paths.get("YOUR FILE PATH"));
        CSVReader csvReader = new CSVReader(reader);
        String[] line = null;
        line = csvReader.readNext();
        int i = 0;
        List<List<String>> results = Lists.newArrayList();
        while (line != null && i <= 3000) {
            //filter the word
            if (!line[0].contains("-")){
                if (!line[0].contains(".")) {
                    if (!line[0].startsWith("'")) {
                        if (line[0].charAt(0) > '9' || line[0].charAt(0) < '0') {
                            if (line[0].length() > 1) {
                                results.add(Arrays.asList(line));
                            }
                        }
                    }
                }
            }
            i++;
            line = csvReader.readNext();
        }
        for (List<String> item : results) {
            System.out.println(item.get(4));
        }
        csvReader.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

}

Zhziyao avatar Jun 06 '23 14:06 Zhziyao