public class Txttest {
private static final Logger Logger = Loggerfactory.getlogger (Txttest.class);
public static String readtxt (file file) throws IOException {
String s = "";
InputStreamReader in = new InputStreamReader (new FileInputStream (file), "UTF-8");
BufferedReader br = new BufferedReader (in);
StringBuffer content = new StringBuffer ();
while ((S=br.readline ())!=null) {
Content = Content.append (s);
}
return content.tostring ();
}
public static void Main (string[] args) {
try {
Get files by absolute path
String S1 = txttest.readtxt (New File ("C:\\users\\....\\du.txt"));
Logger.info (S1);
Files in spring boot are placed directly under the resources directory
String s2 = txttest.readtxt (Resourceutils.getfile ("Classpath:du.txt"));
Logger.info (S2);
} catch (IOException e) {
E.printstacktrace ();
}
}
}
Java (springboot) reads txt text content