BufferedWriter writer =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "GBK")) ;// 附加
上述代码片段作用: 转码
逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字
符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。
http://lqcjdx.blog.163.com/blog/static/207489241201356111749932/
起初,我自己连什么叫CSV文件都不知道,这个问题是来自一个网友的问题,他要我帮他做一个对csv文件数据的操作的题目。要求:如果原来数据是“江苏省南京市南京街……”换成“江苏 省 南京 市 南京 街 ……”也就是把出现类似"省","市","区","街","路","里","幢","村","室","园","苑","巷","号"的都在其前面和后面都加上空格。根据他给的一个参考文档做了一下,第一次做的虽然实现了功能,但是代码比较累赘,之后看了一下String类的一些方法,发现有一个replace(str,str)方法可以很快替换掉原来字符串中的字符,就对原来的方法进行了修改,代码显得少了很多。
CSV文件简介:
Comma Separated Values
,简称
CSV,即
逗号分隔值
,是一种纯文本格式,用来存储数据。在
CSV
中,数据的字段由逗号分开。
CSV
文件是一个计算机数据文件用于执行审判和真正的组织工具,逗号分隔的清单。常常被用于移动表格数据之间的两个不同的计算机程序,例如关系数据库程序和电子表格程序。建议用记事本新建一个文件然后修改后缀名,也建议用记事本打开CSV文件。用Excel打开,有时候会报错。
下面是最开始写的比较累赘的代码:
利用String类的replace()方法之后的代码简化为;package test ;
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 OperateCSVfile {public static void main ( String [] args ){String [] str = { "省" , "市" , "区" , "街" , "路" , "里" , "幢" , "村" , "室" , "园" , "苑" , "巷" , "号" };File inFile = new File ( "C://in.csv" ); // 读取的CSV文件File outFile = new File("C://out.csv");//写出的CSV文件String inString = "";String tmpString = "";try {BufferedReader reader = new BufferedReader(new FileReader(inFile));BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));while((inString = reader.readLine())!= null){char [] c = inString.toCharArray();String [] value = new String[c.length];String result = "";for(int i = 0;i < c.length;i++){value[i] = String.valueOf(c[i]);for(int j = 0;j < str.length;j++){if(value[i].equals(str[j])){String tmp = value[i];value[i] = "," + tmp + ",";}}result += value[i];}writer.write(inString);writer.newLine();}reader.close();writer.close();} catch (FileNotFoundException ex) {System.out.println("没找到文件!");} catch (IOException ex) {System.out.println("读写文件出错!");}}}
效果图;package test ;
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 OperateCSVfile {public static void main ( String [] args ){String [] str = { "省" , "市" , "区" , "街" , "路" , "里" , "幢" , "村" , "室" , "园" , "苑" , "巷" , "号" };File inFile = new File ( "C://in.csv" ); // 读取的CSV文件File outFile = new File("C://out.csv");//写出的CSV文件String inString = "";String tmpString = "";try {BufferedReader reader = new BufferedReader(new FileReader(inFile));BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));while((inString = reader.readLine())!= null){for(int i = 0;i<str.length;i++){tmpString = inString.replace(str[i], "," + str[i] + ",");inString = tmpString;}
writer . write ( inString );writer . newLine ();}reader . close ();writer . close ();} catch ( FileNotFoundException ex ) {System . out . println ( "没找到文件!" );} catch ( IOException ex ) {System . out . println ( "读写文件出错!" );}}}
之后我又在网上查了一下资料,发现java有专门操作CSV文件的类和方法。java开源框架csvreader提供了一个轻量级的、简单方便的统一操作接口可用。要使用CsvReader,CsvWriter需要下载一个javacsv.jar导入到项目中才行,在项目上点击右键--属性--库--添加jar文件,选择javacsv.jar文件即可,然后在程序中用import com.csvreader.CsvReader,import com.csvreader.CsvWriter导入即可。
代码如下:
代码如下:
得到同样的效果。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();}}}