早上好 -
我有一个小问题,我觉得我没有正确理解。我完成了作业,但没有完成教授要求的确切结构。
在其中我们应该使用缓冲读取器读取清单文件,然后我们将读取清单文件中每个文件的内容。
EX- Manifest.txt
File1.txt
File2.txt
FILE1.TXT
I Like
FILE2.TXT
Cats.
这是我的代码 - 我仍在努力理解我在程序中编写的所有内容,并在提交之前将其清理干净。这更让我可以回顾和理解我在做什么。
我的解决方案是创建一个新文件并使用扫描仪读取该文件的内容。代码模板中的部分说明是提交两次使用相同的缓冲读取器来读取清单文件中的文件1和文件2。我尝试将名称重新分配为行值,然后让扫描仪读取该值。这会产生错误:
name=line;
Scanner readThis = new Scanner(name);
out.println(readThis);
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
我想就如何使用缓冲读取器第二次读取清单文件中的每个文件提供一些指导。
public static void main(String[] args) {
System.out.println("input manifest file name");
Scanner in = new Scanner(System.in);
String name = in.nextLine();
// create reader and writer object
//reads the file name inside of manifest
try (BufferedReader input = new BufferedReader(new FileReader(name));
//writes a new file with all of the informaiton
PrintWriter out = new PrintWriter(new FileWriter(name+".out"))) {
String line;
//reading the manifest file - string line reads each line of text
while((line = input.readLine()) != null) {
//for current line of text
//each line in manifest text is a separate input
//this is where i need to take the original buffered reader and read that line input
//line is now the name of the current text file
//read that text file?
name=line;
//each file line is the name, so we can create a new file
File inFile = new File(name);
//the scanner reads the file
Scanner readThis = new Scanner(inFile);
//return true since there is text in the file
while (readThis.hasNextLine()) {
//print the text to the out put file
out.println(readThis.nextLine());
}
}
**// for each file name, use the same reader to read the content of the file, and write to the output file.**
// don't forget to flush the writer in the end
out.flush();
}
catch (IOException e) {
System.out.println("A file that does not exist or is unavailable has been input");
}
in.close();
}
}
感谢所有人的帮助!