欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-24 09:37:04
复制代码 代码如下:import java.io.bufferedreader;import java.io.bufferedwriter;import java.io.f...

复制代码 代码如下:

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();
  }
    }
}