Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.82 KB | None | 0 0
  1. 60000003448595072410013 FFFFFFFFFFV 80 0001438001000014530020120808060134
  2.  
  3. BufferedReader input;
  4. BufferedWriter output;
  5.  
  6. String[] sort, sorted, style, accountNumber, customerNumber;
  7. String holder;
  8.  
  9. int lineCount;
  10.  
  11. int lineCounter() {
  12.  
  13. int result = 0;
  14. boolean eof = false;
  15.  
  16. try {
  17. FileReader inputFile = new FileReader("C:\Users\cbook\Desktop\Chemical\"
  18. + "LB26529.fil");
  19. input = new BufferedReader(inputFile);
  20.  
  21. while (!eof) {
  22.  
  23. holder = input.readLine();
  24. if (holder == null) {
  25. eof = true;
  26. } else {
  27. result++;
  28. }
  29. }
  30.  
  31. } catch (IOException e) {
  32. System.out.println("Error - " + e.toString());
  33. }
  34.  
  35. return result;
  36. }
  37.  
  38. chemSort(){
  39. lineCount = this.lineCounter();
  40. sort = new String[lineCount];
  41. sorted = new String[lineCount];
  42. style = new String[lineCount];
  43. accountNumber = new String[lineCount];
  44. customerNumber = new String[lineCount];
  45.  
  46. try {
  47. FileReader inputFile = new FileReader("C:\Users\cbook\Desktop\Chemical\"
  48. + "LB26529.fil");
  49. input = new BufferedReader(inputFile);
  50.  
  51. for (int i = 0; i < (lineCount + 1); i++) {
  52. holder = input.readLine();
  53. if (holder != null) {
  54. sort[i] = holder;
  55. style[i] = sort[i].substring(0, 1);
  56. customerNumber[i] = sort[i].substring(252, 257);
  57. }
  58. }
  59. } catch (IOException e) {
  60. System.out.println("Error - " + e.toString());
  61. }
  62. }
  63.  
  64. List<String> linesAsList = new ArrayList<String>();
  65. String line=null;
  66. while(null!=(line=reader.readLine())) linesAsList.add(line);
  67.  
  68. Collections.sort(linesAsList, new Comparator<String>() {
  69. public int compare(String o1,String o2){
  70. return (o1.substring(18,23)+o1.substring(0,1)).compareTo(o2.substring(18,23)+o2.substring(0,1));
  71. }});
  72.  
  73. for (String line:linesAsList) System.out.println(line); // or whatever output stream you want
  74.  
  75. // to declare the arraylist
  76. ArrayList<String> lines = new ArrayList<String>();
  77.  
  78. // to add a new line to it (within your reading-lines loop)
  79. lines.add(input.readLine());
  80.  
  81. Collections.sort(lines, new Comparator<String>() {
  82. public int compare(String a, String b) {
  83. String a5 = theFiveNumbersOf(a);
  84. String b5 = theFiveNumbersOf(b);
  85. int firstComparison = a5.compareTo(b5);
  86. if (firstComparison != 0) { return firstComparison; }
  87. String a1 = theDigitOf(a);
  88. String b1 = theDigitOf(b);
  89. return a1.compareTo(b1);
  90. }
  91. });
  92.  
  93. BufferedWriter ow = new BufferedWriter(new FileOutputStream("filename.extension"));
  94. for (String line : lines) {
  95. ow.println(line);
  96. }
  97. ow.close();
  98.  
  99. public class SortParameter {
  100.  
  101. protected int fieldStartByte;
  102. protected int fieldLength;
  103. protected String fieldType;
  104. protected String sortDirection;
  105.  
  106. public SortParameter(int fieldStartByte, int fieldLength, String fieldType,
  107. String sortDirection) {
  108. this.fieldStartByte = fieldStartByte;
  109. this.fieldLength = fieldLength;
  110. this.fieldType = fieldType;
  111. this.sortDirection = sortDirection;
  112. }
  113.  
  114. public int getFieldStartPosition() {
  115. return fieldStartByte - 1;
  116. }
  117.  
  118. public int getFieldEndPosition() {
  119. return getFieldStartPosition() + fieldLength;
  120. }
  121.  
  122. public String getFieldType() {
  123. return fieldType;
  124. }
  125.  
  126. public String getSortDirection() {
  127. return sortDirection;
  128. }
  129.  
  130. public int getDifference(String a, String b) {
  131. int difference = 0;
  132.  
  133. if (getFieldType().equals("CH")) {
  134. String as = a.substring(getFieldStartPosition(),
  135. getFieldEndPosition());
  136. String bs = b.substring(getFieldStartPosition(),
  137. getFieldEndPosition());
  138. difference = as.compareTo(bs);
  139. if (getSortDirection().equals("D")) {
  140. difference = -difference;
  141. }
  142. }
  143.  
  144. return difference;
  145. }
  146.  
  147. }
  148.  
  149. import java.io.BufferedReader;
  150. import java.io.BufferedWriter;
  151. import java.io.FileNotFoundException;
  152. import java.io.FileReader;
  153. import java.io.FileWriter;
  154. import java.io.IOException;
  155. import java.util.ArrayList;
  156. import java.util.Collections;
  157. import java.util.Comparator;
  158. import java.util.List;
  159.  
  160. public class Sort implements Runnable {
  161.  
  162. protected List<String> lines;
  163.  
  164. protected String inputFilePath;
  165. protected String outputFilePath;
  166. protected String sortParameters;
  167.  
  168. public Sort(String inputFilePath, String outputFilePath,
  169. String sortParameters) {
  170. this.inputFilePath = inputFilePath;
  171. this.outputFilePath = outputFilePath;
  172. this.sortParameters = sortParameters;
  173. }
  174.  
  175. @Override
  176. public void run() {
  177. List<SortParameter> parameters = parseParameters(sortParameters);
  178. lines = read(inputFilePath);
  179. lines = sort(lines, parameters);
  180. write(outputFilePath, lines);
  181. }
  182.  
  183. protected List<SortParameter> parseParameters(String sortParameters) {
  184. List<SortParameter> parameters = new ArrayList<SortParameter>();
  185. String[] field = sortParameters.split(",");
  186. for (int i = 0; i < field.length; i += 4) {
  187. SortParameter parameter = new SortParameter(
  188. Integer.parseInt(field[i]), Integer.parseInt(field[i + 1]),
  189. field[i + 2], field[i + 3]);
  190. parameters.add(parameter);
  191. }
  192. return parameters;
  193. }
  194.  
  195. protected List<String> sort(List<String> lines,
  196. final List<SortParameter> parameters) {
  197.  
  198. Collections.sort(lines, new Comparator<String>() {
  199. @Override
  200. public int compare(String a, String b) {
  201. for (SortParameter parameter : parameters) {
  202. int difference = parameter.getDifference(a, b);
  203. if (difference != 0) {
  204. return difference;
  205. }
  206. }
  207. return 0;
  208. }
  209. });
  210.  
  211. return lines;
  212. }
  213.  
  214. protected List<String> read(String filePath) {
  215. List<String> lines = new ArrayList<String>();
  216. BufferedReader reader = null;
  217. try {
  218. String line;
  219. reader = new BufferedReader(new FileReader(filePath));
  220. while ((line = reader.readLine()) != null) {
  221. lines.add(line);
  222. }
  223. } catch (FileNotFoundException e) {
  224. e.printStackTrace();
  225. } catch (IOException e) {
  226. e.printStackTrace();
  227. } finally {
  228. try {
  229. if (reader != null) {
  230. reader.close();
  231. }
  232. } catch (IOException e) {
  233. e.printStackTrace();
  234. }
  235. }
  236. return lines;
  237. }
  238.  
  239. protected void write(String filePath, List<String> lines) {
  240. BufferedWriter writer = null;
  241. try {
  242. writer = new BufferedWriter(new FileWriter(filePath));
  243. for (String line : lines) {
  244. writer.write(line);
  245. writer.newLine();
  246. }
  247. } catch (IOException e) {
  248. e.printStackTrace();
  249. } finally {
  250. try {
  251. if (writer != null) {
  252. writer.flush();
  253. writer.close();
  254. }
  255. } catch (IOException e) {
  256. e.printStackTrace();
  257. }
  258. }
  259. }
  260.  
  261. public static void main(String[] args) {
  262. if (args.length < 3) {
  263. System.err.println("The sort process requires 3 parameters.");
  264. System.err.println(" 1. The input file path.");
  265. System.err.println(" 2. The output file path.");
  266. System.err.print (" 3. The sort parameters in mainframe ");
  267. System.err.println("sort format. Example: 15,5,CH,A");
  268. } else {
  269. new Sort(args[0], args[1], args[2]).run();
  270. }
  271. }
  272.  
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement