Java (springboot) reads txt text content

Source: Internet
Author: User
Tags stringbuffer

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.