Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
让java从用户输入中查找/读取文件吗?_Java_File_Input - Fatal编程技术网

让java从用户输入中查找/读取文件吗?

让java从用户输入中查找/读取文件吗?,java,file,input,Java,File,Input,程序必须打印我要求它读取的文件中的文本。 问题是我将其编码为从特定文件读取文本 File file = new File("numbersonnumbers.txt"); Scanner inputFile = new Scanner(file); 如何让程序从用户输入指定的文件中读取文本? *该文件将与程序位于同一目录/文件夹中,因此这不是问题 编辑:我先前尝试的用户输入 Scanner keyboard = new Scanner(System.in); String filename

程序必须打印我要求它读取的文件中的文本。 问题是我将其编码为从特定文件读取文本

 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);
声明一个字符串变量,例如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,则第二个代码段的操作与第一个代码段完全相同。

首先,您应该要求用户输入文件名。然后将代码中的numbersonnumbers.txt替换为为为用户输入设置的变量

Scanner input = new Scanner(System.in);
String fileName = input.nextLine();
File file = new File(fileName);
Scanner inputFile = new Scanner(file);
Scanner in = new Scanner(System.in);
System.out.println("What is the filename?");
String input = in.nextLine();
File file = new File(input);
1程序必须打印文件中的文本:

让我们以任何一种方式调用此文件、dataFile.text或dataFile.dat

假设客户端将输入任何文件,我们必须获取文件名

这些Java语句将实现以下功能:

//创建扫描仪以读取用户的文件名

Scanner userInput=new ScannerSystem.in

//从客户端请求文件名

System.out.println输入输入文件的名称:

//获取客户端的文件名

字符串文件名=scanFile.nextLine

//scanFile,扫描位于src目录中的新文件名客户端文件。src/表示文件客户端文件的路径

Scanner scanFile=new Scanner new Filesrc/+fileName

由于问题表明文件将与程序位于同一目录中,因此这不是问题。 -----------------------------------------

2问题是我将其编码为从特定文件读取文本

 File file = new File("numbersonnumbers.txt");
 Scanner inputFile = new Scanner(file);
是的,您所做的只是创建自己的名为numbersonnumbers.txt的文件,这正是您在解决此问题时应该遵循的代码,但是您将使用客户端的输入文件,而不是键入numbersonnumbers.text,它是前面代码中的字符串变量文件名*

File File=new Filenumbersonnumbers.txt

扫描仪输入文件=新的扫描仪文件

更正:File File=new Filesrc/+fileName

扫描仪输入文件=新的扫描仪文件;//将扫描文件的所有数据

更正更多:扫描仪输入文件=新扫描仪新文件RC/+文件名

-----------------------------------------

3程序必须打印文件中的文本

现在,要将文件中的数据项打印到控制台,您只需将文件中的每个项复制到数组、列表或树中,具体取决于实现规范,但这显然是初学者编程,因此,更不用说列表和树了,使用数组。 因此,您已在1中完成扫描仪扫描文件或在2中完成扫描仪输入文件

现在,创建一个数组,显然数组的类型将基于文件中数据项的类型。。。字符串(例如月份名称)或int(例如ID号)。。ect

假设文件中的数据项是月份jan、feb、march的名称,我们将创建一个字符串类型的数组

字符串[]文件数据=新字符串[100];//随机长度为100

//现在,我们将把文件中的每个项复制到数组中,您将需要placeholder值来跟踪遍历过程中文件项的每个项的位置

int i=0;//初始化文件项的遍历索引0的开始

当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

您已经在代码中获得了所需的几乎所有内容。您只需正确地重新排列代码行,然后再添加几行即可

您需要两个步骤:扫描用户输入的完整文件路径,然后扫描实际文件数据,如下所示:

// 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);    
// 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));
}
现在您可以读取扫描的文件inputFile。例如,您可以使用while循环一次读取一行来读取所有数据并按如下方式打印出来:

// 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);    
// 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));
}
或者,您可以将每行经过修剪和拆分的原始数据放在一个数组中,并将每行打印为一个数组,如下所示:

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

我试过了,编译器接受了…然后程序陷入了困境。如果我在它说找不到file@WeekzGod:如果有人想进一步提供帮助,我想你必须展示更多的程序。@WeekzGod你需要向我们展示你的尝试。包括明确告诉我们你做了什么您输入的内容和文件的名称。@nhgri如果下面的帮助代码是我以前尝试过的内容…几乎是逐字逐句。我做到了这一点!!!编译器接受了这一点…然后程序陷入了困境。如果我在单词后按enter键,它会说找不到文件,我就是这么做的!!!
编译器接受了这一点……然后程序陷入了困境。而且,如果我在它说找不到文件后按enter键,那么你刚才编辑的代码一点也不相关,假设它在硬编码的numbersonnumbers.txt文件中工作,那么就可以了。如果它对该文件不起作用,那么我会删除这个问题,并在解决让用户键入文件名之前集中精力解决该问题。@nhgri如果它真的起作用……整个while循环设置是程序的重点……那么该代码是无关的。您需要包含相关代码。具体地说,您尝试将用户输入的文件名发送到您的file file=new FilesomeFile;线还有你收到的错误信息,如果你收到的话。这是代码中仅有的两部分……我不知道该告诉你什么。对实际发生的事情的描述是完全不能接受的。如果你不能更具体地说明你的错误,没有人能帮助你。