使用JAVA按2个字段对文件中的行进行排序
我在一家印刷公司工作,该公司有许多COBOL程序,我的任务是 将COBOL程序转换为JAVA程序。我在一次转换中遇到了障碍。我需要一个文件,每一行是一个记录,在每一行的数据被阻止 一条线的例子是使用JAVA按2个字段对文件中的行进行排序,java,sorting,Java,Sorting,我在一家印刷公司工作,该公司有许多COBOL程序,我的任务是 将COBOL程序转换为JAVA程序。我在一次转换中遇到了障碍。我需要一个文件,每一行是一个记录,在每一行的数据被阻止 一条线的例子是 60000003448595072410013 FFFFFFFFFFV 80 0001438001000014530020120808060134 我需要按19-23个字符的5位数字对数据进行排序,然后按行上的第一个字符对数据进行排序 BufferedReader input; Buffere
60000003448595072410013 FFFFFFFFFFV 80 0001438001000014530020120808060134
我需要按19-23个字符的5位数字对数据进行排序,然后按行上的第一个字符对数据进行排序
BufferedReader input;
BufferedWriter output;
String[] sort, sorted, style, accountNumber, customerNumber;
String holder;
int lineCount;
int lineCounter() {
int result = 0;
boolean eof = false;
try {
FileReader inputFile = new FileReader("C:\\Users\\cbook\\Desktop\\Chemical\\"
+ "LB26529.fil");
input = new BufferedReader(inputFile);
while (!eof) {
holder = input.readLine();
if (holder == null) {
eof = true;
} else {
result++;
}
}
} catch (IOException e) {
System.out.println("Error - " + e.toString());
}
return result;
}
chemSort(){
lineCount = this.lineCounter();
sort = new String[lineCount];
sorted = new String[lineCount];
style = new String[lineCount];
accountNumber = new String[lineCount];
customerNumber = new String[lineCount];
try {
FileReader inputFile = new FileReader("C:\\Users\\cbook\\Desktop\\Chemical\\"
+ "LB26529.fil");
input = new BufferedReader(inputFile);
for (int i = 0; i < (lineCount + 1); i++) {
holder = input.readLine();
if (holder != null) {
sort[i] = holder;
style[i] = sort[i].substring(0, 1);
customerNumber[i] = sort[i].substring(252, 257);
}
}
} catch (IOException e) {
System.out.println("Error - " + e.toString());
}
}
BufferedReader输入;
缓冲写入输出;
字符串[]排序、排序、样式、accountNumber、customerNumber;
绳夹;
整数行数;
int行计数器(){
int结果=0;
布尔eof=假;
试一试{
FileReader inputFile=new FileReader(“C:\\Users\\cbook\\Desktop\\Chemical\\”
+“LB26529.fil”);
输入=新的BufferedReader(inputFile);
而(!eof){
holder=input.readLine();
if(holder==null){
eof=真;
}否则{
结果++;
}
}
}捕获(IOE异常){
System.out.println(“错误-”+e.toString());
}
返回结果;
}
化学排序(){
lineCount=this.lineCounter();
排序=新字符串[行计数];
排序=新字符串[行计数];
样式=新字符串[行数];
accountNumber=新字符串[lineCount];
customerNumber=新字符串[lineCount];
试一试{
FileReader inputFile=new FileReader(“C:\\Users\\cbook\\Desktop\\Chemical\\”
+“LB26529.fil”);
输入=新的BufferedReader(inputFile);
对于(int i=0;i<(lineCount+1);i++){
holder=input.readLine();
if(holder!=null){
排序[i]=持有者;
样式[i]=排序[i]。子字符串(0,1);
customerNumber[i]=排序[i]。子字符串(252257);
}
}
}捕获(IOE异常){
System.out.println(“错误-”+e.toString());
}
}
这就是我目前所拥有的,我真的不确定从这里走到哪里,甚至不知道这是不是正确的方法
开始对文件进行排序。文件排序后,将存储到另一个文件中并进行处理
再次使用另一个程序进行打印。列表行saslist=new ArrayList();
List<String> linesAsList = new ArrayList<String>();
String line=null;
while(null!=(line=reader.readLine())) linesAsList.add(line);
Collections.sort(linesAsList, new Comparator<String>() {
public int compare(String o1,String o2){
return (o1.substring(18,23)+o1.substring(0,1)).compareTo(o2.substring(18,23)+o2.substring(0,1));
}});
for (String line:linesAsList) System.out.println(line); // or whatever output stream you want
字符串行=null;
而(null!=(line=reader.readLine())linesAsList.add(line);
Collections.sort(linesAsList,newcomparator(){
公共整数比较(字符串o1、字符串o2){
返回(o1.子串(18,23)+o1.子串(0,1)),比较(o2.子串(18,23)+o2.子串(0,1));
}});
对于(字符串行:linesAsList)System.out.println(行);//或者任何您想要的输出流
这部手机的自动更正功能弄乱了我的答案列表行saslist=new ArrayList();
字符串行=null;
而(null!=(line=reader.readLine())linesAsList.add(line);
Collections.sort(linesAsList,newcomparator(){
公共整数比较(字符串o1、字符串o2){
返回(o1.子串(18,23)+o1.子串(0,1)),比较(o2.子串(18,23)+o2.子串(0,1));
}});
对于(字符串行:linesAsList)System.out.println(行);//或者任何您想要的输出流
这部手机的自动更正功能弄乱了我的答案将文件读入数组列表(而不是数组)。使用以下方法:
// to declare the arraylist
ArrayList<String> lines = new ArrayList<String>();
// to add a new line to it (within your reading-lines loop)
lines.add(input.readLine());
(根据需要添加导入和try/catch)将文件读入ArrayList(而不是数组)。使用以下方法:
// to declare the arraylist
ArrayList<String> lines = new ArrayList<String>();
// to add a new line to it (within your reading-lines loop)
lines.add(input.readLine());
(根据需要添加导入和try/catch)此代码将根据大型机排序参数对文件进行排序 将3个参数传递给
Sort
类的main
方法
public class SortParameter {
protected int fieldStartByte;
protected int fieldLength;
protected String fieldType;
protected String sortDirection;
public SortParameter(int fieldStartByte, int fieldLength, String fieldType,
String sortDirection) {
this.fieldStartByte = fieldStartByte;
this.fieldLength = fieldLength;
this.fieldType = fieldType;
this.sortDirection = sortDirection;
}
public int getFieldStartPosition() {
return fieldStartByte - 1;
}
public int getFieldEndPosition() {
return getFieldStartPosition() + fieldLength;
}
public String getFieldType() {
return fieldType;
}
public String getSortDirection() {
return sortDirection;
}
public int getDifference(String a, String b) {
int difference = 0;
if (getFieldType().equals("CH")) {
String as = a.substring(getFieldStartPosition(),
getFieldEndPosition());
String bs = b.substring(getFieldStartPosition(),
getFieldEndPosition());
difference = as.compareTo(bs);
if (getSortDirection().equals("D")) {
difference = -difference;
}
}
return difference;
}
}
19,5,CH,A,1,1,CH,A
SortParameter
类,它保存排序参数的实例。在排序参数字符串中,每组4个参数都有一个实例。这个类是一个基本的getter/setter类,除了getDifference
方法之外。getDifference
方法将一些排序比较器代码引入SortParameter
类,以简化sort
类中的比较器代码
public class SortParameter {
protected int fieldStartByte;
protected int fieldLength;
protected String fieldType;
protected String sortDirection;
public SortParameter(int fieldStartByte, int fieldLength, String fieldType,
String sortDirection) {
this.fieldStartByte = fieldStartByte;
this.fieldLength = fieldLength;
this.fieldType = fieldType;
this.sortDirection = sortDirection;
}
public int getFieldStartPosition() {
return fieldStartByte - 1;
}
public int getFieldEndPosition() {
return getFieldStartPosition() + fieldLength;
}
public String getFieldType() {
return fieldType;
}
public String getSortDirection() {
return sortDirection;
}
public int getDifference(String a, String b) {
int difference = 0;
if (getFieldType().equals("CH")) {
String as = a.substring(getFieldStartPosition(),
getFieldEndPosition());
String bs = b.substring(getFieldStartPosition(),
getFieldEndPosition());
difference = as.compareTo(bs);
if (getSortDirection().equals("D")) {
difference = -difference;
}
}
return difference;
}
}
Sort
类包含读取输入文件、对输入文件排序和写入输出文件的代码。这个类可能需要更多的错误检查
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Sort implements Runnable {
protected List<String> lines;
protected String inputFilePath;
protected String outputFilePath;
protected String sortParameters;
public Sort(String inputFilePath, String outputFilePath,
String sortParameters) {
this.inputFilePath = inputFilePath;
this.outputFilePath = outputFilePath;
this.sortParameters = sortParameters;
}
@Override
public void run() {
List<SortParameter> parameters = parseParameters(sortParameters);
lines = read(inputFilePath);
lines = sort(lines, parameters);
write(outputFilePath, lines);
}
protected List<SortParameter> parseParameters(String sortParameters) {
List<SortParameter> parameters = new ArrayList<SortParameter>();
String[] field = sortParameters.split(",");
for (int i = 0; i < field.length; i += 4) {
SortParameter parameter = new SortParameter(
Integer.parseInt(field[i]), Integer.parseInt(field[i + 1]),
field[i + 2], field[i + 3]);
parameters.add(parameter);
}
return parameters;
}
protected List<String> sort(List<String> lines,
final List<SortParameter> parameters) {
Collections.sort(lines, new Comparator<String>() {
@Override
public int compare(String a, String b) {
for (SortParameter parameter : parameters) {
int difference = parameter.getDifference(a, b);
if (difference != 0) {
return difference;
}
}
return 0;
}
});
return lines;
}
protected List<String> read(String filePath) {
List<String> lines = new ArrayList<String>();
BufferedReader reader = null;
try {
String line;
reader = new BufferedReader(new FileReader(filePath));
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return lines;
}
protected void write(String filePath, List<String> lines) {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(filePath));
for (String line : lines) {
writer.write(line);
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.flush();
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
if (args.length < 3) {
System.err.println("The sort process requires 3 parameters.");
System.err.println(" 1. The input file path.");
System.err.println(" 2. The output file path.");
System.err.print (" 3. The sort parameters in mainframe ");
System.err.println("sort format. Example: 15,5,CH,A");
} else {
new Sort(args[0], args[1], args[2]).run();
}
}
}
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Comparator;
导入java.util.List;
公共类Sort实现了Runnable{
受保护的列表行;
受保护的字符串输入文件路径;
受保护的字符串outputFilePath;
受保护字符串排序参数;
公共排序(字符串inputFilePath、字符串outputFilePath、,
字符串排序参数){
this.inputFilePath=inputFilePath;
this.outputFilePath=outputFilePath;
this.sortParameters=sortParameters;
}
@凌驾
公开募捐{
列表参数=解析参数(sortParameters);
行=读取(inputFilePath);
行=排序(行、参数);
写入(outputFilePath,行);
}
受保护的列表参数(字符串排序参数){
列表参数=新的ArrayList();
字符串[]字段=sortParameters.split(“,”);
对于(int i=0;i