Use jsp to read TXT files and jsp to read txt files

Source: Internet
Author: User

Use jsp to read TXT files and jsp to read txt files

  1. <% @ Page import = "java. io. BufferedReader" %>
  2. <% @ Page import = "java. io. FileReader" %>
  3. <% @ Page import = "java. io. File" %>
  4. <% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %>
  5. <%
  6. // String path = request. getContextPath ();
  7. // System. out. println ("path =" + path );
  8. // String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
  9. %>
  10. <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
  11. <Html>
  12. <Head>
  13. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
  14. <Title> instance 9: use jsp to read TXT files </title>
  15. </Head>
  16. <Body>
  17. <%
  18. // String filePath = request. getSession (). getServletContext (). getRealPath ("/") + "JSP_Ajax" + "\\";
  19. // System. out. println ("filePath =" + filePath );
  20. String path = "C :\\"; // you must change the file directory to a relative path.
  21. File file = new File (path, "jsp.txt ");
  22. FileReader fr = new FileReader (file); // character input stream
  23. BufferedReader br = new BufferedReader (fr); // enables the file to be read by row and has the buffer function
  24. StringBuffer strB = new StringBuffer (); // use strbto store content in the jsp.txt File
  25. String str = br. readLine ();
  26. While (str! = Null ){
  27. StrB. append (str). append ("<br>"); // Add the read content to strB
  28. Str = br. readLine ();
  29. }
  30. Br. close (); // close the input stream
  31. %>
  32. <Center>
  33. <% = StrB %>
  34. </Center>
  35. </Body>
  36. </Html>
 

Here, the File object must use an absolute path, "C: \". In addition, the directory separator cannot use "\", and must use "\\'.

 

Tip: The Terminator of a text file line is ('\ n') or (' \ R'), but the browser does not recognize the terminator of these lines. Therefore, to display a line break in the browser, you must add an HTML Tag after each line. <br>

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.