使用IntelliJ Idea 开发的一个Java 处理数据文件折行的问题,整体来说功能比较简单的一个java脚本的开发,跨平台的优势可以处理windows和lunix平台的文件折行

package com.company;
import com.sun.javafx.image.BytePixelSetter; import java.io.BufferedInputStream;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.Reader;
import java.io.FileWriter;
public class Main { public static boolean isNumeric(String str){
for (int i = 0; i < str.length(); i++){
// System.out.println(str.charAt(i));
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
} public static void hrwtFilePorcess(String filePath){
try {
String encoding="UTF8";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read); String preLine=null;
String lineTxt = null;
String nextLineTxt=null;
StringBuffer sb=new StringBuffer(); boolean isTR=false;
int a =0;
while((lineTxt = bufferedReader.readLine()) != null ){ int index =lineTxt.indexOf("|");
String id =lineTxt.substring(0,index);
if(isNumeric(id)==false && preLine != null){
preLine+=lineTxt;
isTR=true;
}else {
isTR=false;
if(a!=0)
sb.append("\r\n");
preLine=lineTxt;
} sb.append(lineTxt);
a++; } read.close();
System.out.println("开始替换");
BufferedWriter out = new BufferedWriter(new FileWriter(filePath));
out.write(sb.toString());
out.flush();
out.close();
System.out.println("替换完成:"+filePath);
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
} } public static void sapFileProcess(String filePath){
try {
String encoding="UTF8";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; StringBuffer sb=new StringBuffer();
boolean isTR=false;
int a =0;
while((lineTxt = bufferedReader.readLine()) != null ) {
//5行之后如果不是\现开头就是折行
if (a >=6) {
int index = lineTxt.indexOf("|");
int index2 =lineTxt.indexOf("-------"); if (index > 1 && index2!=0 ) {
sb.append(lineTxt); } else {
sb.append("\r\n");
sb.append(lineTxt); }
}
else
{ if(a>0){
sb.append("\r\n");
}
sb.append(lineTxt); }
a++;
}
BufferedWriter out = new BufferedWriter(new FileWriter(filePath));
out.write(sb.toString());
out.flush();
out.close();
System.out.println("替换完成:"+filePath);
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
} } public static void main(String[] args) {
// write your code here if(args.length>0){ for (int i=0 ; i<args.length; i++ )
{ System.out.println((i));
try
{ System.out.println(args[i]);
String configPath=args[i];
System.out.println("configPath:"+configPath); File file=new File(configPath);
String encoding="UTF8";
if(file.isFile() && file.exists()) { //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null ){ String[] str =lineTxt.split("\\|");
String filePath=str[1];
String type=str[0];
String sap="sap"; System.out.println("filePath:"+filePath);
System.out.println("type:"+type + ": "+sap+ (type.equals(sap))); if(type.equals(sap)){
System.out.println("sapFileProcess:");
sapFileProcess(filePath);
}else {
System.out.println("hrwtFilePorcess:"); hrwtFilePorcess(filePath);
} } }else { System.out.println("configPath:"+configPath +"; 配置文件不存在 "); }
}
catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
} }
}else
{ System.out.println("请指定配置文件");
} //test
String filePath = "C:\\Users\\miachen\\Desktop\\data\\hrwt\\20170630-193314-CHN_PERSON.TXT";
String sapFile = "C:\\Users\\miachen\\Desktop\\data\\sap\\KSB1_201703_2943.txt"; // sapFileProcess(sapFile); }
}

  

最新文章

  1. JavaScript的全局变量与局部变量解析
  2. JS实现自适应宽度的Tag切换
  3. myeclipse + tomcat 项目自动部署
  4. oracle怎么卸载(转)
  5. 进击的Hybrid App,量身定做缓存机制
  6. inline-block总结
  7. 解决弹出的窗口window.open会被浏览器阻止的问题(自定义open方法)
  8. sublimelinter-jshinter
  9. Python字符串格式符号含义
  10. C语言程序设计(翁恺)--第二周课件中的两个遗留点
  11. JavaScript的内置对象(Date日期+string字符串)基础语法总结
  12. Problem V
  13. 编写ROS程序--HelloROS
  14. Java 类设计技巧
  15. Redis之RDB与AOF
  16. [工具/PC]计算机中丢失libiconv-2.dll,丢失libintl-8.dll,无法定位程序输入点libiconv于动态链接库libiconv-2.dll上问题解决方法
  17. NYOJ 47:过河问题(思维)
  18. poj 2506 Tiling(高精度)
  19. CS299笔记:广义线性模型
  20. libgdx自制简易版Don&#39;t Touch The White Tile

热门文章

  1. BZOJ 1087 [SCOI2005]互不侵犯King ——状压DP
  2. Ubuntu 安装PostgreSQL
  3. 记住密码后,密码框Password会自动带出数据
  4. codevs——2651 孔子教学——同桌
  5. IntelliJ IDEA 使用的问题总结
  6. mac 安装ANT
  7. BUPT复试专题—排序(2009)
  8. Sentinel实现Redis高可用
  9. IOS Object和javaScript相互调用
  10. svn 创建分支、切换分支 及 合并分支 操作