首页 > 编程 > Java > 正文

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

2019-11-26 15:36:37
字体:
来源:转载
供稿:网友

复制代码 代码如下:

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表