Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 60000003448595072410013 FFFFFFFFFFV 80 0001438001000014530020120808060134
- 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());
- }
- }
- 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
- // 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());
- Collections.sort(lines, new Comparator<String>() {
- public int compare(String a, String b) {
- String a5 = theFiveNumbersOf(a);
- String b5 = theFiveNumbersOf(b);
- int firstComparison = a5.compareTo(b5);
- if (firstComparison != 0) { return firstComparison; }
- String a1 = theDigitOf(a);
- String b1 = theDigitOf(b);
- return a1.compareTo(b1);
- }
- });
- BufferedWriter ow = new BufferedWriter(new FileOutputStream("filename.extension"));
- for (String line : lines) {
- ow.println(line);
- }
- ow.close();
- 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;
- }
- }
- 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();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement