程序必须打印我要求它阅读的文件。 问题是我将其编码为从特定文件中读取文本
File file = new File("numbersonnumbers.txt");
Scanner inputFile = new Scanner(file);
如何让程序从用户输入指定的文件中读取文本? *该文件将与程序位于同一目录/文件夹中,因此不存在问题。
编辑:我之前尝试过用户输入
Scanner keyboard = new Scanner(System.in);
String filename = keyboard.nextLine();
File file = new File(filename);
Scanner inputFile = new Scanner(file);
答案 0 :(得分:2)
声明String
变量,例如someUserFile
,接受用户输入。然后...
File file = new File(someUserFile);
Scanner inputFile = new Scanner(file);
您可能需要检查其输入并将".txt"
附加到输入的末尾。
如果有效:
File file = new File("numbersonnumbers.txt");
Scanner inputFile = new Scanner(file);
然后这个:
Scanner keyboard = new Scanner(System.in);
String filename = keyboard.nextLine();
File file = new File(filename);
Scanner inputFile = new Scanner(file);
假设您输入numbersonnumbers.txt
并输入完全相同的内容,我会做同样的事情。
假设您完全键入numbersonnumbers.txt
,第二个代码段与第一个代码段完全相同。
答案 1 :(得分:1)
首先,您应该询问用户输入的文件名。然后只需将代码中的“numbersonnumbers.txt”替换为您为用户输入设置的变量。
Scanner in = new Scanner(System.in);
System.out.println("What is the filename?");
String input = in.nextLine();
File file = new File(input);
答案 2 :(得分:0)
Scanner input = new Scanner(System.in);
String fileName = input.nextLine();
File file = new File(fileName);
Scanner inputFile = new Scanner(file);
答案 3 :(得分:0)
1)程序必须在文件中打印文本:
让我们调用此文件, dataFile.text 或 dataFile.dat (两种方式都有效)
假设客户端将输入任何文件,我们必须获取该文件的名称......
这些Java语句可以解决这个问题:
//创建扫描程序以读取用户的文件名
扫描程序userInput =新扫描程序(System.in);
//从客户端
System.out.println(“输入输入文件的名称:”);
//获取客户端的文件名
字符串fileName = scanFile.nextLine();
// scanFile,扫描位于src目录中的新File fileName(客户端文件)。 “src /”表示文件的路径(客户端的文件)。
扫描程序scanFile =新扫描程序(新文件(“src /”+ fileName));
<强> ----------------------------------------- ---------------------------------------- 强>
2)问题是我将其编码为从特定文件中读取文本
是的,你所做的只是创建你自己的文件,“numbersonnumbers.txt”,这正是你应该在解决这个问题的过程中遵循的代码,而不是输入“numbersonnumbers.text” ,您将使用客户端的输入文件,即前一代码中的String变量“fileName”... *
文件文件=新文件(“numbersonnumbers.txt”);
扫描仪inputFile =新扫描仪(文件);
已更正:文件文件=新文件(“src /”+ fileName);
扫描仪inputFile =新扫描仪(文件); //将扫描所有文件的数据
更正了更多:扫描仪inputFile =新扫描程序(新文件(“src /”+ fileName));
<强> -------------------------------------------- ------------------------------------- 强>
3)程序必须打印文件
中的文本所以你已经完成了#1中的扫描仪scanFile或#2中的扫描仪inputFile
现在,创建一个数组,显然数组的类型将基于文件中的数据项的类型...(例如,月份名称)或int的字符串(例如,ID号...等)< / p>
假设文件中的数据项是月份(jan,feb,march)的名称,我们将生成一个String类型的数组。
String [] fileData = new String [100]; //具有随机长度(100)
//现在我们将文件中的每个项目复制到数组中,您需要占位符值来跟踪文件项目遍历(停止在每个项目)的位置。
int i = 0; // itialize遍历的开始(文件项的索引0)
while(scanFile.hasNextLine()){ //当文件在下一行输入时,从文件中读取。
fileData[++i] = scanFile.nextLine(); // Input file data and increment i
System.out.println(fileData[i]); // Prints all of the file's data items
} // end while
尊敬, ! - di0m
答案 4 :(得分:0)
您已经在代码中获得了几乎所有需要的东西。您只需要正确地重新排列代码行,并添加更多代码即可。
您需要执行两个步骤:扫描用户作为完整文件路径输入的内容,然后按如下所示扫描实际文件数据:
// Scanner Object to be used for the user file path input
Scanner keyboard = new Scanner(System.in);
// Asks the user to enter the full path of their file
System.out.print("Enter your full file path: ");
// Stores the user input file path as a string
String filename = keyboard.nextLine();
// Creates a File object for the file path, and scans it
Scanner inputFile = new Scanner(new File(filename);
现在,您可以阅读扫描的文件inputFile
。例如,您可以使用while
循环一次读取一行,以读取所有数据并按以下方式打印出来:
// Reads one line at a time, and prints it out
while(inputFile.hasNext) {
System.out.println(inputFile.nextLine());
或者您可以将每行的原始数据(已修剪和拆分)放入数组中,并按如下所示将每行打印为数组:
// Declares a String array[] to be used for each line
String[] rawData;
// Reads one line at a time, trims and split the raw data,
// and fills up the array with raw data elements, then prints
// out the array. it repeats the same thing for each line.
while (scanner2.hasNext()) {
rawData = scanner2.nextLine().trim().split("\\s+");
System.out.println(Arrays.toString(rawData));
}