2014-02-11 75 views
0

我無法理解,因爲這段代碼讀取的文件太多次了。相同的代碼,帶有特殊字符的CSV作爲分隔符,效果很好......但帶有CSV;因爲分隔符讀取同一個文件的次數太多了。BufferedReader讀取次數太多

這裏COSE:

public static ArrayList<Asset> retriveCIInstances(File sap_csv_source, String parseChar) throws IOException { 
    System.out.println("PARSING WITH THE CHAR: " + parseChar); 
    ArrayList<Asset> res = new ArrayList<Asset>();  
    FileInputStream fstream = new FileInputStream(sap_csv_source);   
    DataInputStream in = new DataInputStream(fstream); 
    BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
    String strLine; 
    while ((strLine = br.readLine()) != null) { 
     System.out.println("LINE CSV: " + strLine); 
     String[] raw = strLine.split(parseChar); 
     res.add(new Asset(null,raw[1],raw[0],raw[8], null,null)); 
    } 
    in.close(); 
    fstream.close(); 
    return res; 
} 

和這兩個文件的內容有: (不工作)

ASSET_TAG;SAP Purchase Order number;SAP Purchase Order position number;Asset Type;Asset Category;Asset description;Manufacturer;Model;Serial Number;Location ID;Delivery Note number;Delivery note date;EOF 
B1CSSW01SN78JJNYU76U;4500136741;20;Switch;Network;;Cisco;WS-C2960-24LT-L;SN78JJNYU76U;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;EOF 
B1CSSW01SNRU7687JUIUJ;4500136741;20;Switch;Network;;Cisco;WS-C2960-24LT-L;SNRU7687JUIUJ;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;EOF 
B1CSSW01SNFGH6765Y66;4500136741;20;Switch;Network;;Cisco;WS-C2960-24LT-L;SNFGH6765Y66;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;EOF 
B1CSSW01SN3435REG45Y;4500136741;20;Switch;Network;;Cisco;WS-C2960-24LT-L;SN3435REG45Y;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;EOF

或 (作品)

 ASSET_TAG¤SAP Purchase Order number¤SAP Purchase Order position number¤Asset Type¤Asset Category¤Asset description¤Manufacturer¤Model¤Serial Number¤Location ID¤Delivery Note number¤Delivery note date¤EOF 
B1HPDT06SN345TREG5Y¤4500117819¤10¤Desktop¤Workstation¤¤HP¤8200 Elite SFF Standard¤SN345TREG5Y¤X-RM-0047_04¤TEST_HW00_11FEB14|17|4500117819-10¤11/02/2014¤EOF 
B1HPDT06SN56654GHTD¤4500117819¤10¤Desktop¤Workstation¤¤HP¤8200 Elite SFF Standard¤SN56654GHTD¤X-RM-0047_04¤TEST_HW00_11FEB14|17|4500117819-10¤11/02/2014¤EOF 
B1HPDT06SNGJ987IKJJH¤4500117819¤10¤Desktop¤Workstation¤¤HP¤8200 Elite SFF Standard¤SNGJ987IKJJH¤X-RM-0047_04¤TEST_HW00_11FEB14|17|4500117819-10¤11/02/2014¤EOF 
B1HPDT06SN65765HT56¤4500117819¤10¤Desktop¤Workstation¤¤HP¤8200 Elite SFF Standard¤SN65765HT56¤X-RM-0047_04¤TEST_HW00_11FEB14|17|4500117819-10¤11/02/2014¤EOF

而輸出是:

 LINE CSV: ASSET_TAG;SAP Purchase Order number;SAP Purchase Order position 
