本文目录一览:
java实现把csv文件导入到sqlserver
可以通过BufferedReader
流的形式进行流缓存,之后通过readLine方法获取到缓存的内容。
BufferedReader
bre
=
null;
try
{
String
file
=
"D:/test/test.txt";
bre
=
new
BufferedReader(new
FileReader(file));//此时获取到的bre就是整个文件的缓存流
while
((str
=
bre.readLine())!=
null)
//
判断最后一行不存在,为空结束循环
{
String
item[]
=
bre.split(",");//CSV格式文件为逗号分隔符文件,这里根据逗号切分
String
last
=
item[item.length-1];//通过此方法获取到数据信息
//int
value
=
Integer.parseInt(last);//如果是数值,可以转化为数值
System.out.println(last);//此处转换成数据库相应的语句即可实现存储操作
};
备注:
流用完之后必须close掉,如上面的就应该是:bre.close(),否则bre流会一直存在,直到程序运行结束。
JAVA导入txt、CSV的例子
导入文件若为二进制格式的csv或xls文件需要用jxl.jar包来解析,若文件为文本格式逗号分隔的csv文件则需要用opencsv.jar包来解析。下面为导入两种格式文件的代码示例:1. 导入文本格式逗号分隔的csv文件Java代码 import au.com.bytecode.opencsv.CSVReader; import au.com.bytecode.opencsv.CSVReader; Java代码 public void importCsvFile() { CSVReader csvReader = null; try { csvReader = new CSVReader(new FileReader(importFile),',');//importFile为要导入的文本格式逗号分隔的csv文件,提供getXX/setXX方法 if(csvReader != null){ //first row is title, so past csvReader.readNext(); String[] csvRow = null;//row while ((csvRow = csvReader.readNext()) != null){ for (int i =0; icsvRow.length; i++){ String temp = csvRow[i]; switch (i) { case 0: if(StringUtils.isNotEmpty(temp)){ linkman.setLinkmanName(temp); } break; case 1: if(StringUtils.isNotEmpty(temp)){ linkman.setLinkmanEmail(temp); } break; default: break; } } //保存linkman到数据库 if(linkman.getLinkmanName() != null linkman.getLinkmanEmail() != null){ EmailLinkmanAPI.insertLinkman(linkman); } } } } catch (Exception e) { e.printStackTrace(); } } public void importCsvFile() { CSVReader csvReader = null; try { csvReader = new CSVReader(new FileReader(importFile),',');//importFile为要导入的文本格式逗号分隔的csv文件,提供getXX/setXX方法 if(csvReader != null){ //first row is title, so past csvReader.readNext(); String[] csvRow = null;//row while ((csvRow = csvReader.readNext()) != null){ for (int i =0; icsvRow.length; i++){ String temp = csvRow[i]; switch (i) { case 0: if(StringUtils.isNotEmpty(temp)){ linkman.setLinkmanName(temp); } break; case 1: if(StringUtils.isNotEmpty(temp)){ linkman.setLinkmanEmail(temp); } break; default: break; } } //保存linkman到数据库 if(linkman.getLinkmanName() != null linkman.getLinkmanEmail() != null){ EmailLinkmanAPI.insertLinkman(linkman); } } } } catch (Exception e) { e.printStackTrace(); } } 2. 二进制格式的csv或xls文件Java代码 import jxl.Sheet; import jxl.Workbook; import jxl.Sheet; import jxl.Workbook; Java代码 public void importXlsFile() { Workbook book = null; try { book = Workbook.getWorkbook(importFile);//importFile为要导入的xls文件,或二进制的csv文件,提供getXX/setXX方法 } catch (Exception e) { e.printStackTrace(); } if(book != null){ int sheetNo = book.getNumberOfSheets(); for(int i = 0; i sheetNo; i++){ Sheet sheet=book.getSheet(i); int rowNum = sheet.getRows(); int colNum = sheet.getColumns(); //first row is title, so past for(int r = 1; r rowNum; r++){//行 for(int c = 0; c colNum;c++){//列 String temp = sheet.getCell(c,r).getContents(); switch (c) { case 0: if(StringUtils.isNotEmpty(temp)){ linkman.setLinkmanName(temp); } break; case 1: if(StringUtils.isNotEmpty(temp)){ linkman.setLinkmanEmail(temp); } break; default: break; } } //保存linkman到数据库 if(linkman.getLinkmanName() != null linkman.getLinkmanEmail() != null){ EmailLinkmanAPI.insertLinkman(linkman); } } } } }
求个java读取csv文件并输出的程序
要使用CsvReader,CsvWriter需要下载一个javacsv.jar导入到项目中
package test;import com.csvreader.CsvReader;import com.csvreader.CsvWriter;import java.io.BufferedReader; import java.io.BufferedWriter;import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter;import java.io.IOException; public class readandwrite { public static void main(String[] args) throws IOException { String [] str ={"省","市","区","街","路","里","幢","村","室","园","苑","巷","号"}; String inString = ""; String tmpString = ""; File inFile = new File("C://in.csv"); // 读取的CSV文件 File outFile = new File("C://outtest.csv");//输出的CSV文 try { BufferedReader reader = new BufferedReader(new FileReader(inFile)); BufferedWriter writer = new BufferedWriter(new FileWriter(outFile)); CsvReader creader = new CsvReader(reader, ','); CsvWriter cwriter = new CsvWriter(writer,','); while(creader.readRecord()){ inString = creader.getRawRecord();//读取一行数据 for(int i = 0;i str.length;i++){ tmpString = inString.replace(str[i], "," + str[i] + ","); inString = tmpString; } //第一个参数表示要写入的字符串数组,每一个元素占一个单元格,第二个参数为true时表示写完数据后自动换行 cwriter.writeRecord(inString.split(","), true); //注意,此时再用cwriter.write(inString)方法写入数据将会看到只往第一个单元格写入了数据,“,”没起到调到下一个单元格的作用 //如果用cwriter.write(String str)方法来写数据,则要用cwriter.endRecord()方法来实现换行 //cwriter.endRecord();//换行 cwriter.flush();//刷新数据 } creader.close(); cwriter.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } }}
java里将从excel读到的数据用csv导出,代码怎么写
解释:csv文件实际上就是字符串,之间用“,”进行分割,之后进行的存储。
工具类如下:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
/**
*
* CSV文件导出工具类
*/
public class CSVUtils {
/**
* CSV文件生成方法
* @param head
* @param dataList
* @param outPutPath
* @param filename
* @return
*/
public static File createCSVFile(List head, ListList dataList,
String outPutPath, String filename) {
File csvFile = null;
BufferedWriter csvWtriter = null;
try {
csvFile = new File(outPutPath + File.separator + filename + ".csv");
File parent = csvFile.getParentFile();
if (parent != null !parent.exists()) {
parent.mkdirs();
}
csvFile.createNewFile();
// GB2312使正确读取分隔符","
csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
csvFile), "GB2312"), 1024);
// 写入文件头部
writeRow(head, csvWtriter);
// 写入文件内容
for (List row : dataList) {
writeRow(row, csvWtriter);
}
csvWtriter.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvWtriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
}
/**
* 写一行数据方法
* @param row
* @param csvWriter
* @throws IOException
*/
private static void writeRow(List row, BufferedWriter csvWriter) throws IOException {
// 写入文件头部
for (Object data : row) {
StringBuffer sb = new StringBuffer();
String rowStr = sb.append("\"").append(data).append("\",").toString();
csvWriter.write(rowStr);
}
csvWriter.newLine();
}
}
Object-Z