Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.net.URISyntaxException;
- import java.util.Scanner;
- public class Cleaner
- {
- static File input = null;
- static File output = null;
- static File config = null;
- public static void main(String[] args)
- {
- //Find config file
- java.net.URL configPath = ClassLoader.getSystemResource("config.txt");
- if(configPath==null)
- {
- System.out.println("The config file was not valid try again");
- }
- try {
- config = new File(configPath.toURI());
- } catch (URISyntaxException e1) {
- e1.printStackTrace();
- }
- //Find output file
- java.net.URL outputPath = ClassLoader.getSystemResource("output.txt");
- if(outputPath==null)
- {
- System.out.println("The output file was not valid try again");
- }
- try {
- output = new File(outputPath.toURI());
- } catch (URISyntaxException e1) {
- //cannot find output file;
- e1.printStackTrace();
- }
- //Find input file
- java.net.URL inputPath = ClassLoader.getSystemResource("input.txt");
- if(inputPath==null)
- {
- System.out.println("The input file was not valid try again");
- }
- try {
- input = new File(inputPath.toURI());
- } catch (URISyntaxException e1) {
- e1.printStackTrace();
- }
- //Run the scanner
- try {
- Cleaner(input, output, config);
- } catch (IOException e) {
- //cannot find the output file
- }
- }
- public static void Cleaner(File input, File output, File conFig) throws IOException
- {
- //Create scanners and writers
- Scanner inFile = new Scanner(input);
- Scanner inConfigFile = new Scanner(config);
- BufferedWriter out = new BufferedWriter(new FileWriter(output));
- System.out.println("Processing");
- String Line = null;
- while(inFile.hasNextLine())
- {
- StringBuilder outputLine = new StringBuilder();
- Line = inFile.nextLine();
- //remove parentheses and brackets
- Line = Line.replaceAll("\\([^\\(]*\\)", "");
- Line = Line.replaceAll("\\[[^\\[]*\\]", "");
- char []LineAsChars = Line.toCharArray();
- //Remove numbers from the beginning, when done stop.
- for(int i = 0; i < LineAsChars.length ;i++)
- {
- if(LineAsChars[i] == 45)
- {
- LineAsChars[i] = 32;
- }
- else if(DeleteChar(LineAsChars[i]))
- {
- LineAsChars[i] = 32;
- }
- else
- {
- break;
- }
- //System.out.println(outputLine.append(LineAsChars));
- }
- outputLine.append(LineAsChars);
- //normalization Yes this is horrible insane regex code, if you can improve it that would be great.
- String outputString = outputLine.toString().trim();
- //remove leading dash and trailing \
- if(outputString.length()> 0)
- {
- if(outputString.substring(0,1).equals("-"))
- {
- outputString = outputString.substring(2);
- }
- if(outputString.substring(outputString.length()-1,outputString.length()).equals("\\"))
- {
- outputString = outputString.substring(0,outputString.length()-1);
- }
- if(outputString.contains("|"))
- {
- outputString = outputString.substring(0, outputString.indexOf("|") - 1);
- }
- if(!outputString.contains("-"))
- {
- outputString = " ";
- }
- }
- outputString = outputString.trim();
- //Do replacements from the config file which is setup like this: XXXX$YYYYY (you want to replace XXXX with YYYY
- while(inConfigFile.hasNextLine())
- {
- String configLine = inConfigFile.nextLine();
- String[] theTwo = configLine.split("\\$");
- String ReplaceA = theTwo[0];
- String ReplaceB = theTwo[1];
- //replace it!
- outputString = outputString.replaceAll(ReplaceA,ReplaceB);
- }
- outputString = outputString.trim();
- //Print the cleaned line
- try{
- out.write(outputString);
- out.newLine();
- //Close the output stream
- }catch (Exception e){//Catch exception if any
- System.err.println("Error: " + e.getMessage());
- } System.out.println(outputString);
- }
- System.out.println("Done");
- out.close();
- }
- //removes the number and other stuff at the beginning of lines.
- public static Boolean DeleteChar (char valueIn)
- {
- if(valueIn >=48 && valueIn <=57)
- {
- return true;
- }
- //if valueIn== ) || valueIn == - || valueIn == .
- if(valueIn== 41|| valueIn == 45|| valueIn == 46)
- {
- return true;
- }
- if(valueIn >=65 &&valueIn <=90 ||valueIn >=97 &&valueIn <=122)
- {
- //this line is done
- return false;
- }
- else
- {
- return false;
- }
- }
- public static class writer {
- public static void log(String message) throws IOException {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment