迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

Java 中 The System Cannot Find the File Specified

作者:迹忆客 最近更新:2023/07/12 浏览次数:

本篇文章介绍如何解决 Java 中的 The system cannot find the file specified 错误。


修复Java中 The system cannot find the file specified 错误

当我们正在加载的文件不在目录中或文件名不正确时,就会出现Java错误“The system cannot find the file specified”。 它也是 Java IO 包中的一个异常,当系统找不到具有给定名称的文件时抛出该异常。

让我们尝试一个会抛出相同错误的示例:

package jiyik;

import java.io.*;

public class Example{
    public static void main(String[] args){
        try{
            File NewFile = new File("NewJiyik.txt");
            System.out.println(NewFile.getCanonicalPath());
            FileInputStream File_Input_Stream = new FileInputStream(NewFile);

            DataInputStream Data_Input_Stream = new DataInputStream(File_Input_Stream);
            BufferedReader Buffered_Reader = new BufferedReader(new InputStreamReader(Data_Input_Stream));
            String line;

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

文件 NewJiyik.txt 不在该目录中,因此上面的代码将抛出错误。 查看输出:

C:\Users\Sheeraz\eclipse-workspace\Demos\NewJiyik.txt
Error: NewDelftstack.txt (The system cannot find the file specified)

要解决此问题,请确保您输入了正确的文件名和路径。 我们还可以检查 Java 中的文件列表,如果我们输入了正确的名称和路径,这将对我们有所帮助。

参见示例:

package jiyik;

import java.io.*;

public class Example{
    public static void main(String[] args){
        try{
            File file = new File(".");
            for(String fileNames : file.list()) System.out.println(fileNames);
            File NewFile = new File("NewJiyik.txt");
            System.out.println(NewFile.getCanonicalPath());
            FileInputStream File_Input_Stream = new FileInputStream(NewFile);

            DataInputStream Data_Input_Stream = new DataInputStream(File_Input_Stream);
            BufferedReader Buffered_Reader = new BufferedReader(new InputStreamReader(Data_Input_Stream));
            String line;

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

上面的代码将显示目录中的文件列表,我们可以查找我们的文件,然后在代码中更正文件名和路径。 查看输出:

.classpath
.project
bin
Jiyik.png
jiyik.txt
jiyik.xml
jiyik.zip
src
C:\Users\Sheeraz\eclipse-workspace\Demos\NewJiyik.txt
Error: NewJiyik.txt (The system cannot find the file specified)

该目录中没有名为 NewJiyik.txt 的文件。 我们可以用这个名称创建一个文件,或者在代码中更改文件名。

参见示例:

package jiyik;

import java.io.*;

public class Example{
    public static void main(String[] args){
        try{

            File NewFile = new File("Jiyik.txt");
            System.out.println(NewFile.getCanonicalPath());
            FileInputStream File_Input_Stream = new FileInputStream(NewFile);

            DataInputStream Data_Input_Stream = new DataInputStream(File_Input_Stream);
            BufferedReader Buffered_Reader = new BufferedReader(new InputStreamReader(Data_Input_Stream));
            String line;

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

由于该目录包含文件 Jiyik.txt,因此代码将正常工作。 查看输出:

C:\Users\Sheeraz\eclipse-workspace\Demos\jiyik.txt
Hello, This is a new text file from jiyik.com after overwriting the previous file.

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

JavaScript POST

发布时间:2024/03/23 浏览次数:96 分类:JavaScript

本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便