标签:inpu user code http red book throws array val
<!-- https://mvnrepository.com/artifact/net.sourceforge.javacsv/javacsv -->
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
private void readCsv(File file) throws IOException {
BufferedInputStream bufferedInputStream = null;
CsvReader reader = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
reader = new CsvReader(bufferedInputStream, ‘,‘, Charset.forName("GBK"));
while (reader.readRecord()) {
List<String> strings = Arrays.asList(reader.getValues());
System.out.println(strings);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
}
}
private void writeCsv(String[] strs) throws IOException {
String property = System.getProperty("user.dir");
File file = new File(property + "/result/commnet.csv");
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
CsvWriter csvWriter = null;
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
csvWriter = new CsvWriter(bufferedOutputStream, ‘,‘, Charset.forName("GBK"));
for (String str : strs) {
csvWriter.writeRecord(strs);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (csvWriter != null) {
csvWriter.close();
}
if (bufferedOutputStream != null) {
bufferedOutputStream.close();
}
}
}
POI中读取与excel不同之处:
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
private void readAllLine(File file) {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
XSSFWorkbook sheets = new XSSFWorkbook(bufferedInputStream)
) {
XSSFSheet sheetA = sheets.getSheetAt(0);
// 注意这里获取到的是最后一行的行号,而不是总行数,所以遍历要使用<=,不然会漏掉最后一行
int lastRowNum = sheetA.getLastRowNum();
for (int i = 0; i <= lastRowNum; i++) {
XSSFRow row = sheetA.getRow(i);
XSSFCell cell = row.getCell(0);
String stringCellValue = cell.getStringCellValue();
System.out.println("the row of " + i + " : " + stringCellValue);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 写入excel
* @param strs 待写入数据
*/
private void writeArgs(String[] strs){
File file = new File(System.getProperty("user.dir") + "/result/abc.xlsx");
if (file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try (
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
XSSFWorkbook sheets = new XSSFWorkbook();
){
XSSFSheet sheetName = sheets.createSheet("sheetName");
for (int i = 0; i < strs.length; i++) {
XSSFRow row = sheetName.createRow(i);
row.createCell(0).setCellValue(strs[i]);
}
sheets.write(bufferedOutputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
标签:inpu user code http red book throws array val
原文地址:https://www.cnblogs.com/xiaojiluben/p/14771766.html