Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a sample code for readiing csv file by java #106

Open
Zhziyao opened this issue Jun 6, 2023 · 0 comments
Open

Add a sample code for readiing csv file by java #106

Zhziyao opened this issue Jun 6, 2023 · 0 comments

Comments

@Zhziyao
Copy link

Zhziyao commented Jun 6, 2023

/**

  • 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);
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant