系统找不到java中指定的文件 [英] The system cannot find the file specified in java

查看:462
本文介绍了系统找不到java中指定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个打开并读取文件的程序。
这是我的代码:

I am making a program that opens and reads a file. This is my code:

import java.io.*;

public class FileRead{
    public static void main(String[] args){
        try{
            File file = new File("hello.txt");
            System.out.println(file.getCanonicalPath());
            FileInputStream ft = new FileInputStream(file);

            DataInputStream in = new DataInputStream(ft);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strline;

            while((strline = br.readLine()) != null){
                System.out.println(strline);
            }
            in.close();
        }catch(Exception e){
            System.err.println("Error: " + e.getMessage());
        }
    }
}

但是当我跑步时,我得到此错误:

but when I run, I get this error:

C:\Users\User\Documents\Workspace\FileRead\hello.txt
Error: hello.txt (The system cannot find the file specified)

my FileRead.java hello.txt 在同一目录中的哪个位置可以找到:

my FileRead.java and hello.txt where in the same directory that can be found in:

C:\Users\User\Documents\Workspace\FileRead

我想知道我做错了什么?

I'm wondering what I am doing wrong?

推荐答案

我复制了你的代码运行正常。

I have copied your code and it runs fine.

我怀疑你在hello.txt的实际文件名中遇到了一些问题,或者你在错误的目录中运行。请考虑使用@ Eng.Fouad

I suspect you are simply having some problem in the actual file name of hello.txt, or you are running in a wrong directory. Consider verifying by the method suggested by @Eng.Fouad

这篇关于系统找不到java中指定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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