java - 获取不同的文件

标签 java methods java.util.scanner

嗨,我正在编写一个程序,它接受一个文本文件并对其进行遍历,如果它找到消息#GetFile“filename.txt”,则会获取该消息并将其存储在与第一个文本文件相同的数组列表中,但我不是能够思考问题,因为如果一个文件调用另一个文件,而另一个文件又调用另一个文件,并且该文件可能能够调用另一个文件。我想知道是否可以一遍又一遍地调用包含扫描仪类的方法。

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

最佳答案

基本算法是:

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
}

不,我认为您不能继续重复使用同一扫描仪来读取不同的文件。

干杯。基思。

关于java - 获取不同的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6314849/

相关文章:

java - 如何在字符串数组中查找重复的字符串

java - 了解 Dalvik 代码的反汇编?

class - 是否可以在 Visio 2007 中显示方法/操作返回和对象数组(而不仅仅是单个对象)?

java - 如何使用扫描仪在集合中动态添加数据

Java Codelab While 语句

java - Spark 数据集 - NumberFormatException : Zero length BigInteger

iphone - 在 iOS 5 中使用弃用的方法

javascript - 为什么 JavaScript 中的某些方法会复制所调用的对象?

java - 使用java计算求和中的处理构造函数

java - 在自定义 View 中触摸和删除触摸时绘制圆圈