Problem with adding input to file ( using regex )
Problem: trying to read in the lines of the 1300 files i am looping through and parsing them with the regex i have - then saving the new lines into new files. When i try to add the lines into a Set, however, nothing is added. I'll attach a sample of one of the input files and the program as it is now, below. Any help would be greatly appreciated. Thanks.
Sample file:
癹/?惣M?t?C|怄紆鯅嶨垝鶨蔖疢2 0 0 3A D M . j p g? R A S T E R :W o r k s p a c e=L : \ a l b e r t a \ A r c G I S \ s l i d e s \ ;R a s t e r D a t a s e t=2 0 0 3A D M . j p g ;R a s t e rD a t a s e tR a s t e rB a n d 5Zq阊獋 繭? 4L : \ a l b e r t a \ A r c G I S \ s l i d e s \\R a s t e rW o r k s p a c e=L : \ a l b e r t a \ A r c G I S \ s l i d e s \ ;R a s t e rD a t aZ嶺浶?獆 繭? D A T A B A S E 4L : \ a l b e r t a \ A r c G I S \ s l i d e s \c賾L? 鴛52L : \ a l b e r t a \ A r c G I S \ s l i d e s
Program:
import java.io.*;
import java.util.*;
import java.util.regex.*;
publicclass CleanFile{
private File oldFile;
private File newFile;
private String workspacePath;
private String workspaceType;
private String workspaceFile;
private String workspaceDb;
private BufferedReader in;
private PrintWriter out;
privatestatic BufferedReader fn;
private String line;
private String templine;
privatefinal String PATH_REGEX ="workspace.*?;";
private Pattern pathPattern;
private Matcher pathMatcher;
privatefinal String TYPE_REGEX ="[rasterdataset|coverage|featureclass][a-z]*[0-9]*?;";
private Pattern typePattern;
private Matcher typeMatcher;
privatefinal String NAME_REGEX ="";
private Pattern namePattern;
private Matcher nameMatcher;
privatefinal String MDB_REGEX ="l:\\.*?.mdb";
private Pattern mdbPattern;
private Matcher mdbMatcher;
private Set pathSet;
private Set typeSet;
private Set mdbSet;
public CleanFile(String filePath, String newFilePath){
this.oldFile =new File(filePath);
this.newFile =new File(newFilePath);
this.pathSet =new HashSet();
this.typeSet =new HashSet();
this.mdbSet =new HashSet();
pathPattern = Pattern.compile(PATH_REGEX, Pattern.CASE_INSENSITIVE);
typePattern = Pattern.compile(TYPE_REGEX, Pattern.CASE_INSENSITIVE);
namePattern = Pattern.compile(NAME_REGEX, Pattern.CASE_INSENSITIVE);
mdbPattern = Pattern.compile(MDB_REGEX, Pattern.CASE_INSENSITIVE);
try{
this.in =new BufferedReader(new FileReader(oldFile));
this.out =new PrintWriter(new FileWriter(newFile));
}
catch(FileNotFoundException fnfe){
System.out.println("File not found, please enter a " +
"valid file name.");
System.exit(0);
}
catch(IOException ioe){
System.out.println(ioe.getMessage());
}
}
privatevoid findReplaceFormat(){
try{
while((line = in.readLine()) !=null){
templine = line.replaceAll(" ","##").replaceAll(" ","").replaceAll("##"," ");
getWorkspacePath(templine);
getWorkspaceType(templine);
//getWorkspaceName(templine);
getMdb(templine);
}
formatOutput(pathSet, typeSet, mdbSet);
}
catch(IOException ioe){
System.out.println(ioe.getMessage());
}
}
privatevoid getWorkspacePath(String temp){
pathMatcher = pathPattern.matcher(temp);
while(pathMatcher.find()){
workspacePath = pathMatcher.group();
pathSet.add(workspacePath);
}
}
privatevoid getWorkspaceType(String temp){
typeMatcher = typePattern.matcher(temp);
while(typeMatcher.find()){
workspaceType = typeMatcher.group();
typeSet.add(workspaceType);
}
}
privatevoid getMdb(String temp){
mdbMatcher = mdbPattern.matcher(temp);
while(mdbMatcher.find()){
workspaceDb = mdbMatcher.group();
mdbSet.add(workspaceDb);
}
}
privatevoid formatOutput(Set path, Set type, Set mdb){
out.println(pathSet);
out.println(typeSet);
out.println(mdbSet);
}
privatevoid closeFiles(){
try{
in.close();
out.close();
}
catch(IOException ioe){}
}
publicstaticvoid main(String[] args){
CleanFile cleanMe;
String oldFileName;
String newFileName;
try{
fn =new BufferedReader(new FileReader("alberta/list.txt"));
while((oldFileName = fn.readLine()) !=null){
newFileName = oldFileName.replace(".shortcut",".txt");
cleanMe =new CleanFile("alberta/"+oldFileName,"alberta/new/"+newFileName);
cleanMe.findReplaceFormat();
cleanMe.closeFiles();
}
}
catch(FileNotFoundException fnfe){
System.out.println("Cannot find file list.");
System.exit(0);
}
catch(IOException ioe){
System.out.println(ioe.getMessage());
}
}
}

