我的任務是撰寫一種方法,該方法可以接收 csv 檔案并將其資料保存到資料庫中的適當空間中。當直接輸入csv資料時,我寫的函式就成功了。但是,當使用 cURL 并輸入整個 csv 檔案時,它不會讀取新的行分隔符。實際上,csv 然后變成一行x列,其中x是檔案中的單元格數。我曾嘗試更改 csv 格式(例如使用回車與換行),但似乎沒有任何效果。附件是通過 csv 運行的代碼,它需要一個InputStream csvData:
CSVReader reader = new CSVReader(new InputStreamReader(csvData));
String[] line;
int bookNum = 1, lineNum = 2; // skip headers
while ((line = reader.readNext()) != null) {
// map line
String productCode = line[0];
String author = line[1];
String description = line[2];
Integer edition;
try {
edition = Integer.parseInt(line[3].replaceAll("[^\\d.]", ""));
} catch (NumberFormatException nfe) {
edition = null;
}
String copyright = line[4];
String publisher = line[5];
BigDecimal listPrice = !line[6].equals("") ? new BigDecimal(line[6]) : null;
// do stuff with data...
if (bookNum == 1) System.out.println("1 book has been processed");
else System.out.println(bookNum " books have been processed");
bookNum;
lineNum;
}
uj5u.com熱心網友回復:
我認為沒有辦法實作你想要的,因為reader.readNext() 本身回傳陣列代表你的 CSV 檔案的列,陣列中的列按 CSV 檔案中列的順序索引,你可以操作通過先前知道列標題來創建陣列,以便您可以正確獲取資料。基于此:
line[0] 將是您的第一列 / line[1] 將是第二列,依此類推。
更新添加一些代碼,請嘗試使用此代碼而不是您的代碼并告訴我們結果如何
InputStream inputStream = Files.newInputStream(Paths.get(ClassLoader.getSystemResource("csv/test.csv").toURI()));
List<String[]> list = new ArrayList<>();
CSVReader csvReader = new CSVReader(new
InputStreamReader(inputStream));
String[] line;
while ((line = csvReader.readNext()) != null) {
System.out.println(line[0] " --- " line[1]);
list.add(line);
}
inputStream.close();
csvReader.close();
return list;
我在具有相同專案的類路徑中的檔案,我假設資料如下:
colA | colB
A | B
C | D
D | E
輸出如下:
colA --- colB
A --- B
C --- D
E --- F
uj5u.com熱心網友回復:
這對我有用。我認為問題在于您的輸入 csvData 是如何形成的。當您嘗試時,這是否有效?
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
public class TestCsv {
private static final String TEST_CSV = "ISBN,Author,Title,Edition,Copyright,Publisher,Value,Grade\n"
"1781435460553,1ALTEN,1WORKING WITH AUDIO (PB),12,,1CENGAGE L,0.01,1\n"
"2781435460553,2ALTEN,1WORKING WITH AUDIO (PB),22,,2CENGAGE L,0.02,2\r\n"
"3781435460553,3ALTEN,1WORKING WITH AUDIO (PB),32,,3CENGAGE L,0.03,3\r\n";
public static void main(String[] args) throws CsvValidationException, UnsupportedEncodingException, IOException {
System.out.println("String Test");
parseCsv(new ByteArrayInputStream(TEST_CSV.getBytes("UTF-8"))); // from String
System.out.println("\n\n---------------------\nFrom File Test");
String fileName = "/tmp/test.csv";
try (FileInputStream fis = new FileInputStream(fileName)) {
parseCsv(fis);
}
}
public final static void parseCsv(InputStream csvData) throws CsvValidationException, IOException {
CSVReader reader = new CSVReader(new InputStreamReader(csvData));
String[] line;
int bookNum = 1, lineNum = 2; // skip headers
while ((line = reader.readNext()) != null) {
// map line
String productCode = line[0];
String author = line[1];
String description = line[2];
Integer edition;
try {
edition = Integer.parseInt(line[3].replaceAll("[^\\d.]", ""));
} catch (NumberFormatException nfe) {
edition = null;
}
String copyright = line[4];
String publisher = line[5];
String listPrice = line[6];
// do stuff with data...
if (bookNum == 1) {
System.out.println("1 book has been processed");
} else {
System.out.println(bookNum " books have been processed");
}
bookNum;
lineNum;
}
}
}
這也適用于我,所以不太確定你遇到了什么問題:
System.out.println("standard input");
try {
parseCsv(System.in);
} catch (IOException e) {
// If nothing is passed in
e.printStackTrace();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/395592.html
