Reputation: 59
I have written a java app for a makefile. The app crawls through the files in a folder and read them one by one to get the include files. But it doesnot reports all the files. The app is:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class testCrawler {
/**
* @param args
*/
static String path = "C:\\APP_Eclipse\\TestCrawler\\Test";
static Scanner sc;
static FileReader fr;
static BufferedReader br;
static FileWriter fw;
static BufferedWriter bw;
static String make = "makefile";
static String line;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File f = new File(path);
folderCrawler(f);
}
static public void folderCrawler(File f) throws IOException{
File[] files = f.listFiles();
String name;
String[] names;
fw = new FileWriter(make);
bw = new BufferedWriter(fw);
for(File aFile:files){
name = aFile.getName().toLowerCase();
names = name.split("\\.");
bw.write(names[0] + ".o : " + name);
if ((name.endsWith(".c") || (name.endsWith(".h")))){
try{
fr = new FileReader(path + "\\" + name);
br = new BufferedReader(new FileReader(path + "\\" + name));
while((line=br.readLine()) != null){
line = line.trim();
if (line.contains("#include") && line.contains("\"")){
line = line.replace("#include", "");
line = line.replace("\"", "");
line = line.trim();
System.out.println("LINE : " + line);
bw.write(" " + line + " ");
makeMakefile(line);
}
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
bw.write("\n");
}
}
br.close();
bw.close();
}
static public void makeMakefile(String name) throws Exception{
br = new BufferedReader(new FileReader(path + "\\" + name));
while((line=br.readLine()) != null){
if (line.contains("#include") && line.contains("\"")){
line = line.replace("#include", "");
line = line.replace("\"", "");
line = line.trim();
System.out.println("LINE : " + line);
bw.write(" " + line + " ");
makeMakefile(line);
}
}
}
}
The files that are read: test.c containing :
#include "test.h"
#include "test_1.h"
Test.h containing :
#include "test_2.h"
#include "test_3.h"
and the files test_1.h, test_2.h and test_3.h (these files contain nothing).
The make file should be :
test.o : test.c test.h test_1.h
test.o : test.h test_2.h test_3.h
test_1.o : test_1.h
test_2.o : test_2.h
test_3.o : test_3.h
But is :
test.o : test.c test.h
test.o : test.h test_2.h
test_1.o : test_1.h
test_2.o : test_2.h
test_3.o : test_3.h
I know where the error comes from : line=br.readLine() become null when return from makeMakefile and I can not read the net line in file.
How can to avoid this ?
Thanks a lot
EB
Upvotes: 1
Views: 78
Reputation: 14709
You're redefining your BufferedReader
when you call makeMakeFile()
, and you're passing the line read from your C file, as opposed to the filename. So, it will always work when you only have one #include
, but it won't work after that, because br
now points to a file cooresponding to the line from your C code, which probably doesn't exist, but you're throwing a FileNotFoundException
and handling it in your main.
Additionally, why are you calling the code in your main and immediately afterwards in a another function?
Upvotes: 1