java读取csv文件和写csv示例分享

时间:2022-06-02 01:08:17

代码如下:


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

 

public class csv{
 public static void main(String[] args) throws IOException {
  File outfile = new File("D://new_car.csv");//存储到新文件的路径
  try {
   InputStreamReader isr = new InputStreamReader(new FileInputStream("D://car.csv"));//待处理数据的文件路径
   BufferedReader reader = new BufferedReader(isr);
   BufferedWriter bw = new BufferedWriter(new FileWriter(outfile));
   String line = null;
   while((line=reader.readLine())!=null){
                String item[] = line.split(",");
                /*
                *信息处理块
                */  
                bw.newLine();//新起一行
                bw.write(""+","+"");//写到新文件中
            }
   bw.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    }
}