Java provides I/O class libraries, allowing you to easily perform operations on files using Java. The following describes how to use Java to implement these operations. </P> <p> create a directory <% @ page contenttype = "text/html; charset = gb2312 "%> <br/> <% <br/> // string url = request. getrequesturi (); <br/> string filepath = "C: // test //"; <br/> filepath = filepath. tostring (); // Chinese conversion <br/> JAVA. io. file myfilepath = new Java. io. file (filepath); <br/> If (! Myfilepath. exists () <br/> myfilepath. mkdir (); <br/>%> </P> <p> create a file <br/> <% @ page contenttype = "text/html; charset = gb2312 "%> <br/> <% @ page import =" Java. io. * "%> <br/> <% <br/> string filepath =" C:/test/newfile.txt "; <br/> filepath = filepath. tostring (); <br/> file myfilepath = new file (filepath); <br/> If (! Myfilepath. exists () <br/> myfilepath. createnewfile (); <br/> filewriter resultfile = new filewriter (myfilepath); <br/> printwriter myfile = new printwriter (resultfile ); <br/> string content = "this is the test data"; <br/> string strcontent = content. tostring (); <br/> myfile. println (strcontent); <br/> resultfile. close (); <br/>%> </P> <p> delete a file <% @ page contenttype = "text/html; charset = gb2312 "%> <br/> <% <br/> string filepath =" C: // Test // Newfile.txt "; <br/> filepath = filepath. tostring (); <br/> JAVA. io. file mydelfile = new Java. io. file (filepath); <br/> If (mydelfile. exists () <br/>{< br/> mydelfile. delete (); <br/> out. println (filepath + "deleted successfully !!! "); <Br/>}< br/> else <br/>{< br/> out. println (filepath + "this file does not exist"); <br/>}< br/>%> <br/> file copy <% @ page contenttype = "text/html; charset = gb2312 "%> <br/> <% @ page import =" Java. io. * "%> <br/> <% <br/> int bytesum = 0; <br/> int byteread = 0; <br/> // file: reading the stream <br/> inputstream instream = new fileinputstream ("C: // test // newfile.txt"); <br/> fileoutputstream FS = new fileoutputstream ("C: // test // copyfile.txt "); <br/> byte [] Buf Fer = new byte [1444]; <br/> int length; <br/> while (byteread = instream. Read (buffer ))! =-1) <br/>{< br/> out. println ("<DT> <B>" + byteread + "</B> </DT>"); <br/> bytesum + = byteread; <br/> out. println (bytesum); <br/> FS. write (buffer, 0, byteread); <br/>}< br/> instream. close (); <br/>%> </P> <p> copy the entire folder <br/> <% @ page contenttype = "text/html; charset = gb2312 "%> <br/> <% @ page import =" Java. io. * "%> <br/> <% string url1 =" C:/AAA "; <br/> string url2 =" D:/Java /"; <br/> (new file (url2 )). mkdirs (); <br/> file [] File = (new file (url1 )). listfiles (); <br/> for (INT I = 0; I <file. length; I ++) {<br/> If (file [I]. isfile () {<br/> file [I]. tostring (); <br/> fileinputstream input = new fileinputstream (file [I]); <br/> fileoutputstream output = new fileoutputstream (url2 + "/" + (file [I]. getname ()). tostring (); <br/> byte [] B = new byte [1024*5]; <br/> int Len; <br/> while (LEN = input. read (B ))! =-1) {<br/> output. write (B, 0, Len); <br/>}< br/> output. flush (); <br/> output. close (); <br/> input. close (); <br/>}< br/>%> </P> <p> File Download <br/> <% @ page contenttype = "text/html; charset = gb2312 "%> <br/> <% @ page import =" Java. io. * "%> <br/> <% <br/> string filename =" newfile.txt ". tostring (); <br/> // read to the stream <br/> inputstream instream = new fileinputstream ("C: // test // newfile.txt "); <br/> // set the output format <br/> r Esponse. reset (); <br/> response. setcontenttype ("text/plain"); <br/> response. addheader ("content-disposition", "attachment; filename =/" "+ filename + "/""); <br/> // cyclically retrieve the data in the stream <br/> byte [] B = new byte [100]; <br/> int Len; <br/> servletoutputstream outstream = response. getoutputstream (); </P> <p> while (LEN = instream. read (B)> 0) <br/> outstream. write (B, 0, Len); <br/> outstream. flush (); <br/> outstream. Close (); <br/> instream. close (); <br/>%> </P> <p> File Download in the database Field <br/> <% @ page contenttype = "text/html; charset = gb2312 "%> <br/> <% @ page import =" Java. util. *, Java. SQL. *, Java. io. * "%> <br/> <% <br/> string id = request. getparameter ("ID"); <br/> If (ID = NULL) <br/> {Throw new exception ("no image found "); <br/>}< br/> else <br/> {<br/> try <br/> {<br/> com.gz realmap. lib. JDBC. jdbcutil sqlbean = com.gz realmap. lib. JDBC. Jdbcutil. getinstance (); <br/> sqlbean. connect (); <br/> string SQL = "select * From innernews where id = '" + 79 + "'"; <br/> resultset rs = sqlbean. queryforupdate (SQL); <br/> Rs. next (); <br/> // string filenamedb = Rs. getstring ("imagename"); <br/> string file = Rs. getstring ("ACC"); <br/> // string filename = new string (filenamedb. getbytes (), "iso-8859-1"); <br/> string filename = "a.jpg"; <br/> response. s Etheader ("content-disposition", "inline; filename =/" "+ filename +"/""); <br/> string filter = filename. substring (filename. lastindexof (". "); </P> <p> If (filter. equals (". TXT ") <br/>{< br/> response. setcontenttype ("text/plain"); <br/>}< br/> else if (filter. equals (". doc ") | filter. equals (". dot ") <br/>{< br/> response. setcontenttype ("application/MSWord"); <br/>}< br/> else <br/>{< br/> response. Setcontenttype ("image/JPEG; charset = gb2312"); <br/>}< br/> servletoutputstream o = response. getoutputstream (); <br/> // o. write (File); <br/> out. println (File); <br/> // o. flush (); <br/> // o. close (); <br/> sqlbean. disconnect (); <br/>}< br/> catch (exception ex) <br/>{< br/> out. println (ex. getmessage ()); <br/>}< br/>%> </P> <p> Save the webpage as a file <% @ page contenttype = "text/html; charset = gb2312 "%> <br/> <% @ Page import = "Java. text. *, Java. util. *, java.net. *, Java. io. * "%> <br/> <% <br/> URL stdurl = NULL; <br/> bufferedreader stdin = NULL; <br/> printwriter stdout = NULL; <br/> try {<br/> stdurl = new URL ("http://www.163.com"); <br/>}< br/> catch (malformedurlexception E) {<br/> throw E; <br/>}</P> <p> try {<br/> // convert a byte stream to a bytes stream <br/> stdin = new bufferedreader (New inputstreamreader (stdurl. openstream () ); <Br/> string thefilename = "C: // test // 163.html"; <br/> stdout = new printwriter (New bufferedwriter (New filewriter (thefilename. tostring (); <br/>}< br/> catch (ioexception E) {<br/>}</P> <p>/*** read the page specified by the URL as a stream, write the specified file ***/<br/> try {<br/> string strhtml = ""; <br/> while (strhtml = stdin. readline ())! = NULL) {<br/> stdout. println (strhtml); <br/>}< br/> catch (ioexception e) {<br/> throw E; <br/>}< br/> finally {<br/> try {<br/> If (stdin! = NULL) <br/> stdin. Close (); <br/> If (stdout! = NULL) <br/> stdout. close (); <br/>}< br/> catch (exception e) {<br/> system. out. println (E ); <br/>}< br/>%> </P> <p> directly download an online file <br/> <% @ page contenttype = "Text /html; charset = gb2312 "%> <br/> <% @ page import =" Java. io. * "%> <br/> <% @ page import =" java.net. * "%> <br/> <% <br/> int bytesum = 0; <br/> int byteread = 0; <br/> URL url = new URL ("http://pimg.163.com/sms/micheal/logo.gif"); <br/> urlconnection Conn = URL. openconnection (); <br/> inputstream instream = Conn. getinputstream (); </P> <p>/** <br/> string thefilename = "C:/test/logo.gif"; <br/> thefilename = thefilename. tostring (); <br/> file myfilepath = new file (thefilename); <br/> If (! Myfilepath. exists () <br/> myfilepath. createnewfile (); <br/> **/</P> <p> fileoutputstream FS = new fileoutputstream ("C:/test/logo2.gif "); <br/> byte [] buffer = new byte [1444]; <br/> while (byteread = instream. read (buffer ))! =-1) <br/>{< br/> out. println ("<DT> <B>" + byteread + "</B> </DT>"); <br/> bytesum + = byteread; <br/> // system. out. println (bytesum); <br/> FS. write (buffer, 0, byteread ); <br/>}< br/>%> </P> <p> Read files by row <% @ page contenttype = "text/html; charset = gb2312 "%> <br/> <% @ page import =" Java. io. * "%> <br/> <% <br/> filereader myfilereader = new filereader (" C:/har.txt .txt "); <br/> bufferedreader mybufferedreader = new bufferedreader (M Yfilereader); <br/> string mystring = NULL; <br/> string resultstring = new string (); <br/> while (mystring = mybufferedreader. Readline ())! = NULL) {<br/> resultstring = resultstring + mystring + "<br>"; <br/>}< br/> out. println (resultstring); <br/> myfilereader. close (); <br/>%> </P> <p> processing of Word documents (uploading and downloading) <% @ page contenttype = "application/MSWord" %> <br/> <! -- In the preceding line, set this webpage to an Excel webpage --> <br/> <% <br/> response. setheader ("content-disposition", "inline; filename=test1.doc"); // online browsing method <br/> // response. setheader ("content-disposition", "attachment; filename=test1.doc"); // download method <br/> // test1.doc is used when the file is set to be transferred to the browser. <br/> // This line is used, let the front-end browser assume that it receives a Word file <br/>%> <br/> // and then outputs dynamic content to get a Word file </P> <p> 1, open: <br/> 1) Add <% @ page contenttype = "application/MSWord" %> <br/> XML File: <br/> <mime-mapping> <br/> <extension> doc </extension> <br/> <mime-type> application/MSWord </mime-type> <br/> </mime-mapping> <br/> 2) you can use Js. The following code is for reference: <br/> <% @ page contenttype = "text/html; charset = gb2312" Import = "Java. io. * "%> <br/> <HTML> <br/> <MCE: Script Type =" text/JavaScript "> <! -- </P> <p> var WRD = new activexobject ("word. application ") <br/> WRD. visible = true <br/> alert ("your" + WRD. application. caption + "installation path:/N" + WRD. application. path + "/n version:" + WRD. application. version + "/N:" + WRD. application. username) <br/> WRD. documents. add () <br/> // WRD. documents. open ("C: // exam.doc") <br/> WRD. selection. typetext ("this is some text. ") <br/> WRD. application. activate () <br/> WRD. activedocument. savea S ("C: // exam111.doc") <br/> WRD = NULL <br/> // --> </MCE: SCRIPT> <br/> </ptml> </P> <p> 2. Download: <br/> <% @ page contenttype = "text/html; charset = gb2312 "Import =" Java. io. * "%> <br/> <% // get the file name and path <br/> string filename =" jsp.doc "; <br/> string filepath =" C: // "; </P> <p> // set the response header and the downloaded and saved file name <br/> response. setcontenttype ("application/OCTET-STREAM"); <br/> response. setheader ("content-disposition", "attachment; filenam E =/"" + filename + "/" "); </P> <p> // open the stream information of the specified file <br/> JAVA. io. fileinputstream = new Java. io. fileinputstream (filepath + filename); <br/> // fileoutputstream out = new fileoutputstream (filepath + "test //" + filename ); <br/> // write stream information <br/> int I; <br/> while (I = fileinputstream. read ())! =-1) {<br/> out. write (I); <br/>}< br/> fileinputstream. close (); <br/> out. close (); <br/>%>