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

  

JAVA 0 的突破的更多相关文章

  1. MessagePack Java 0.6.X 动态类型

    我们知道 Java 是一个静态类型的语言.通过输入 Value MessagePack能够实现动态的特性. Value 有方法来检查自己的类型(isIntegerType(), isArrayType ...

  2. MessagePack Java 0.6.X 可选字段

    你可添加一个新的字段来保持可用性.在新字段中使用 @Optional 注解. @Message public static class MyMessage {     public String na ...

  3. MessagePack Java 0.6.X 不使用注解(annotations)来序列化

    如果你不能添加 @Message 到你的定义对象中但是你还是希望进行序列化.你可以使用 register 方法来在类中启用序列化对象. 如下的代码所示: MessagePack msgpack = n ...

  4. MessagePack Java 0.6.X List, Map 对象的序列化和反序列化

    为了序列化原生的容器对象例如  List 和 Map 对象,你必须使用 Template. Template 对象是 serializer 和 deserializer 的配对.例如,为了序列化一个  ...

  5. MessagePack Java 0.6.X 多种类型变量的序列化和反序列化(serialization/deserialization)

    类 Packer/Unpacker 允许序列化和反序列化多种类型的变量,如后续程序所示.这个类启用序列化和反序列化多种类型的变量和序列化主要类型变量以及包装类,String 对象,byte[] 对象, ...

  6. MessagePack Java 0.6.X 使用一个消息打包(message-packable)类

    使用注解 @Message 来让你可以序列化你自己类中对象的 public 字段. 本代码可以在 https://github.com/cwiki-us-demo/messagepack-6-demo ...

  7. MessagePack Java 0.6.X 快速开始指南 - 安装

    0.6.x 版本的 MessagePack 已经过期被淘汰了.如果你现在开始使用 MessagePack 话,请不要使用这个版本. 我们再这里保留 0.6.x 版本的内容主要用于参考用途. 最新的 M ...

  8. Java{0}占位符替换字符串

    Java{0}占位符替换字符串 public class Test { public static void main(String[] args) { System.out.println(Stri ...

  9. java 0 开始

    利用了61天的时间学习了 se 不过忘得也很多  .在这里开一个帖子   打算利用几天的时间进行复习,把凡是能用到的都放在这边. 不带图形界面的第一个项目已经弄完 (看视频加看书..而且自己往上面加东 ...

随机推荐

  1. POJ——2251Dungeon Master(三维BFS)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25379   Accepted: 9856 D ...

  2. [SCOI2003]字符串折叠 (区间DP)

    题目描述 折叠的定义如下: 一个字符串可以看成它自身的折叠.记作S = S X(S)是X(X>1)个S连接在一起的串的折叠.记作X(S) = SSSS…S(X个S). 如果A = A’, B = ...

  3. java system.out.printf()的使用方法

    package test; public class Main { public static void main(String[] args) { // 定义一些变量,用来格式化输出. double ...

  4. Java面试题集(一)

    作为一名java开发软件工程,一定要记住,基础非常重要,往往就是一些基础,很简单,但是你就是不知道实现原理,为什么使用,有没有自己去发现,对比,差异从而总结,有些东西看似简单,但是不一定你描述清楚,直 ...

  5. POJ 1182 食物链 [并查集 带权并查集 开拓思路]

    传送门 P - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit  ...

  6. POJ 1502 MPI Maelstrom [最短路 Dijkstra]

    传送门 MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5711   Accepted: 3552 ...

  7. grafana 安装 和 Nginx 、EL 联调

    https://blog.csdn.net/u010735147/article/details/80943593

  8. linux top %VSZ含义

    https://blog.csdn.net/sinohenu/article/details/58216013 https://unix.stackexchange.com/questions/449 ...

  9. saltstack 开发相关命令记录

    SALT API开发相关命令记录. 查看当前的salt key信息salt-key -L 测试被控主机的连通性salt '*' test.ping 远程命令执行测试salt '*' cmd.run ' ...

  10. 真的了解CDN服务吗?

    Im CDN,WOW,COOL CDN知识详解 全称:Content Delivery Network或Content Ddistribute Network,即内容分发网络,CDN是构建在网络之上的 ...