2017-02-27 163 views
0

的數據,有一個問題我已經發現的是,它在我的計劃的Java CsvReader沒有得到我使用JavaCSV讀寫CSV文件的第一列

字符串n已接收=飛機的部分忽略了第一列數據。獲得( 「N-NUMBER」);沒有返回,即使有東西在數據庫

CsvReader http://javacsv.sourceforge.net/com/csvreader/CsvReader.html

import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.lang.String; 
import java.io.File; 
import java.io.FileWriter; 
import java.lang.String; 
import com.csvreader.CsvReader; 

private static void compare(String firstName, String lastName, String st1, String st2, String city, String state, String zip, 
     String country, String region, String fileName, Integer pilotCount) 
{ 
    // TODO Auto-generated method stub 
    try { 

     CsvReader planes = new CsvReader("MASTER.csv"); 
     planes.readHeaders(); 

     while (planes.readRecord()) 
     { 
      String nNumber = planes.get("N-NUMBER"); 
      String SN = planes.get("SERIAL NUMBER"); 
      String model = planes.get("MFR MDL CODE"); 
      String engine = planes.get("ENG MFR MDL"); 
      String year = planes.get("YEAR MFR"); 
      String typeReg = planes.get("TYPE REGISTRANT"); 
      String name = planes.get("NAME"); 
      String st_1 = planes.get("STREET"); 
      String st_2 = planes.get("STREET2"); 
      String city_plane = planes.get("CITY"); 
      String pilot_state = planes.get("STATE"); 
      String zip_code = planes.get("ZIP CODE"); 
      String country_name = planes.get("COUNTRY"); 
      String region_name = planes.get("REGION"); 
      //boolean response = compare(st1, st2, city, state, zip, country, region, st_1, st_2, city_plane, pilot_state, zip_code, country_name, region_name); 
      boolean response = compareSimple(firstName, lastName, name); 

      if (response == true) 
      { 
       System.out.println("N Number"); 
       System.out.println(nNumber); 
       System.out.println("Serial"); 
       System.out.println(SN); 
       System.out.println("Model"); 
       System.out.println(model); 
       System.out.println("Engine"); 
       System.out.println(engine); 
       System.out.println("Year"); 
       System.out.println(year); 
       System.out.println("Type Registration"); 
       System.out.println(typeReg); 
       System.out.println("Name"); 
       System.out.println(name); 
       pilotCount++; 
       write(nNumber, SN, model, engine, year, typeReg, name, st_1, st_2, city_plane, pilot_state, zip_code, country_name, region_name, fileName); 
       System.out.println(pilotCount); 
      } 
      else if (response == false) 
      { 
       // do nothing 
      } 
     } 
     planes.close(); 
    } 
    catch (FileNotFoundException e) 
    { 
     e.printStackTrace(); 
    } catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
} 
+1

您是否嘗試過調試代碼? MASTER.csv文件包含什麼? – Janar

+1

顯示您的'MASTER.csv'的實際內容,特別是標題。我認爲你的第一個列標題名稱是關閉的。 – VHS

+0

N-NUMBER,序列號,MFR MDL代碼,ENG MFR MDL,年份MFR,類型註冊者,名稱,街道,街道2,城市,州,郵政編碼,地區,縣,國家,最後訴訟日期,證書發放日期, (1),其他名稱(2),其他名稱(3),其他名稱(4),其他名稱(5),類型飛機,類型引擎,狀態代碼,模式S代碼, ,到期日期,唯一ID,套件MFR,套件模型,模式S代碼十六進制, – user3058423

回答

0

可能與this one

您可能正在閱讀中的文章有錯誤的編碼,或者您的文件有一個BOM標記,在這種情況下,文件以幾個字節開始,指示編碼是什麼。在這種情況下,你可以跳過這些字節,以獲得正確的輸出:如果您確信這是情況並非如此,沒有一個BOM標記

InputStream in = new FileInputStream("/path/to/file.csv", "UTF-8"); 

//this skips UTF-8 BOM bytes, adjust to discard the bytes of whatever you have: 

if(in.read() == 239 & in.read() == 187 & in.read() == 191){ 
    System.out.println("UTF-8 with BOM, bytes discarded"); 
} 

//parse the InputStream now as the BOM bytes have been skipped. 

,然後嘗試切換到另一個庫,看看問題仍然存在。

+0

將編碼從UTF-8 BOM更改爲UTF-8修復了這個問題。 – user3058423