首页 技术 正文
技术 2022年11月19日
0 收藏 545 点赞 2,479 浏览 3711 个字

使用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); }
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898