题目:
# 笔试题: CSV 文件处理
给定一个 CSV 文件,其内容的展现规则如下:
- 每一行数据包含多个字段,字段间以 [,] 分割。
- 如果字段值不含有 [,] 和 ["] ,直接解析输出。
- 如果字段值内部含有逗号 [,],在在字段值两边加上双引号 ["] 将字段值括起来。
- 如果字段值内部含有双引号 ["],则字段值两边加上双引号 ["] 括起来,同时,将字段值内的一个双引号 ["] 替换为两个双引号 [""],例如: [下棋,"飞"] 在 CSV 文件中被表现为 ["下棋,""飞"""]。
## 处理要求:
读入文件 cvs.txt,根据上述 csv 文件的规则进行解析,重新格式化字段生成输出文件 output.txt
将
第一列转为整形(int)
第二列为字符串型
第三列为字符串型
第四列转为浮点数(float)
第五列转为日期类型(DateTime)
输出文件的字段以制表符 [TAB] 来分割字段,
字符串字段输出时用单引号[']括起来
日期字段显示成 YYYY/MM/DD 的格式
说明:
1、可以假设字段值只包含单行数据,即字段值本身不含有 [回车换行]
2、不能对文件 csv.txt 作任何修改
## 编程要求:
使用任何你熟悉的编程语言编写,时间为 1.5 小时。
-----------------------------------分割线-------------------------------------------------
csv文件名:csv.txt
csv 文件里的内容
1,Jane,"下""棋,""飞""",56.2,1976-8-23
2,Kate,购物,49.6,1979-12-56
3,Jerry,"羽毛球,爬山",55.6,1980-5-26
-----------------------------------分割线-------------------------------------------------
我的答案:
import org.junit.Test;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by malin on 2019/5/16.
*/
public class ReadWrite {
private static Reader reader;
private static BufferedReader br;
/**
* 文件读入方法
*/
public static void readFile(String url) throws FileNotFoundException {
File file = new File(url);
if(!file.exists()){
System.out.println("该路径不存在,未读取文件");
}
reader = new FileReader(file);
br = new BufferedReader(reader);
}
public static boolean writeFile(String content, String url) throws UnsupportedEncodingException, IOException {
File file = new File(url);
if(!file.exists()){
file.createNewFile();
}
FileOutputStream o = null;
o = new FileOutputStream(url,true);
o.write(content.getBytes("UTF-8"));
o.flush();
o.close();
return true;
}
/**
* 核心处理
* @throws IOException
*/
public static String core(String before) throws ParseException {
String [] result = new String[5];
String[] split = before.split(",");
int length = split.length;
result[0] = split[0];
result[1] = "'"+split[1]+"'";
result[3] =split[length-2];
result[4] = dateChange(split[length-1]);
StringBuffer sb = new StringBuffer();
for(int i = 5 ; i<=length ; i++){
if(i!=5){
sb.append(",");
}
sb.append(split[i-3]);
}
result[2] = colunmChange(sb);
return result[0]+"\t"+result[1]+"\t"+result[2]+"\t"+result[3]+"\t"+result[4]+"\n";
}
public static String dateChange(String date) throws ParseException {
SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 =new SimpleDateFormat("YYYY/MM/DD");
Date parse = sdf1.parse(date);
String format = sdf2.format(parse);
return format;
}
public static String colunmChange(StringBuffer s){
String column3 = s.toString();
if(column3.contains("\"")){
column3 = column3.substring(1,column3.length()-1);
column3 = column3.replace("\"\"","\"");
}
return "'"+column3+"'";
}
public static void main(String[] args) throws IOException {
try{
readFile("D:/csv.txt");
String s = null;
while((s = br.readLine()) != null){
String result = core(s);
/* list.add(result);*/
writeFile(result,"D:/output.txt");
}
}catch (Exception e){
System.out.println("异常");
e.printStackTrace();
}finally {
System.out.println("执行结束,关闭输入流");
br.close();
reader.close();
}
}
}
写的仓促,并没有优化