;Asset Type;Asset Category;Asset description;Manufacturer;Model;Serial Num 
cation ID;Delivery Note number;Delivery note date;EOF 
LINE CSV: B1CSSW01SN78JJNYU76U;4500136741;20;Switch;Network;;Cisco;WS-C296 
-L;SN78JJNYU76U;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;EOF 
LINE CSV: B1CSSW01SNRU7687JUIUJ;4500136741;20;Switch;Network;;Cisco;WS-C29 
T-L;SNRU7687JUIUJ;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;E 
LINE CSV: B1CSSW01SNFGH6765Y66;4500136741;20;Switch;Network;;Cisco;WS-C296 
-L;SNFGH6765Y66;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;EOF 
LINE CSV: B1CSSW01SN3435REG45Y;4500136741;20;Switch;Network;;Cisco;WS-C296 
-L;SN3435REG45Y;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;EOF 
LINE CSV: ASSET_TAG;SAP Purchase Order number;SAP Purchase Order position 
;Asset Type;Asset Category;Asset description;Manufacturer;Model;Serial Num 
cation ID;Delivery Note number;Delivery note date;EOF 
LINE CSV: B1CSSW01SN78JJNYU76U;4500136741;20;Switch;Network;;Cisco;WS-C296 
-L;SN78JJNYU76U;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;EOF 
LINE CSV: B1CSSW01SNRU7687JUIUJ;4500136741;20;Switch;Network;;Cisco;WS-C29 
T-L;SNRU7687JUIUJ;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;E 
LINE CSV: B1CSSW01SNFGH6765Y66;4500136741;20;Switch;Network;;Cisco;WS-C296 
-L;SNFGH6765Y66;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;EOF 
LINE CSV: B1CSSW01SN3435REG45Y;4500136741;20;Switch;Network;;Cisco;WS-C296 
-L;SN3435REG45Y;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;EOF 
LINE CSV: ASSET_TAG;SAP Purchase Order number;SAP Purchase Order position 
;Asset Type;Asset Category;Asset description;Manufacturer;Model;Serial Num 
cation ID;Delivery Note number;Delivery note date;EOF 
LINE CSV: B1CSSW01SN78JJNYU76U;4500136741;20;Switch;Network;;Cisco;WS-C296 
-L;SN78JJNYU76U;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;EOF 
LINE CSV: B1CSSW01SNRU7687JUIUJ;4500136741;20;Switch;Network;;Cisco;WS-C29 
T-L;SNRU7687JUIUJ;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;E 
LINE CSV: B1CSSW01SNFGH6765Y66;4500136741;20;Switch;Network;;Cisco;WS-C296 
-L;SNFGH6765Y66;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;EOF 
LINE CSV: B1CSSW01SN3435REG45Y;4500136741;20;Switch;Network;;Cisco;WS-C296 
-L;SN3435REG45Y;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;EOF 
LINE CSV: ASSET_TAG;SAP Purchase Order number;SAP Purchase Order position 
;Asset Type;Asset Category;Asset description;Manufacturer;Model;Serial Num 
cation ID;Delivery Note number;Delivery note date;EOF 
LINE CSV: B1CSSW01SN78JJNYU76U;4500136741;20;Switch;Network;;Cisco;WS-C296 
-L;SN78JJNYU76U;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;EOF 
LINE CSV: B1CSSW01SNRU7687JUIUJ;4500136741;20;Switch;Network;;Cisco;WS-C29 
T-L;SNRU7687JUIUJ;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;E 
LINE CSV: B1CSSW01SNFGH6765Y66;4500136741;20;Switch;Network;;Cisco;WS-C296 
-L;SNFGH6765Y66;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;EOF 
LINE CSV: B1CSSW01SN3435REG45Y;4500136741;20;Switch;Network;;Cisco;WS-C296 
-L;SN3435REG45Y;X-MAG-VIRTUA;TEST11FEB2014|17|4500136741-20;11/02/2014;EOF

對於四行,它打印了很多行...我不明白。

有什麼想法?

在此先感謝

+1

顯示你的所有代碼。特別是,你要返回'List ';是打印內容的來電者?另外,使用'try-with-resources'比手動關閉流更好,但即使您需要手動關閉它們,調用'br.close()'也會導致它所構建的所有流成爲關閉。 – chrylis

+0

我試過你的代碼,它的工作原理。我改變了唯一的事情就是註釋掉'res.add(new Asset(null,raw [1],raw [0],raw [8],null,null));'因爲我不知道資產是什麼上課是。那裏必須發生一些事情。 – StephaneM

+0

問題出在返回之前...... System.out會打印完整的文件(逐行),您可以看到太多次(請參見打印X次的文件頭)。 – sickk

回答

0

這是一個愚蠢的問題...該文件是由外部源寫入多次。抱歉給你帶來不便!我很慚愧