tags:

views:

376

answers:

1

hello fellow java developers. I'm having a very strange issue.

I'm trying to read a csv file line by line. Im at the point where Im just testing out the reading of the lines. ONly each time that I read a line, the line contains square characters between each character of text. I even saved the file as a txt file in wordpad and notepad with no change.

Thus I must be doing something stupid...

I have a csv file, standard csv file, yes a text file with commas in it. I try to read a line of text, but the text is all f-ed up and cannot find the phrase within the text.

Any advice? code below.

    //open csv
  File filReadMe = new File(strRoot + "data2.csv");
  BufferedReader brReadMe = new BufferedReader
     (new InputStreamReader(new FileInputStream(filReadMe)));

  String strLine = brReadMe.readLine();
  //for all lines
  while (strLine != null){

   //if line contains "(see also"
   if (strLine.toLowerCase().contains("(see also")){
    //write line from "(see also" to ")"
    int iBegin = strLine.toLowerCase().indexOf("(see also");
    String strTemp = strLine.substring(iBegin);
    int iLittleEnd = strTemp.indexOf(")");
    System.out.println(strLine.substring(iBegin, iBegin + iLittleEnd));
   }

   //update line
   strLine = brReadMe.readLine();
  } //end for

  brReadMe.close();
+3  A: 

I can only think that this is an inconsistent character encoding. Open the file in notepad, choose Save As, and select UTF-8 in the drop down for "encoding". Then add "UTF-8" as a second parameter to InputStreamReader, e.g.

BufferedReader brReadMe = new BufferedReader
     (new InputStreamReader(new FileInputStream(filReadMe), "UTF-8"));

That should sort out any inconsistencies with encoding.

mdma
Thanks, but no luck. Still seeing the squares in the read lines..
rockit
The bad bytes are happening between each byte, so I suspect a UTF-16 encoding, not UTF-8.
bkail
Wooo! UTF-16 - that was it, sorry Im a newb with file format lingo
rockit
http://www.joelonsoftware.com/articles/Unicode.html
bkail