【问题标题】:Have java find/read a file from user input?java是否从用户输入中查找/读取文件?
【发布时间】:2013-10-28 02:48:49
【问题描述】:

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

 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" 文件的情况下可以正常工作,那么您刚刚编辑的代码一点也不相关。如果它不适用于该文件,那么我将删除此问题并专注于解决该问题,然后再解决让用户输入文件名的问题。
  • @nhgrif 它确实有效...整个 while 循环设置是程序的重点...
  • 那么该代码无关紧要。您需要包含相关代码。具体来说,您尝试将用户输入的文件名发送到您的File file = new File(someFile); 行的代码。以及您收到的错误消息,如果您收到的话。
  • 只有这两部分的代码.....
  • 我不知道该告诉你什么。 “不起作用”、“不接受”和“陷入困境”是对实际发生的事情的完全不可接受的描述。如果您的错误不能更具体,没有人可以帮助您。

标签: java file input


【解决方案1】:

声明一个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,第二个 sn-p 与第一个 sn-p 执行完全相同的操作。

【讨论】:

  • 我试过了,编译器接受了……然后程序陷入了困境。
  • 如果我在单词后按回车,它说它找不到文件
  • @WeekzGod:我认为如果有人能够提供更多帮助,您将不得不展示更多的程序。
  • @WeekzGod 您需要向我们展示您的尝试。包括明确告诉我们您输入的内容以及文件的名称。
  • @nhgrif 下面的帮助 suppleid 确实是我之前尝试过的......几乎是逐字逐句。
【解决方案2】:

首先你应该让用户输入文件名。然后只需将代码中的“numbersonnumbers.txt”替换为您为用户输入设置的变量。

Scanner in = new Scanner(System.in);
System.out.println("What is the filename?");
String input = in.nextLine();
File file = new File(input);

【讨论】:

  • 我就是这么做的!!!并且编译器接受了……然后程序陷入了困境。
  • 如果我在说它找不到文件后按回车键
【解决方案3】:
Scanner input = new Scanner(System.in);
String fileName = input.nextLine();
File file = new File(fileName);
Scanner inputFile = new Scanner(file);

【讨论】:

  • 我就是这么做的!!!并且编译器接受了……然后程序陷入了困境。
  • 如果我在单词后按回车,它说它找不到文件
【解决方案4】:

1) 程序必须打印文件中的文本:

  • 让我们调用这个文件,dataFile.textdataFile.dat(任何一种方式都可以)

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

    这些 Java 语句可以解决问题:

    // 创建一个扫描器来读取用户的文件名

  • Scanner userInput = new Scanner(System.in);

    // 向客户端请求文件名

  • System.out.println("输入输入文件名:");

    // 获取客户端的文件名

  • 字符串文件名 = scanFile.nextLine();

    // scanFile,扫描新的File fileName(客户端的文件),位于src目录下。 “src/”代表文件(客户端文件)的路径。

  • 扫描仪 scanFile = new Scanner(new File("src/" + fileName));

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

------------------------------ ---------------------------------------- p>

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

  • 是的,您所做的只是创建自己的名为“numbersonnumbers.txt”的文件,这正是您在解决此问题时应遵循的代码,但您无需键入“numbersonnumbers.text”,而是正在使用客户端的输入文件,也就是前面代码中的String变量“fileName”...*

    文件file = new File("numbersonnumbers.txt");

    Scanner inputFile = new Scanner(file);

更正:文件 file = new File("src/" + fileName);

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

更正更多:Scanner inputFile = new Scanner(new File("src/" + fileName));

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

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

  • 现在要将文件中的数据项打印到控制台,您只需将文件中的每个项复制到数组、列表或树中,具体取决于实现规范,但这显然是初学者编程,所以不要介意列表和树,使用数组。

所以你已经完成了 #1 中的 Scanner scanFile 或 #2 中的 Scanner inputFile

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

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

String[] fileData = new String[100]; // 随机长度 (100)

// 现在我们将把文件中的每个项目复制到数组中,您将需要占位符值来跟踪文件项目的遍历(在每个项目处停止)期间的位置。

int i = 0; //初始化遍历的开始(文件项的索引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

【讨论】:

    【解决方案5】:

    您已经在代码中获得了几乎所有需要的东西。你只需要正确地重新安排你的代码行,并添加更多。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 2014-06-17
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      相关资源
      最近更新 更多