2011-06-11 31 views
0

嗨我正在写一个程序,接受一个文本文件,并通过它,如果它发现消息#GetFile“filename.txt”去,并将其存储在与第一个文本文件相同的arraylist但我无法考虑这个问题,因为如果一个文件调用另一个调用另一个文件的文件,并且该文件可能能够调用另一个文件。我想知道是否可以一遍又一遍地调用一个包含扫描器类的方法。采取不同的文件

This is file one 
#GetFile "fileSecond.txt" 

---------- 
this is file two 
#GetFile "fileThird.txt" 
---------- 
this is text file three 
#GetFile "fileOne.txt" 

这是文本文件如何不同拥有它--- < - 是不同的文本文件不是同一个页面对不起,我不知道怎么在这里显示它

import java.io.BufferedReader; 
import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class Project3 
{ 
    public static void main(String[] args) 
    { 
    ArrayList<String> text = new ArrayList<String>(); 
    File dictionaryFile = null; // set default value 
    File dictionaryFileTwo = null; 
    Scanner inputFile = null; // set default value 
    Scanner inputFileTwo = null; 
    // use a try-catch block to handle situations when the file is not present 
    keyboard = new Scanner(System.in); 
    // fileName = keyboard.next(); 
    String fileName = "test1.txt"; 
    try { 
     dictionaryFile = new File(fileName); // declare the file 
     inputFile = new Scanner(dictionaryFile); 
    } catch (Exception e) { 
     // if File object creation failed (such as when file is not there) 
     // then this code gets executed. 
     // print the directory where this program expects to find dictionary 
     System.out.println(System.getProperty("user.dir")); 
     // ensure file exists and is in the correct directory 
     if (!dictionaryFile.exists()) { 
     System.out.println("*** Error *** \n" 
      + "Your text file has the wrong name or is " 
      + "in the wrong directory. \n" 
      + "Aborting program...\n\n"); 
     System.exit(-1); // Terminate the program 
     } 
    }// end catch 

    // while there are words in the input file, add them to the dictionary 
    while (inputFile.hasNext()) { 
     if(inputFile.next().startsWith("#GetFile")){ 
     String filing = inputFile.next(); 
     System.out.println("HEY THIS IS THE FILE THAT I FOUND "+ filing); 
     String fileNameSecond = filing; 
     try { 
      dictionaryFileTwo = new File(filing); // declare the file 
      inputFile = new Scanner(dictionaryFile); 
     }catch (Exception e) { 
      // if File object creation failed (such as when file is not there) 
      // then this code gets executed. 
      // print the directory where this program expects to find dictionary 
      System.out.println(System.getProperty("user.dir")); 
      // ensure file exists and is in the correct directory 
      if (!dictionaryFile.exists()) { 
      System.out.println("*** Error *** \n" 
       + "Your text file has the wrong name or is " 
       + "in the wrong directory. \n" 
       + "Aborting program...\n\n"); 
      System.exit(-1); // Terminate the program 
      } 
     }// end catch 
     } else { 
     text.add(inputFile.nextLine()); 
     } 
    } 
    for(int i =0; i < text.size(); i++){ 
     System.out.println(text.get(i)); 
    } 
    } 
} 
+1

似乎使用递归的自然情况。你有一个loadFile(filename1),它可以调用loadFile(filename2) – 2011-06-11 08:07:44

+0

,它不会通过arraylist,因为我想将每个文本文件存储在它被调用的地方。 – 2011-06-11 08:08:45

+0

所以基本上,你想实现C预处理器#包括功能......是否正确? – corlettk 2011-06-11 08:11:41

回答

0

你的问题有点混乱,但似乎你需要调查如何在这里使用一些递归。

你只需要一个方法,找到“#GetFile”指令然后抓住文件名获取并再次调用该名称的方法。

public void parseFile(String filename) { 
    //readline while not end of file... 

    //is line a #GetFile directive? 
     //parseFile(newFilename) 
} 

...或者类似的东西

2

的基本算法是:

open the output-file 

ExpandIncudes(input-file, output-file) { 
    open input-file 
    while (read line from input) 
    if (line is-a #include) then 
     ExpandIncudes(input-file) 
    else 
     write line to output-file 
    endif 
    next line 
} 

不,我不认为你可以继续重复使用相同的扫描仪读取不同的文件。

干杯。基思。

+0

所以我会有两个扫描仪类? – 2011-06-11 08:19:54

+0

您将拥有一个Scanner类和一个扫描器变量,但每次调用该方法都有自己的副本,您将拥有与一次打开文件一样多的副本。 – 2011-06-11 08:49:56

0

Sibghatuk,

我要假定你的功课一直在流传,所以它的“安全”,只是交给你的“答案”。

我会做这样的事情:

package forums; 

import java.io.File; 
import java.io.FileReader; 
import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

public class HashInclude 
{ 
    private static final String[] INCLUDE_PATH = 
    System.getenv("INCLUDE_PATH").split(File.pathSeparator); 

    public static void main(String... args) { 
    try { 
     for (String filename : filenames) { 
     hashInclude(filename); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 

    public static void hashInclude(String filename) 
    throws FileNotFoundException, IOException 
    { 
    BufferedReader reader = new BufferedReader(new FileReader(filename)); 
    try { 
     String line = null; 
     int lineCount = 0; 
     while ((line=reader.readLine()) != null) { 
     ++lineCount; 
     if (line.startsWith("#include ")) { 
      String targetFilename = line.replaceFirst("^#include[ \t]*", "").trim(); 
      if (!targetFilename.matches("^[<\"][A-z0-9_]+\\.h[\">]$")) 
      // not a <valid.h> or a "valid.h" 
      throw new IncludeException(targetFilename, lineCount, filename); 
      // <valid.h> --> valid.h 
      targetFilename = targetFilename.substring(1, targetFilename.length()-1); 
      // search directories in the INCLUDE_PATH for targetFilename 
      for (String dir : INCLUDE_PATH) { 
      File targetFile = new File(dir, targetFilename); // c:/path/to/valid.h 
      if (targetFile.exists()) { 
       hashInclude(targetFile.getAbsolutePath()); // <<-- recursive call 
       return; 
      } 
      } // next dir 
      throw new FileNotFoundException("File " + targetFilename 
      + " not found in INCLUDE_PATH="+ System.getenv("INCLUDE_PATH")); 
     } else { 
      System.out.println(line); 
     } 
     } // next line 
    } finally { 
     reader.close(); 
    } 
    } 

} 

class IncludeException extends RuntimeException { 
    private static final long serialVersionUID = 0L; 
    public IncludeException(String targetFilename, int lineCount, String filename) { 
    super("Invalid #include: " + targetFilename + " at " + lineCount + " " + filename); 
    } 
} 

我认为上面是一个“reasaonbly优雅的”解决问题的办法......即使我不这样说我自己;-)

请注意,hashInclude方法递归调用自身...递归本身自然适用于遵循“任意树结构”......即在您编写软件时其精确结构不可知的树...因此“递归“是关于当许多程序员头脑中出现”树“这个词时,第一件事情。

请注意,上面的代码实现了C预处理器#include机制的大大简化版本......但是可以(很容易)将其扩展为“合适的预处理器”......甚至可以(递归地)扩展#define语句。

干杯。基思。

+0

感谢队友在我深入阅读后放弃了它,并开始阅读overmyhead结构和代码。但是,这看起来很有前途,可以理解,谢谢! – 2011-07-09 09:10:22