Servlet read/write operations on files

Source: Internet
Author: User

(1) how to read the file content in Serlvet

 
Package COM. tsinghua; import Java. io. *; import javax. servlet. HTTP. *; public class servletfiletest1 extends httpservlet {public void doget (httpservletrequest req, httpservletresponse res) {try {// Chinese garbled res. setcontenttype ("text/html; charset = GBK"); printwriter PW = res. getwriter (); // create a filereaderfilereader F = new filereader ("F: \ mycounter.txt"); bufferedreader BW = new bufferedreader (f ); // read a row of data or use string numval = bw for all content in the for loop. readline (); // be sure to close the file stream BW. close (); PW. println ("the data read from the file is:" + numval);} catch (exception ex) {ex. printstacktrace () ;}} public void dopost (httpservletrequest req, httpservletresponse res) {This. doget (req, Res );}}

(2) how to write data to a file in Serlvet

 
Package COM. tsinghua; import Java. io. *; import javax. servlet. HTTP. *; public class servletfiletest2 extends httpservlet {public void doget (httpservletrequest req, httpservletresponse res) {try {// Chinese garbled res. setcontenttype ("text/html; charset = GBK"); printwriter PW = res. getwriter (); // create a filewriter to view memory data-> Disk Files (write, output) // to view memory data <-disk files (read, input) filewriter F = new filewriter ("F: \ mycounter.txt"); bufferedwriter BW = new bufferedwriter (f); // write data BW to the file. write ("1"); // close the file stream BW. close (); PW. println ("data is written in the file");} catch (exception ex) {ex. printstacktrace () ;}} public void dopost (httpservletrequest req, httpservletresponse res) {This. doget (req, Res );}}



Servlet read/write operations on files

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.