0

I want to read data from CSV file.

File csvfile = new File("/sdcard/Download/" + returnFileName(Integer.parseInt(year)));
CSVReader reader = new CSVReader(new FileReader(csvfile.getAbsolutePath()));
List<String[]> nextLine = reader.readAll();

This is my functon. It reads all data exepct data with special letters like Gdańsk with (ń). It just gives empty string

I expect to get all data even if it has special letters

  • 1
    I can't reproduce this problem. What is the file encoding? (it works fine with UTF-8) Also are you sure you are reading correct file? Maybe file you are *actually* reading really doesn't have values you expect. – Pshemo Dec 06 '22 at 12:15

1 Answers1

0

It's all about character encoding used to store the file contents. The file was probably written in a different encoding than the one you are using to read the file. If you know what encoding was used to write the file you should use the same encoding while reading the file. FileReader reader uses the default platform encoding if you do not provide one by means of a dedicated FileReader(File, Charset) constructor:

The documentation is clear about it:

Decoding from bytes to characters uses either a specified charset or the default charset.

Unfortunately, there's no reliable way to automatically detect the encoding of a text file. The only way is to try the most popular encodings used to write your language diacritical characters and see if the file read operation gives you the expected outcome. You can read more about possible encodings used for Polish alphabet here.

Marcin Kłopotek
  • 5,271
  • 4
  • 31
  • 51
  • Yes it is UTF-8. ``Charset charset = Charset.forName("UTF-8"); File csvfile = new File("/sdcard/Download/" + returnFileName(Integer.parseInt(year))); CSVReader reader = new CSVReader(new FileReader(csvfile.getAbsolutePath(), charset)); List nextLine = reader.readAll(); ` But im recieving an error when i want to implement the Charset – Bartosz Boluk Dec 06 '22 at 13:51
  • What error do you have? – Marcin Kłopotek Dec 06 '22 at 14:01
  • error: no suitable constructor found for FileReader(String,Charset) CSVReader reader = new CSVReader(new FileReader(csvfile.getAbsolutePath(), charset)); – Bartosz Boluk Dec 06 '22 at 14:04
  • The constructor `FileReader(File, Charset)` was introduced in JDK11. In case you are using an older JDK version then you can use `FileInputStream(InputStream, Charset)` instead. – Marcin Kłopotek Dec 06 '22 at 14:57
  • Charset charset = Charset.forName("UTF-8"); File csvfile = new File("/sdcard/Download/" + returnFileName(Integer.parseInt(year))); CSVReader reader = new CSVReader(new FileReader(new FileInputStream(csvfile.getAbsolutePath(), charset))); Got the same error "no suitable constructor found for FileInputStream(String,Charset)" – Bartosz Boluk Dec 06 '22 at 15:27
  • @BartoszBoluk You can use `InputStreamReader` to specify charset: `CSVReader reader = new CSVReader(new InputStreamReader(new FileInputStream(csvfile), StandardCharsets.UTF_8));` – Pshemo Dec 06 '22 at 16:33
  • @Pshemo still not loading the data with special letters. Maybe i check endcoding wrong. How can i do it for excel file? – Bartosz Boluk Dec 06 '22 at 18:42