如何在java程序中打开文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26455340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:32:48  来源:igfitidea点击:

How to open a file in a java program?

javafile

提问by justaregularguy

my professor wants us to write a program that will open a file and read the lines and he gave this example;

我的教授希望我们编写一个程序来打开一个文件并读取行,他给出了这个例子;

import java.io.*;
import java.util.Scanner;
import java.io.IOException;

public class LineNumbers {

public static void main(String[] args) throws IOException

  {
       // Create a Scanner object for keyboard input.
      Scanner keyboard = new Scanner(System.in);

      // Get the filename.
      System.out.print("Enter the filename: ");
      String filename = keyboard.nextLine();

      // Open the file.
      File file = new File(filename);
      Scanner inputFile = new Scanner(file);

      // Read lines from the file until no more are left.
      while (inputFile.hasNext())
      {
         // Read the next name.
         String familyName = inputFile.nextLine();

         // Display the last name read.
         System.out.println(familyName);

            }

      // Close the file.
      inputFile.close();
      keyboard.close();
}

}

The problem is, once I run the program and it tells me to enter the filename, what exactly do I type? If I make a random text file named "test" on my desktop and input "test" into the program it won't open it up. Am I supposed to type some special character to open it up and have it read? Thank you.

问题是,一旦我运行程序并告诉我输入文件名,我到底要输入什么?如果我在桌面上创建一个名为“test”的随机文本文件,然后在程序中输入“test”,它就不会打开它。我应该输入一些特殊字符来打开它并阅读它吗?谢谢你。

回答by Azurlake

You can enter the file name with the whole path:

您可以输入带有整个路径的文件名:

C:\\Path\\To\\The\\File.txt

C:\\Path\\To\\The\\File.txt

or

或者

/tmp/path/to/file.txt

/tmp/path/to/file.txt

Or type in just the file name given that it is located under the same path than the executable java class.

或者只输入文件名,因为它与可执行的 java 类位于同一路径下。

You may need to put the filename between quotes or double quotes if the path contains any spaces.

如果路径包含任何空格,您可能需要将文件名放在引号或双引号之间。

回答by César

you should type the path to the file...if you are using windows, you should type something like

你应该输入文件的路径...如果你使用的是windows,你应该输入类似的东西

c:\<path_to_desktop>\test.txt (if it is a txt file)

on linux, you should type something like

在 linux 上,你应该输入类似

/<path_to_desktop>/test.txt

回答by Nikhil Katre

Try creating a sample file called "test.txt" Input some values into it such as "Hello World" Close the file

尝试创建一个名为“test.txt”的示例文件 向其中输入一些值,例如“Hello World” 关闭文件

NOTE -> Make sure that the file is created in the same directory where your java program is

注意 -> 确保该文件创建在您的 java 程序所在的同一目录中

Now, run the java program, when the program prompts for the file name, enter text.txt (the complete name of the file)

现在,运行java程序,当程序提示输入文件名时,输入text.txt(文件的全名)

This should run and print the contents of the file on the console, which is what this program is doing.

这应该运行并在控制台上打印文件的内容,这就是该程序正在执行的操作。

回答by Pokechu22

Enter the full path to the file.

输入文件的完整路径。

If you are using windows, you can get this by holding Shiftand then pressing Right-Clickon the file, and then selecting Copy as path.

如果您使用的是 Windows,您可以通过按住文件Shift然后按下Right-Click文件,然后选择Copy as path.