简体   繁体   中英

Special characters from txt file

I am downloading a text file from ftp, with common ftp library.

The problem is when i read the file into an array line by line, it doesnt take characters such as æøå. Instead it just show the "?" character.

Here is my code

  FileInputStream fstream = openFileInput("name of text file");
  BufferedReader br = new BufferedReader(new InputStreamReader(fstream, "UTF-8"));
  String strLine;

  ArrayList<String> lines = new ArrayList<String>(); 

  while ((strLine = br.readLine()) != null)   {
      lines.add(strLine);
  }

  String[] linjer = lines.toArray(new String[0]);

  ArrayList<String> imei = new ArrayList<String>(); 

  for(int o=0;o<linjer.length;o++)
  {
      String[] holder = linjer[o].split(" - ");
      imei.add(holder[0] + " - " + holder[2]);
  }

  String[] imeinr = imei.toArray(new String[0]);

I have tried to put UTF-8 in my inputstreamreader, and i have tried with a UnicodeReader class, but with no success.

I am fairly new to Java, so might just be some stupid question, but hope you can help. :)

There is no reason to use a DataInputStream . The DataInputStream and DataOutputStream classes are used for serializing primitive Java data types ("serializing" means reading/writing data to a file). You are just reading the contents of a text file line by line, so the use of DataInputStream is unnecessary and may produce incorrect results.

FileInputStream fstream = openFileInput("name of text file");
//DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream, "UTF-8"));

Professional Java Programmer Tip : The foreach loop was recently added to the Java programming language. It allows the programmer to iterate through the contents of an array without needing to define a loop counter. This simplifies your code, making it easier to read and maintain over time.

for(String line : linjer){
  String[] holder = line.split(" - ");
  imei.add(holder[0] + " - " + holder[2]);
}

Note: Foreach loops can also be used with List objects.

I would suggest that the file may not be in UTF-8. It could be in CP1252 or something, especially if you're using Windows.

Try downloading the file and running your code on the local copy to see if that works.

FTP has two modes binary and ascii. Make sure you are using the correct mode. Look here for details: http://www.rhinosoft.com/newsletter/NewsL2008-03-18.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM