package com.test.demo.mockdemo;
import org.apache.commons.lang.StringUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class ProcessData {
public ListreadFileContent(String filePath) {
BufferedReader br =null;
String line =null;
String[] item;
List list =new ArrayList();
try {
br =new BufferedReader(new FileReader(filePath));
while ((line = br.readLine()) !=null) {
if(!StringUtils.isBlank(line)){
if(line.contains(",")&&line.contains("jinhm,")){
String[] a= line.split(",");
if(a.length>2){
String s="jin";
String s1 = s+new Random().nextInt(100);
a[a.length-1]=s1;
String s2 = s+new Random().nextInt(100);
a[a.length-2]=s2;
}
String b="";
for(int i=0;i
b+=a[i];
if (i!=a.length-1)
b+=",";
}
if (!StringUtils.isBlank(b)){
list.add(b);
}
}
}
}
}catch (FileNotFoundException e1)
{
e1.printStackTrace();
}catch (IOException e1)
{
e1.printStackTrace();
}
return list;
}
public void writeFile(String filePath,List list){
BufferedWriter bw =null;
String content=null;
try {
bw=new BufferedWriter(new FileWriter(filePath));
for(int i=0;i
content=list.get(i);
bw.write(content);
bw.newLine();
}
}catch (IOException e) {
e.printStackTrace();
}finally {
if(bw!=null){
try{
bw.close();
}catch (IOException e){
bw=null;
}
}
}
}
public static void main(String[] args) {
String filePath="/Users/jinhongmei/1/demo.csv";
ProcessData processData=new ProcessData();
List stringList = processData.readFileContent(filePath);
if (stringList==null|| stringList.size()==0)return;
processData.writeFile(filePath,stringList);
}
}
网友评论