gpt4 book ai didi

java - 获取不同的文件

转载 作者:行者123 更新时间:2023-12-02 08:16:26 24 4
gpt4 key购买 nike

嗨,我正在编写一个程序,它接受一个文本文件并对其进行遍历,如果它找到消息#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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com