взять в разных файлах - PullRequest
0 голосов
/ 11 июня 2011

Привет, я пишу программу, которая принимает текстовый файл и проходит через него, и если он находит сообщение #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"

вот как он у другого текстового файла: --- <- это другой текстовый файл, а не на той же странице, извините, я не знал, как показать это здесь </p>

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

Ответы [ 3 ]

2 голосов
/ 11 июня 2011

Основной алгоритм будет:

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 голосов
/ 13 июня 2011

Сибгхатук,

Я собираюсь предположить, что ваша домашняя работа была сдана, поэтому "безопасно" просто передать вам "ответ".

Я бы сделал это примерно так:

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

Я думаю, что вышеупомянутое является "достаточно элегантным" решением проблемы ... даже если я сам так говорю; -)

Обратите внимание, что метод hashInclude рекурсивно вызывает себя ... рекурсия поддается естественному следованию "произвольной древовидной структуре" ... то есть дереву, чья точная структура непостижима при написании программного обеспечения ... поэтому «рекурсия» - это первое, что приходит в голову многим программистам, когда они здесь произносят слово «дерево».

Обратите внимание, что в приведенном выше коде реализована значительно упрощенная версия механизма препроцессора C #include ... но его можно (довольно легко) расширить в "правильный препроцессор" ... который даже (рекурсивно) расширяет # определяет.

Приветствия. Кит.

0 голосов
/ 11 июня 2011

Ваш вопрос немного запутан, но, похоже, вам нужно выяснить, как использовать некоторую рекурсию здесь.

Вам просто нужен метод, который при нахождении директивы "#GetFile" затем захватит файлимя, чтобы получить и вызвать метод снова с этим именем.

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

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

... или что-то в этом роде

...