使用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. HDU 1724 Ellipse ——Simpson积分

    [题目分析] 一看题目,直接把椭圆积分起来就可以了嘛. 然后发现椭圆比较难积分,还是算了吧. 用Simpson积分硬上. 大概就是用二次函数去拟合面积. [代码] #include <cstdi ...

  2. OS X 下iso刻录U盘(系统安装启动盘)

    1. 查看盘 $diskutil list /dev/disk0 #: TYPE NAME SIZE IDENTIFIER : GUID_partition_scheme *320.1 GB disk ...

  3. [SDOI2008]仪仗队 (欧拉函数)

    题目描述 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是否整齐(如下图 ...

  4. leetcode 319 灯泡问题

    例子:1-9 1的因子1 2       1,2 3        1,,3 4        1,2,4 5        1,5 6        1,2,3,6 7        1,7 8   ...

  5. 如何解决maven archetype加载太慢的方法

    有时候在构建maven项目时,archetype加载不出来,一直在Retrieving archetype...,现象如下: 有两种解决方法: ①点击链接下载:http://files.cnblogs ...

  6. 线段树练习5(codevs 4927)

    题目描述 Description 有n个数和5种操作 add a b c:把区间[a,b]内的所有数都增加c set a b c:把区间[a,b]内的所有数都设为c sum a b:查询区间[a,b] ...

  7. ElasticSearch聚合入门(续)

    主要理解聚合中的terms. 参考:http://www.cnblogs.com/xing901022/p/4947436.html Terms聚合 记录有多少F,多少M { "size&q ...

  8. windows bat语法

    @echo off  表示在这条语句之后,所有执行命令的语句不会显示 setLocal    参考1 rem  注释 :或者::    参考 set /p  等待用户输入 SET /A express ...

  9. Linux c内存泄漏检测

    在Linux下些C语言程序,最大的问题就是没有一个好的编程IDE,当然想kdevelop等工具都相当的强大,但我还是习惯使用kdevelop工具,由于没有一个习惯的编程IDE,内存检测也就成了在Lin ...

  10. 解决树莓派8G的SD卡只能识别3.3G,SD卡扩容

    8GB microSD在Windows下使用Win32 Disk Imager下载映像后,在RPi中只能识别出3.3GB.而本身还有很多容量没有释放出来. 使用sudo raspi-config工具可 ...