file - How to use relative filepath in java -
im trying use relative path read file path cant found.
maxsuccession name of project
this workspace path: c:\programming
this project's path c:\programming\maxsuccessions\maxsuccessions
this 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()); } }
if class file , required file in same directory, can try code:
public static void main(string[] args) { string filename = "00.in.txt"; //relative path path of class. try { scanner sc = new scanner(test.class.getresource(filename).openstream()); system.out.println(sc.nextint()); } catch (ioexception e) { e.printstacktrace(); } }
Comments
Post a Comment