0

Im trying to use relative path to read a file but the path cant be found.

maxSuccession is the name of my project

This is my workspace path: C:\Programming

This is my project's path C:\Programming\MaxSuccessions\maxSuccessions

This is my filepath C:\Programming\MaxSuccessions\Tests\test1\00.in.txt

package test1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class test1 {
    public static void main(String[] args) throws IOException {
        String myPath="..\\MaxSuccessions\\Tests\\test1\\";
        Scanner sc = new Scanner(new File(myPath+"00.in.txt"));
        System.out.println(sc.nextInt());
    }
}
Juan Olivadese
  • 115
  • 1
  • 8
  • Please, provide more information. What is the absolute path to the file you need? What is the absolute path of your project and of your source file? – Dmitrii Bocharov Aug 20 '17 at 15:08
  • See above or show the dir of 'MaxSuccessions'. – cliff2310 Aug 20 '17 at 15:12
  • Your relative path is relative to jar location. – user2026753 Aug 20 '17 at 15:15
  • Possible duplicate of [How does Java resolve a relative path in new File()?](https://stackoverflow.com/questions/21060992/how-does-java-resolve-a-relative-path-in-new-file) – MordechayS Aug 20 '17 at 15:19
  • 1
    project's path is C:\Programming\MaxSuccessions\maxSuccessions, going one level up already takes to MaxSuccessions, so I think you should omit MaxSuccessions in relative path ie. ..\\Tests\\test1\\ – Pallavi Sonal Aug 20 '17 at 15:21
  • What does the .. mean exactly? – Juan Olivadese Aug 20 '17 at 15:40
  • @user2026753: This is not true. The actual path is the location where you started java. Started within eclipse it's the root path of the eclipse project.. – Heri Aug 20 '17 at 16:34
  • A hint: path combining is better done by the File class instead of self concatenating the two parts my Path and "00.in.txt". The File class has a constructor which takes two strings (new File(myPath, "00.in.txt"), which will result in a combined path. So you do not have to bother if the myPath already has a trailing path separator or not. And: within java you better use the forward slash (only one). The backslash is a windows thing, and must be doubled within a java string – Heri Aug 20 '17 at 16:42

1 Answers1

0

If your class file and required file are in the same directory, you can try this code:

public static void main(String[] args) {
    String fileName = "00.in.txt"; //relative path from the path of your class.
    try {
        Scanner sc = new Scanner(Test.class.getResource(fileName).openStream());
        System.out.println(sc.nextInt());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
  • **Never** use URL.getFile() to convert a URL to a file name. Despite the name, getFile() does not return a valid file name—it returns the path and query portions of the URL, with all percent-escapes intact. For instance, it might return `file:/C:/Program%20Files`. Also, assuming Class.getResource returns a file: URL will prevent the code from working when run from a .jar file. – VGR Aug 20 '17 at 16:18
  • This is only true if your main class is in the root package. Class.getResource looks up in the classpath. If you have class files in a directory hierarchy (e.g. build/my/project/main/Main.class) then your classpath points to build and your 00.in.txt would only be found if its location is build/00.in.txt. If it is in the same directory as Main.class, its only found if you search with my/project/main/00.in.txt. – Heri Aug 20 '17 at 16:40