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