The system cannot find the file specified in java

前端 未结 8 834
轮回少年
轮回少年 2020-11-28 08:52

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){
           


        
相关标签:
8条回答
  • 2020-11-28 09:33

    Generally, just stating the name of file inside the File constructor means that the file is located in the same directory as the java file. However, when using IDEs like NetBeans and Eclipse i.e. not the case you have to save the file in the project folder directory. So I think checking that will solve your problem.

    0 讨论(0)
  • 2020-11-28 09:38

    When you run a jar, your Main class itself becomes args[0] and your filename comes immediately after.

    I had the same issue: I could locate my file when provided the absolute path from eclipse (because I was referring to the file as args[0]). Yet when I run the same from jar, it was trying to locate my main class - which is when I got the idea that I should be reading my file from args[1].

    0 讨论(0)
提交回复
热议问题