我有一个具有文件上传功能的Spring MVC应用程序.文件作为MultipartFile传递给控制器,从中很容易获得InputStream.我正在上传包含CSV的zip文件,我正在努力寻找打开CSV并一次读取一行的方法."读入固定大小缓冲区的网络上有很多例子.我试过这个,但缓冲区不能很好地连接,很快就会失去同步并占用大量内存:
ZipEntry entry = input.getNextEntry();
while(entry != null)
{
if (entry.getName().matches("Data/CSV/[a-z]{0,1}[a-z]{0,1}.csv"))
{
final String fullPath = entry.getName();
final String filename = fullPath.substring(fullPath.lastIndexOf('/') + 1);
visitor.startFile(filename);
final StringBuilder fileContent = new StringBuilder();
final byte[] buffer = new byte[1024];
while (input.read(buffer) > 0)
fileContent.append(new String(buffer));
final String[] lines = fileContent.toString().split("\n");
for(String line : lines)
{
final String[] columns = line.split(",");
final String postcode = columns[0].replace(" ", "").replace("\"", "");
if (columns.length > 3)
visitor.location(postcode, "", "");
}
visitor.endFile();
}
entry = input.getNextEntry();
}
Run Code Online (Sandbox Code Playgroud)
必须有一种更好的实际工作方式.
不清楚这是否适合您的需要,但您尝试过opencsv(http://opencsv.sourceforge.net)吗?他们的例子非常直观:
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
Run Code Online (Sandbox Code Playgroud)
对于您的情况,您只需要将压缩文件流包装到缓冲读取器中并传递读取器以创建CSVReader并使用它:
FileInputStream fis = new FileInputStream(file);
GZIPInputStream gis = new GZIPInputStream(fis);
InputStreamReader isr = new InputStreamReader(gis);
BufferedReader br = new BufferedReader(isr);
CSVReader reader = new CSVReader(br);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5143 次 |
| 最近记录: |