Note: the disk of the executed code file is the root directory.
Assuming that the compiled class file is on the e drive, the E: \ opt \ test.txt file under e can be written as/opt/test.txt in the code.
The advantage is that the code written in windows can be directly deployed on the linux server without changing the path.
Test code:
Package com. yanek. util; import java. io. bufferedReader; import java. io. file; import java. io. fileReader; import java. io. IOException; public class Test {/*** @ param args */public static void main (String [] args) {String path = "/opt/test.txt "; string c = readText (path); System. out. println ("c =" + c);}/*** read content from the file ** @ param filename * @ return */public static String readText (String filename) {String content = ""; tr Y {File file = new File (filename); if (file. exists () {FileReader fr = new FileReader (file); BufferedReader br = new BufferedReader (fr); String str = ""; String newline = ""; while (str = br. readLine ())! = Null) {content + = newline + str; newline = "\ n" ;}br. close (); fr. close () ;}} catch (IOException e) {e. printStackTrace () ;}return content ;}}