import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.*;
public class FileUtils {
public static void main(String args[]){
// formatFile("D:\\home\\fomat\\quanlianghuankuanjihua");
formatFile(" D:\\home\\fomat\\quanliangshihuan");
}
public static String formatFile(String path) {
File file = new File(path);
String filePath = "";
FileOutputStream fos = null;
BufferedReader reader = null;
// 如果这个路径是文件夹
if (file.isDirectory()) {
// 获取路径下的所有文件
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
// 如果还是文件夹 递归获取里面的文件 文件夹
if (files[i].isDirectory()) {
System.out.println("目录:" + files[i].getPath());
filePath = formatFile(files[i].getPath());
} else {
System.out.println("文件:" + files[i].getPath());
filePath = files[i].getPath();
}
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
String outFileName = files[i].getPath().split("\\.")[0].concat("tmp.").concat("csv");
System.out.println("outFileName:" + outFileName);
fos = new FileOutputStream(outFileName);
while (true) {
String str = reader.readLine();
if(StringUtils.isBlank(str)){
break;
}
str = str.replace("\"", "");
str = str.replace("/", "-");
str = str.replace("GSP", "");
fos.write(str.concat("\n").getBytes("UTF-8"));
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(reader);
IOUtils.closeQuietly(fos);
}
}
} else {
System.out.println("文件:" + file.getPath());
}
return filePath;
}
}