「解析CSVjava」解析俄乌冲突焦点
本篇文章给大家谈谈解析CSVjava,以及解析俄乌冲突焦点对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java读取CSV文件
这是我写的个类 你参考下 其实那个引号是不用管的
public class CsvUtil1 {
private String filename = null;
private BufferedReader bufferedreader = null;
private List list = new ArrayList();
public CsvUtil1() {
}
public CsvUtil1(String filename) throws IOException {
this.filename = filename;
bufferedreader = new BufferedReader(new FileReader(filename));
String stemp;
while ((stemp = bufferedreader.readLine()) != null) {
list.add(stemp);
}
}
public List getList() throws IOException {
return list;
}
public int getRowNum() {
return list.size();
}
public int getColNum() {
if (!list.toString().equals("[]")) {
if (list.get(0).toString().contains(",")) {
return list.get(0).toString().split(",").length;
} else if (list.get(0).toString().trim().length() != 0) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
}
public String getRow(int index) {
if (this.list.size() != 0)
return (String) list.get(index);
else
return null;
}
public String getCol(int index) {
if (this.getColNum() == 0) {
return null;
}
StringBuffer scol = new StringBuffer();
String temp = null;
int colnum = this.getColNum();
if (colnum 1) {
for (Iterator it = list.iterator(); it.hasNext();) {
temp = it.next().toString();
scol = scol.append(temp.split(",")[index] + ",");
}
} else {
for (Iterator it = list.iterator(); it.hasNext();) {
temp = it.next().toString();
scol = scol.append(temp + ",");
}
}
String str = new String(scol.toString());
str = str.substring(0, str.length() - 1);
return str;
}
public String getString(int row, int col) {
String temp = null;
int colnum = this.getColNum();
if (colnum 1) {
temp = list.get(row).toString().split(",")[col];
} else if (colnum == 1) {
temp = list.get(row).toString();
} else {
temp = null;
}
return temp;
}
public void CsvClose() throws IOException {
this.bufferedreader.close();
}
public void test() throws IOException {
CsvUtil1 cu = new CsvUtil1("D:/学习/00dw.csv");
List tt = cu.getList();
for (Iterator itt = tt.iterator(); itt.hasNext();) {
System.out.println(itt.next().toString()+"||");
}
// System.out.println(cu.getRowNum());
// System.out.println(cu.getColNum());
// System.out.println(cu.getRow(0));
// System.out.println(cu.getCol(0));
// System.out.println(cu.getString(0, 0));
cu.CsvClose();
}
public void createCsvTest1(HttpServletResponse Response) throws IOException {
CsvUtil1 cu = new CsvUtil1("D:/学习/00dw.csv");
List tt = cu.getList();
String data = "";
SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMddHHmm");
Date today = new Date();
String dateToday = dataFormat.format(today);
File file=new File("D:/学习/001dw.csv");
if(!file.exists())
file.createNewFile();
// else
// file.delete() ;
String str[] ;
StringBuilder sb = new StringBuilder("");
BufferedWriter output=new BufferedWriter(new FileWriter(file,true));
for (Iterator itt = tt.iterator(); itt.hasNext();) {
String fileStr = itt.next().toString() ;
str = fileStr.split(",");
for(int i=0;i=str.length-1;i++){ //拆分成数组 用于插入数据库中
System.out.print("str["+i+"]="+str[i]+" ");
}
System.out.println("");
sb.append(fileStr+"\r\n") ;
}
//System.out.println(sb.toString());
output.write(sb.toString());
output.flush() ;
output.close();
cu.CsvClose();
}
public static void main(String[] args) throws IOException {
CsvUtil1 test = new CsvUtil1();
//test.test();
HttpServletResponse response = null ;
test.createCsvTest1(response);
}
}
如何正确读取csv文件
package xufei;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
* 文件规则
* Microsoft的格式是最简单的。以逗号分隔的值要么是“纯粹的”(仅仅包含在括号之前),
* 要么是在双引号之间(这时数据中的双引号以一对双引号表示)。
* Ten Thousand,10000, 2710 ,,"10,000","It's ""10 Grand"", baby",10K
* 这一行包含七个字段(fields):
* Ten Thousand
* 10000
* 2710
* 空字段
* 10,000
* It's "10 Grand", baby
* 10K
* 每条记录占一行
* 以逗号为分隔符
* 逗号前后的空格会被忽略
* 字段中包含有逗号,该字段必须用双引号括起来。如果是全角的没有问题。
* 字段中包含有换行符,该字段必须用双引号括起来
* 字段前后包含有空格,该字段必须用双引号括起来
* 字段中的双引号用两个双引号表示
* 字段中如果有双引号,该字段必须用双引号括起来
* 第一条记录,可以是字段名
*/
/**
*
タイトル: xufei.CSVAnalysis.java
*
说明:
*
著作権: Copyright (c) 2006
*
会社名: technodia
* @author 徐飞
* @version 1.0
* createDate Aug 11, 2008
* 修正履歴
* 修正日 修正者 修正理由
*/
public class CSVAnalysis {
private InputStreamReader fr = null;
private BufferedReader br = null;
public CSVAnalysis(String f) throws IOException {
fr = new InputStreamReader(new FileInputStream(f));
}
/**
* 解析csv文件 到一个list中
* 每个单元个为一个String类型记录,每一行为一个list。
* 再将所有的行放到一个总list中
* @return
* @throws IOException
*/
public List readCSVFile() throws IOException {
br = new BufferedReader(fr);
String rec = null;//一行
String str;//一个单元格
List listFile = new ArrayList();
try {
//读取一行
while ((rec = br.readLine()) != null) {
Pattern pCells = Pattern
.compile("(\"[^\"]*(\"{2})*[^\"]*\")*[^,]*,");
Matcher mCells = pCells.matcher(rec);
List cells = new ArrayList();//每行记录一个list
//读取每个单元格
while (mCells.find()) {
str = mCells.group();
str = str.replaceAll(
"(?sm)\"?([^\"]*(\"{2})*[^\"]*)\"?.*,", "$1");
str = str.replaceAll("(?sm)(\"(\"))", "$2");
cells.add(str);
}
listFile.add(cells);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fr != null) {
fr.close();
}
if (br != null) {
br.close();
}
}
return listFile;
}
public static void main(String[] args) throws Throwable {
CSVAnalysis parser = new CSVAnalysis("c:/test2.csv");
parser.readCSVFile();
}
}
java处理csv文件
我来说一下大致的实现步骤,具体实现需要你自己去写了
1.检索数据,检索到的数据假定为一个list
2.你需要自己写一个objectToString之类的方法来把检索到的数据转化为一个String或StringBuffer,就是往各字段间插",",往个记录间插"\r\n",如此这类的转换,假定转换好的字符串为strResult.
3.然后用下面的代码写在后台来控制下载,文件名那里你可以把时间格式控制好,或者用前台传过来的参数做名字。
response.setContentType("application/download;charset=UTF-8");
response.setHeader("Content-disposition","attachment;filename=\"" +new Date()+".csv\"");
OutputStream o = response.getOutputStream();
byte b[] = strResult.getBytes();
try{
o.write(b);
}catch(IOException e){
e.printStackTrace();
}finally{
o.close();
}
如何用java解析CSV文件
思想:先获取csv文件的路径,通过BufferedReader类去读该路径中的文件,使用readLine方法进行逐行读取。
注意:使用readLine方法后会自动转到下一行。因此在判断是否为空后得先将读取到的内容赋值给一变量,在循环中使用该变量即可。
public static void main(String[] args)
{
File csv = new File("C:\\Users\\chenxumin\\Desktop\\Result.csv"); // CSV文件路径
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(csv));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
String line = "";
String everyLine = "";
try {
ListString allString = new ArrayList();
while ((line = br.readLine()) != null) //读取到的内容给line变量
{
everyLine = line;
System.out.println(everyLine);
allString.add(everyLine);
}
System.out.println("csv表格中所有行数:"+allString.size());
} catch (IOException e)
{
e.printStackTrace();
}
}
关于解析CSVjava和解析俄乌冲突焦点的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。