public String runMsg(String fileName, File Path, int option)
{
int x = 0;
String name = " ", ret = "";
if (option == 1) {
x = fileName.indexOf(".c");
name = fileName.substring(0, x);
command = name + ".exe < C:\\iptest\\input.txt > C:\\outtest\\" + name + ".txt";
} else if (option == 2) {
x = fileName.indexOf(".cpp");
name = fileName.substring(0, x);
command = name + ".exe < C:\\iptest\\input.txt > C:\\outtest\\" + name + ".txt";
} else {
x = fileName.indexOf(".java");
name = fileName.substring(0, x);
command = "java " + name + " < C:\\iptest\\input.txt > C:\\outtest\\" + name + ".txt";
}
String output = executeCommand(command, Path);
}
private String executeCommand(String command, File Path)
{
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command, null, Path);
p.waitFor();
BufferedReader reader1 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader reader2 = new BufferedReader(new InputStreamReader(p.getInputStream()));
// BufferedReader reader3 = new BufferedReader(new InputStreamReader(p.getOutputStream()));
String line = "";
while ((line = reader1.readLine()) != null) {
output.append(line + "\n");
}
while ((line = reader2.readLine()) != null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
I am trying to execute a .class file from another java file. The .java file will take input from a txt file located at C:\iptest\input.txt and will produce an output file at the following location C:\outputtest\out.txt but when I run this application nothing happens and the application goes into some kind of infinite loop.
Related
Im working on converter. I load file, start reading date from it and creating directories by year, month and day(+ another one dir) in witch ends are those converted text files. Everything is fine while creating those directories but in text files is nothing or only chunk of it.
public static void convertFile(File fileToConvert, File whereToSave, long shift) {
BufferedReader reader = null;
BufferedWriter writer = null;
String oldDate = "";
String newDate = "";
boolean boolDate = true;
try {
for (File file : fileToConvert.listFiles()) {
reader = new BufferedReader(new FileReader(file));
boolean block = true;
String line = "";
int lineCounter = 0;
while ((line = reader.readLine()) != null) {
if (lineCounter==0) {
block = true;
} else {
block = false;
}
line = line.replaceAll("[^0-9-,:+NaN.]", "");
String[] data = line.split(",");
if (block) {
data[0] = data[0].substring(0, 10) + " " + data[0].substring(10);
data[0] = SimulatorForRealData.timeShift(data[0], shift);
// ====================================================================================
newDate = data[0].substring(0, 4) + " " + data[0].substring(5, 7) + " "
+ data[0].substring(8, 10);
String savingIn = SimulatorForRealData.createDirs(whereToSave.toString(),
data[0].substring(0, 4), data[0].substring(5, 7), data[0].substring(8, 10));
File f = new File(savingIn + "\\" + FILE_NAME + ".log");
if (!newDate.equals(oldDate) && boolDate == false) {
writer.close();
boolDate = true;
} else {
oldDate = newDate;
boolDate = false;
}
writer = new BufferedWriter(new FileWriter(f));
// =====================================================================================
writer.write("<in date=\"" + data[0].substring(0, 10) + "T" + data[0].substring(11)
+ "\" t=\"1\" >\n");
writer.write(data[0] + "\n");
writer.write(0 + " " + 0 + " " + 0 + "\n");
for (int x = 0; x <= 10; x++) {
writer.write("NaN" + " ");
}
writer.write("\n");
for (String s : data) {
if (s.equals(data[0])) {
continue;
}
writer.write(s + ";");
}
writer.write("\n");
} else {
for (String s : data) {
writer.write(s + ";");
}
writer.write("\n");
}
lineCounter++;
if (lineCounter == 118) {
lineCounter = 0;
writer.write("</in>\n\n");
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is method where i perform it. Can someone help me. I tried different "writers" and nothing. I have suspicious that it will be problem in closing file but i dont know for sure.
I think you should close every writer you created, not only last one.
for (File file : fileToConvert.listFiles()) {
reader = new BufferedReader(new FileReader(file));
...
writer = new BufferedWriter(new FileWriter(f));
while ((line = reader.readLine()) != null) {
....
}
writer.close();
}
writer flushes all changes on disk only when buffer is overflowed or it is closed.
I see two main problems:
You create a file every time you read a line. You should put it outside the loop (or loops, if you want only one file)
Always data is written with the same filename. It should have different filenames if you make a file for every file read.
I have a simple python program (hw1.py) as follows:
x = int(input("Enter x value: "))
y = int(input("Enter y value: "))
print("x is " + str(x))
print("y is " + str(y))
print("Output is " + str(x+y))
When I run it from terminal, I get following output as expected:
Enter x value: 10
Enter y value: 20
x is 10
y is 20
Output is 30
However, when I run it from Java with providing inputs ("10" and "20") I get slightly different output. Here is the Java code to compile and run python file:
String osName = System.getProperty("os.name").toLowerCase();
boolean isMacOs = osName.startsWith("mac os x");
String macPythonPath = "/Library/Frameworks/Python.framework/Versions/3.6/bin/python3";
String unixPythonPath = "/usr/bin/python3";
Process p;
if (isMacOs) {
p = Runtime.getRuntime().exec(macPythonPath + " -m py_compile " + "/Users/inanc/Desktop/pythonDocs/hw1.py");
} else {
p = Runtime.getRuntime().exec(unixPythonPath + " -m py_compile " + "/Users/inanc/Desktop/pythonDocs/hw31.py");
}
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
String resError = "", s;
// read any errors from the attempted command
while ((s = stdError.readLine()) != null) {
resError = resError + s + "\n";
}
resError = resError.trim();
stdError.close();
if (resError.equals("")) {
if (isMacOs) {
p = Runtime.getRuntime().exec(macPythonPath + " " + "/Users/inanc/Desktop/pythonDocs/hw1.py");
} else {
p = Runtime.getRuntime().exec(unixPythonPath + " " + "/Users/inanc/Desktop/pythonDocs/hw1.py");
}
String[] inputs = {"10", "20"};
OutputStream out = p.getOutputStream();
for (String input: inputs) {
if (input.equals("") == false)
out.write((input+"\n").getBytes());
}
out.flush();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
String res = "";
s = null;
while ((s = stdInput.readLine()) != null) {
res = res + s + "\n" ;
}
res = res.trim();
resError = "";
// read any errors from the attempted command
while ((s = stdError.readLine()) != null) {
resError = resError + s + "\n";
}
resError = resError.trim();
stdInput.close();
stdError.close();
p = null;
System.out.println(res);
} else {
System.err.println(resError);
}
After calling this code, the output is as follows:
Enter x value: Enter y value: x is 10
y is 20
Output is 30
How can I get the exactly same output with terminal execution? At least I want to keep the newlines after entering inputs.
Then there is a way only when you make your program such that argument 10 is written to process, then next line is read from the process and then 20 is written to process, and the another line is read from the process,so on
Whereas, in your code, you are writing all the inputs the process needs in a single loop
I solved the problem, here is the solution:
String osName = System.getProperty("os.name").toLowerCase();
boolean isMacOs = osName.startsWith("mac os x");
String macPythonPath = "/Library/Frameworks/Python.framework/Versions/3.6/bin/python3";
String unixPythonPath = "/usr/bin/python3";
String filepath = "/Users/inanc/Desktop/pythonDocs/hw1additionalInputs.py ";
Process p;
if (isMacOs){
p = Runtime.getRuntime().exec(macPythonPath + " -m py_compile " + filepath);
}
else{
p = Runtime.getRuntime().exec(unixPythonPath + " -m py_compile " + filepath);
}
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
String resError = "", s;
// read any errors from the attempted command
while ((s = stdError.readLine()) != null) {
resError = resError + s + "\n";
}
resError = resError.trim();
stdError.close();
if(resError.equals("")) {
if (isMacOs){
p = Runtime.getRuntime().exec(macPythonPath + " " + filepath);
}
else{
p = Runtime.getRuntime().exec(unixPythonPath + " " + filepath);
}
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
String res = "";
s = null;
String[] inputs = {"10", "20"};
OutputStream out = p.getOutputStream();
for(String input:inputs){
if(input.equals("") == false)
{
Thread.sleep(200);
while (stdInput.ready() && (s = "" + (char) stdInput.read()) != null) {
res = res + s ;
}
// read any errors from the attempted command
Thread.sleep(200);
while (stdError.ready() && (s = "" + (char) stdError.read()) != null) {
resError = resError + s;
}
if(resError.equals("") == false)
break;
out.write((input+"\n").getBytes());
out.flush();
res = res + input + "\n";
Thread.sleep(200);
while (stdInput.ready() && (s = "" + (char) stdInput.read()) != null) {
res = res + s ;
}
//res = res.trim();
// read any errors from the attempted command
Thread.sleep(200);
while (stdError.ready() && (s = "" + (char) stdError.read()) != null) {
resError = resError + s;
}
if(resError.equals("") == false)
break;
}
}
Thread.sleep(200);
while (stdInput.ready() && (s = "" + (char) stdInput.read()) != null) {
res = res + s ;
}
//res = res.trim();
// this part is for in case of no inputs
Thread.sleep(200);
while (stdError.ready() && (s = "" + (char) stdError.read()) != null) {
resError = resError + s;
}
stdInput.close();
stdError.close();
if(p.isAlive())
{
System.err.println("Timeout");
p.destroy();
}
p = null;
System.out.println(res);
System.out.println(resError);
}
else {
System.err.println(resError);
}
I want to change the score of the user, but instead of overwriting the file with new scores, it just makes an empty file.
The lines are written like this: "username-password-wins-losses-ties"
try {
BufferedReader br = new BufferedReader(new FileReader("users.txt"));
PrintWriter pw = new PrintWriter(new FileWriter("users.txt"));
String[] tab = null;
String zlepek = "";
while(br.readLine()!=null) {
String line = br.readLine();
tab = line.split("-");
int countLoss = Integer.parseInt(tab[3]+plusLoss);
int countWin = Integer.parseInt(tab[2]+plusWin);
int countTie = Integer.parseInt(tab[4]+plusTie);
if(tab[0].equals(loginUser.user)) {
String newScore = (tab[0] + "-" + tab[1] + "-" + countWin + "-" + countLoss + "-" + countTie + "\n");
zlepek = zlepek+newScore;
} else {
String oldScore = (tab[0] + "-" + tab[1] + "-" + tab[2] + "-" + tab[3] + "-" + tab[4] + "\n");
zlepek = zlepek+oldScore;
}
}
pw.print(zlepek);
pw.close();
br.close();
plusWin = 0;
plusLoss = 0;
plusTie = 0;
} catch(IOException e) {}
Any help is appreciated.
You are skipping the first line and every odd lines, change the code like below
String line = null;
while ((line = br.readLine()) != null){
// code
}
also you are reading and writing on a same file, you can't read and write simultaneously on a same file. If you want you can write it to a temporary file and rename it later.
If you want write on the same file, you have to close the reader before writing, see below
BufferedReader br = new BufferedReader(new FileReader("users.txt"));
String line = null;
StringBuilder sb = new StringBuilder();
String zlepek = "";
while ((line = br.readLine()) != null) {
zlepek = // logic to get value
sb.append(zlepek).append("\n");
}
br.close();
PrintWriter pw = new PrintWriter(new FileWriter("users.txt")); // pass true to append
pw.print(sb);
pw.close();
The problem is with this line while(br.readLine()!=null), you read the line but didnt save it, so it doesnt know where it is and the line will always be empty after you read the line and dont save it.
try to remove this String line = br.readLine(); and read it in the while. define the String line; and while( (line = br.readLine()) != null){ // do something}
try {
String[] tab = null;
String line = null;
tab = line.split("-");
int countLoss = Integer.parseInt(tab[3]+plusLoss);
int countWin = Integer.parseInt(tab[2]+plusWin);
int countTie = Integer.parseInt(tab[4]+plusTie);
BufferedReader br = new BufferedReader(new FileReader("users.txt"));
StringBuilder sb = new StringBuilder();
String zlepek = "";
while ((line = br.readLine()) != null) {
if(tab[0].equals(loginUser.user)) {
String newScore = (tab[0] + "-" + tab[1] + "-" + countWin + "-" + countLoss + "-" + countTie + "\n");
zlepek = zlepek+newScore;
} else {
String oldScore = (tab[0] + "-" + tab[1] + "-" + tab[2] + "-" + tab[3] + "-" + tab[4] + "\n");
zlepek = zlepek+oldScore;
}
sb.append(zlepek).append("\n");
}
br.close();
PrintWriter pw = new PrintWriter(new FileWriter("users.txt")); // pass true to append
pw.print(sb);
pw.close();
plusWin = 0;
plusLoss = 0;
plusTie = 0;
} catch(IOException e) {}
}
I am working on an Editor application where I can compile and run c,cpp and Java file.I am developing this application using java programming language.I am developing it in Eclipse.
I am able to create new files(c,cpp and java) on specific locations and also I am able to save file to different-2 locations.
And for execution I am using following methods.
String compileFileCommand = "javac "+fileName;
Process compile_process = new ProcessBuilder(compileFileCommand).redirectErrorStream(true).start();
compile_process.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(compile_process.getInputStream()));
String line=reader.readLine();
while(line!=null) {
System.out.println(line);
line=reader.readLine();
}
My problem is that I am not able to compile files from their corresponding locations.
Always giving Exception
java.io.IOException: error=2, No such file or directory
Please tell me how can I compile and run all c,c++ & java files.
Please also give me any other suggestion for my application.
Edited ..
I have used these two methods for compiling and running.On Compiling it creates a class file in case of Java.But all the time I am getting null from InputStreams(both getErrorStream() and getInputStream()).
void compileJavaFile(String fileName)
{
String compileFileCommand = "javac " + fileName;
try
{
System.out.println("Executing Java File");
Process compileProcess = Runtime.getRuntime().exec(compileFileCommand);
String line = "";
BufferedReader bri = new BufferedReader(new InputStreamReader(compileProcess.getInputStream()));
BufferedReader bre = new BufferedReader(new InputStreamReader(compileProcess.getErrorStream()));
while ((line = bri.readLine()) != null)
{
System.out.println(line);
}
bri.close();
while ((line = bre.readLine()) != null)
{
System.out.println(line);
}
bre.close();
compileProcess.waitFor();
System.out.println("Done.");
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
void runJavaFile(String fileName)
{
String runFileCommand = "java " + fileName.split(".java")[0];
try
{
System.out.println("runFileCommand : " + runFileCommand);
System.out.println("Running Java File");
Process runProcess = Runtime.getRuntime().exec(runFileCommand);
BufferedReader reader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
String line = reader.readLine();
System.out.println("line = " + line);
while (line != null)
{
System.out.println(line);
line = reader.readLine();
}
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
And for C and C++ I am using.
void compileCFile(String fileName)
{
String compileFileCommand = "gcc " + fileName;
resultString = "";
try
{
System.out.println("Compiling C File");
Process processCompile = Runtime.getRuntime().exec(compileFileCommand);
BufferedReader brCompileError = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
String errorCompile = brCompileError.readLine();
if (errorCompile != null)
System.out.println("Error Compiler = " + errorCompile);
resultString += errorCompile +"\n";
BufferedReader brCompileRun = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
String outputCompile = brCompileRun.readLine();
if (outputCompile != null)
System.out.println("Output Compiler = " + outputCompile);
resultString += outputCompile +"\n";
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
void runCFile(String fileName)
{
String runFileCommand = "./" + fileName.split(".c")[0];
try
{
System.out.println("Running C File");
Process processRun = Runtime.getRuntime().exec(runFileCommand);
BufferedReader brRun = new BufferedReader(new InputStreamReader(processRun.getErrorStream()));
String errorRun = brRun.readLine();
if (errorRun != null)
System.out.println("Error Run = " + errorRun);
BufferedReader brResult = new BufferedReader(new InputStreamReader(processRun.getInputStream()));
String outputRun = brResult.readLine();
if (outputRun != null)
System.out.println("Output Run = " + outputRun);
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
void compileCPPFile(String fileName)
{
String compileFileCommand = "g++ " + fileName;
try
{
System.out.println("Compiling CPP File");
Process processCompile = Runtime.getRuntime().exec(compileFileCommand);
BufferedReader brCompileError = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
String errorCompile = brCompileError.readLine();
if (errorCompile != null)
System.out.println("Error Compiler = " + errorCompile);
resultString += errorCompile +"\n";
BufferedReader brCompileRun = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
String outputCompile = brCompileRun.readLine();
if (outputCompile != null)
System.out.println("Output Compiler = " + outputCompile);
resultString += outputCompile +"\n";
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
void runCPPFile(String fileName)
{
String runFileCommand = "./" + fileName.split(".cpp")[0];
try
{
System.out.println("Running CPP File");
Process processRun = Runtime.getRuntime().exec(runFileCommand);
BufferedReader brRun = new BufferedReader(new InputStreamReader(processRun.getErrorStream()));
String errorRun = brRun.readLine();
if (errorRun != null)
System.out.println("Error Run = " + errorRun);
BufferedReader brResult = new BufferedReader(new InputStreamReader(processRun.getInputStream()));
String outputRun = brResult.readLine();
if (outputRun != null)
System.out.println("Output Run = " + outputRun);
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}
In case of C and C++ it show error like
g++: /media/disk/eclipse/\/UniversalIDE/CPP/firstCPP: No such file or directory
Please give me solution for my problems ..
Please try following,
Process p = Runtime.getRuntime().exec(command);
command is a string you pass. in command you can pass "javac Test.java" to compile your java file & just like that you can use other commands.
replace the line
String runFileCommand = "./" + fileName.split(".c")[0];
with
String runFileCommand = "./a.out";
If I have 2 files say ABCD.txt and DEF.txt. I need to check if the String "ABCD" is present in DEF.txt and also the string "DEF" present in ABCD.txt and write the combination to a file.
Totally I have around 15000 files and each file contain nearly 50 - 3000 lines has to be searched. I wrote a piece of code, its working.. but it takes one hour to display the entire list...
Is any better way of performing this? Please suggest me.
public void findCyclicDependency(){
Hashtable<String, String> htFileNameList_1 = new Hashtable<String, String>();
Hashtable<String, String> htCyclicNameList = new Hashtable<String, String>();
FileWriter fwCyclicDepen = null;
PrintWriter outFile = null;
FileInputStream fstream = null;
FileInputStream fstream_1 = null;
DataInputStream in = null;
BufferedReader br = null;
DataInputStream in_1 = null;
BufferedReader br_1 = null;
String strSV_File_CK="";
boolean bFound = false;
File fileToSearch = null;
String strSVFileNameForComparison = "";
String strSVDependencyFileLine = "";
String strSVDFileLineExisting = "";
String strCyclicDependencyOut = "";
try {
File baseInputDirectory = new File(strInputPath);
List<File> baseInputDirListing = FileListing.getFileListing(baseInputDirectory);
// Printing out the filenames for the SodaSystem
for (File swPackage : baseInputDirListing)
{
if (swPackage.isDirectory() && swPackage.getName().endsWith("Plus")) {
List<File> currSwPackageFileListing = FileListing.getFileListing(swPackage);
System.out.println("\n swPackage File --> " + swPackage.getName() );
strCyclicDependencyOut = strOutputPath + "_"+ swPackage.getName() + "_CyclicDependency.xml";
System.out.println("\n strCyclicDependencyOut File --> " + strCyclicDependencyOut );
fwCyclicDepen = new FileWriter(strCyclicDependencyOut);
outFile = new PrintWriter(new BufferedWriter(fwCyclicDepen));
outFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
outFile.write("<CyclicDependencyFile>");
for (File DependentFile : currSwPackageFileListing) {
strSV_File_CK = DependentFile.getName().substring(0, (DependentFile.getName().length() - 4)).trim();
htFileNameList_1.put(strSV_File_CK.toUpperCase(),strSV_File_CK.toUpperCase());
}
for (File DependentFile : currSwPackageFileListing)
{
fstream = new FileInputStream(DependentFile);
// Get the object of DataInputStream
in = new DataInputStream(fstream);
br = new BufferedReader(new InputStreamReader(in));
strSVFileNameForComparison = DependentFile.getName().substring(0, (DependentFile.getName().length() - 4)).trim();
//Read File Line By Line
while ((strSVDependencyFileLine = br.readLine()) != null)
{
bFound = false;
if (strSVDependencyFileLine.toUpperCase().indexOf("INDICES") == -1)
{
//Check the current line matches any of the file name in software package folder
if (htFileNameList_1.contains(strSVDependencyFileLine.trim().toUpperCase())
&& strSVDependencyFileLine.compareTo(strSVFileNameForComparison) != 0)
{
bFound = true;
// Get the file to search
for (File searchFile : currSwPackageFileListing)
{
if((searchFile.getName().substring(0, (searchFile.getName().length() - 4)).trim()).equals(strSVDependencyFileLine))
{
fileToSearch = searchFile;
break;
}
}
// Read the file where the file name is found
fstream_1 = new FileInputStream(fileToSearch);
in_1 = new DataInputStream(fstream_1);
br_1 = new BufferedReader(new InputStreamReader(in_1));
while ((strSVDFileLineExisting = br_1.readLine()) != null)
{
if (strSVDFileLineExisting.toUpperCase().indexOf("EXTRA") == -1)
{
if (htFileNameList_1.contains(strSVDFileLineExisting.trim().toUpperCase()) && bFound
&& strSVDFileLineExisting.compareTo(strSVDependencyFileLine) != 0
&& strSVDFileLineExisting.compareTo(strSVFileNameForComparison) == 0 )
{
if(!htCyclicNameList.containsKey(strSVDependencyFileLine) &&
!htCyclicNameList.containsValue(strSVDFileLineExisting))
{
htCyclicNameList.put(strSVDFileLineExisting,strSVDependencyFileLine);
outFile.write("<CyclicDepedency FileName = \"" + strSVDFileLineExisting + "\""+ " CyclicFileName = \"" +
strSVDependencyFileLine + "\" />");
break;
}
}
}
}
}
}
else
{
bFound = false;
}
}//if current line <>
}// reach each line in the current file
outFile.write("</CyclicDependencyFile>");
}
outFile.flush();
outFile.close();
}
}
catch(Exception e){
e.printStackTrace();
}
}
Thanks
Ramm
There are several problems with your design. The most important one is repeated scanning of the file system. Try the code below.
static public void findCyclicDependency2() {
PrintWriter outFile = null;
Map<String,File> fileNames = new HashMap<String,File>();
Map<String,Set<String>> fileBackward = new HashMap<String,Set<String>>();
Map<String,Set<String>> fileForward = new HashMap<String,Set<String>>();
try {
File baseInputDirectory = new File(strInputPath);
List<File> baseInputDirListing = getFileListing(baseInputDirectory);
// Printing out the filenames for the SodaSystem
for(File swPackage:baseInputDirListing) {
if (! (swPackage.isDirectory()
|| swPackage.getName().endsWith("Plus"))) continue;
System.out.println("Loading file names");
List<File> currSwPackageFileListing = getFileListing(swPackage);
for(File dependentFile:currSwPackageFileListing) {
String name = trimName(dependentFile);
fileNames.put(name,dependentFile);
BufferedReader br = new BufferedReader(new FileReader(dependentFile));
String line;
Set<String> contFor = new HashSet<String>();
Set<String> contBack = new HashSet<String>();
while( (line=br.readLine()) != null ) {
line = line.toUpperCase().trim();
if( line.equals("EXTRA") ) continue;
if( line.equals("INDICES") ) continue;
if( line.equals(name) ) continue;
if( line.compareTo(name) == 1 ) {
contFor.add(line);
} else {
contBack.add(line);
}
}
fileBackward.put(name,contBack);
fileForward.put(name,contFor);
}
String strCyclicDependencyOut = strOutputPath + "_"
+ swPackage.getName() + "_CyclicDependency.xml";
outFile = new PrintWriter(new BufferedWriter(new FileWriter(strCyclicDependencyOut)));
outFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
outFile.write("<CyclicDependencyFile>");
for(Entry<String,Set<String>> entry : fileForward.entrySet()) {
String curr = entry.getKey();
for(String other : entry.getValue()) {
Set<String> otherRefs = fileBackward.get(other);
if( otherRefs == null ) continue;
if( otherRefs.contains(curr) ) {
outFile.write("<CyclicDepedency FileName = \""
+ fileNames.get(curr).getPath()
+ "\""
+ " CyclicFileName = \""
+ fileNames.get(other).getPath()
+ "\" />");
}
}
}
outFile.write("</CyclicDependencyFile>");
outFile.flush();
outFile.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
Lucene comes to my mind.
Maybe it's more efficient to index all files, then query for the file names and use the results to detect your circular dependencies.