Java文件阅读:必须输入完整路径 [英] Java File Reading: Have to enter full path

查看:65
本文介绍了Java文件阅读:必须输入完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package files;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;

public class file {

    public static void main(String[] args)throws FileNotFoundException {

            File file = new File("txtfile.txt");
            Scanner input = new Scanner(file);
            while (input.hasNextLine()) {
                System.out.println(input.nextLine());
            }

    }

}

在说file.txt的地方,我必须输入完整的文件路径.我看过的所有教程都不必这样做.

Where it says file.txt I have to enter the full file path. All tutorials I watch do not have to do this.

推荐答案

是的! File file = new File("txtfile.txt"); txtfile.txt 是您要读取的文件的路径.如果该文件不在您的Java文件所在的目录中,则提供该文件的路径,例如"C:\ Users \ me \ Desktop \ txtfile.txt".编译Java文件后,将创建一个.class文件,该.class文件也将在与该文件相同的文件夹中创建.

Yeah! File file = new File("txtfile.txt"); txtfile.txt is a path to your file that you want to read. Provide the path where file is like "C:\Users\me\Desktop\txtfile.txt" if the file is not in the same directory where your java file is. After you compile the java file a .class file is created it that .class file is also created in the same folder it will work with.

文件=新文件("txtfile.txt"); ,您无需指定完整路径.

File file = new File("txtfile.txt"); and you don't need to specify the full path.

否则,您必须提供上述绝对文件路径.

If not you then you have to provide the absolute file path like above.

这篇关于Java文件阅读:必须输入完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