Java 接收不同的文件

Java 接收不同的文件,java,methods,java.util.scanner,Java,Methods,Java.util.scanner,您好,我正在编写一个程序,它接收一个文本文件并遍历它,如果它找到消息#GetFile“filename.txt”去获取它,并将其存储在与第一个文本文件相同的arraylist中,但我无法思考这个问题,因为如果一个文件调用另一个文件,而另一个文件调用另一个文件,那么该文件可能会调用另一个文件。我想知道是否可以反复调用一个包含scanner类的方法 This is file one #GetFile "fileSecond.txt" ---------- this is file two

您好,我正在编写一个程序,它接收一个文本文件并遍历它,如果它找到消息#GetFile“filename.txt”去获取它,并将其存储在与第一个文本文件相同的arraylist中,但我无法思考这个问题,因为如果一个文件调用另一个文件,而另一个文件调用另一个文件,那么该文件可能会调用另一个文件。我想知道是否可以反复调用一个包含scanner类的方法

 This is file one 
 #GetFile "fileSecond.txt"

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

这就是不同文本文件的使用方式---您的问题有点混乱,但似乎您需要研究如何在这里使用一些递归

您只需要一个方法,在找到“#GetFile”指令后,该方法将获取要获取的文件名,并使用该名称再次调用该方法

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

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

。。。或者类似的

基本算法是:

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
}
不,我不认为你可以继续重复使用同一台扫描仪来读取不同的文件

干杯。基思。

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);
  }
}
套餐论坛;
导入java.io.File;
导入java.io.FileReader;
导入java.io.BufferedReader;
导入java.io.FileNotFoundException;
导入java.io.IOException;
公共类哈希表包括
{
私有静态最终字符串[]包含路径=
System.getenv(“INCLUDE_PATH”).split(File.pathSeparator);
公共静态void main(字符串…参数){
试一试{
for(字符串文件名:文件名){
hashInclude(文件名);
}
}捕获(例外e){
e、 printStackTrace();
}
}
publicstaticvoidhashinclude(字符串文件名)
抛出FileNotFoundException,IOException
{
BufferedReader reader=新的BufferedReader(新文件读取器(文件名));
试一试{
字符串行=null;
int lineCount=0;
而((line=reader.readLine())!=null){
++行数;
if(第行起始带(“#包括”)){
字符串targetFilename=line.replaceFirst(“^#include[\t]*”,”).trim();
如果(!targetFilename.matches(“^[]$”)
//不是a或“有效的.h”
抛出新IncludeException(targetFilename、lineCount、filename);
//-->valid.h
targetFilename=targetFilename.substring(1,targetFilename.length()-1);
//在INCLUDE_路径中搜索目录以查找targetFilename
for(字符串目录:包含路径){
File targetFile=新文件(dir,targetFilename);/c:/path/to/valid.h
if(targetFile.exists()){

hashInclude(targetFile.getAbsolutePath());//使用递归似乎是一种自然的情况。您有一个loadFile(filename1),它可以调用loadFile(filename2)它不会通过arraylist,因为我想将每个文本文件存储在调用它的地方。所以基本上,你想实现C预处理器#include功能…对吗?是的,这正是我想要实现的,但是使用GetFile@corlettk,我很难想象它是如何工作的。所以我想有两个scanner类吗?你会有一个scanner类和一个scanner变量,但每次调用该方法都有自己的副本,一次打开的文件的副本就有多个。谢谢伙计,在我开始深入阅读并开始阅读overmyhead结构和代码后,我放弃了它。但这看起来很有希望谢谢你!
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);
  }
}