2013-11-24 133 views
1

我已经为makefile编写了一个java应用程序。该应用程序会抓取文件夹中的文件并逐一读取它们以获取包含文件。但它没有报告所有的文件。该应用程序是:阅读链接的文件

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); 
     } 
    }  
} 

} 

被读取的文件: test.c的包含:

#include "test.h" 
#include "test_1.h" 

Test.h包含:

#include "test_2.h" 
#include "test_3.h" 

和文件test_1.h,test_2 .h和test_3.h(这些文件不包含任何内容)。

make文件应该是:

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 

但就是:

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 

我知道那里的错误来源于:行= br.readLine()成为空当从makeMakefile和我的回报无法读取文件中的网线。

如何避免这种情况?

非常感谢

EB

回答

1

你重新定义你的BufferedReader当你打电话makeMakeFile(),和你传递你的C文件中读取行,而不是文件名。所以,只有当你只有一个#include时它才会起作用,但在此之后它将不起作用,因为br现在指向一个文件,与您的C代码中的行保持一致,这可能不存在,但是您正在投掷一个FileNotFoundException并在您的主要处理它。

此外,你为什么要在你的main中调用代码,然后立即在另一个函数中调用代码?

+0

我明白了,但是我怎样才能指向要读取的新文件呢? –

+0

@EB你已经在你的'try'中做了这个。另外,代码中不需要'fr',因为它什么也不做。你正在'BufferedReader'构造函数中创建一个'FileReader'。使用方法很好,但是你称之为方法的方式没有意义。你的方法没有理由递归,使用你当前的main并且再次调用完全相同的代码片段是没有意义的,虽然不正确。 –

+0

P谢谢。我使用递归性,因为它是相同的操作流程,即:读取文件 - 写入它的naem,如果它包含的文件首先打开,读取它 - 写入名称等等。只有当它遇到包含在<>之间的系统文件时才结束。我真的没有看到任何其他方式。但是,也许我错了。 –