user1
user1

Reputation: 499

File path in java code in ubuntu

I'm new in java and Ubuntu. I want to know how can i write the right path of specific file in code as i got exception like that

/home/me/javaException in thread "main" java.io.FileNotFoundException: 
home/me/java/config.properites (No such file or directory)

Here is my code:

public static void main(String[] args) {
    Properties prop = new Properties();
    String queryFile = null;
    FileOutputStream fos = null;
    try {
        // load a properties file
        FileInputStream finputstream = new FileInputStream("config.properties");

Upvotes: 3

Views: 11083

Answers (2)

liuzhengyang
liuzhengyang

Reputation: 406

        System.out.println(this.getClass().getResource("").getPath());
        System.out.println(this.getClass().getResource("/").getPath());

Maybe this is helpful.It prints the current class location and class path root

        System.out.println(new File("").getAbsolutePath());

This prints the path where java command executes

Upvotes: 2

prit4fun
prit4fun

Reputation: 460

In ubuntu or any linux os your home folder is available in 'home/< username >'. You can check this from terminal or in gui. You can try some thing like,

File file = new File("/home/<your user name>/file.txt");
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);

Upvotes: 4

Related Questions