简体   繁体   中英

java eclipse fails to detect a “bin” located file

I use eclipse and put a File called "example.txt" into the bin-folder where the class files are generated (and into the sub-folder of the package). But still, the program allways prints out the error message i wrote for the case the file is not found.

Main Class:

import java.io.BufferedReader;

public class Main {

public static void main(String[] args){

    BufferedReader file = Console.file("example.txt");

    ...
}

Console Class:

public final class Console {


public static BufferedReader file(String args) {

    BufferedReader file = null;

    try {
        file = new BufferedReader(new FileReader(args));
    } catch (FileNotFoundException e) {
        println("Error, file not found!");
        System.exit(1);
    }

    return file;
}
}

any ideas?

For the Eclipse project, the current path is the project folder, not the BIN directory, you can use the code below to get the current path so that you will know where to put and how to access the file.

import java.io.File;
import java.io.IOException;


    public class MainTest {
        public static void main(String[] args)
        {
            File directory = new File("");
            try {
                System.out.println(directory.getCanonicalPath());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

So in your case, the path you specify should be: ./bin/example.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM