Relative path of IO stream file and method for obtaining system path

There is often no problem when testing this machine, but when the server fails to read the file, the system path of the file can be obtained through class reflection to avoid abnormalities.



package com.ute.action;



import java.io.File;


public class action {
public static void main(String[] args) {
action.teIO ();
}

public static void teIO(){
//Wrong method
File file=new File("utest.txt");
System.out.println(file.isFile());//false

//The correct way·The stream object can be obtained by reflection
File file1 = new File(action.class.getResource("utest.txt").getFile());
System.out.println(file1.isFile());//true
}

}

Guess you like

Origin blog.csdn.net/feng8403000/article/details/78212585