下面是最开始写的比较累赘的代码:
利用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("读写文件出错!");}}}
得到同样的效果。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();}}}