Operation of files in Java

xiaoxiao2021-03-06  76

The IO class libraries are available in Java, which can easily use Java to implement various operations of the file. Here are how to use Java to implement these operations.

1. New directory

<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% string filepath = "c: / aaa /"; filepath = filepath.tostring (); // Chinese conversion java.io.file myfilepath = new Java.io.file (FilePath); if (! myfilepath.exists ()) MyFilePath.mkdir ();%>

2. New file <% @ page contentty = "text / html; charset = GB2312"%> <% @ page import = "java.io. *"%> <% string filepath = "c: / haha.txt"; filepath = filePath.toString (); File myFilePath = new File (filePath); if myFilePath.createNewFile () (myFilePath.exists ()!); FileWriter resultFile = new FileWriter (myFilePath); PrintWriter myFile = new PrintWriter (resultFile); String strContent = "Chinese test" .tostring (); myfile.Println (StrContent); ResultFile.Close ();%>

3. Delete file <% @ page contentty = "text / html; charset = GB2312"%> <% string filepath = "c: / spending certificate single .xls"; filepath = filepath.toString (); java.io.file myDelfile = New java.io.file (filepath); mydelfile.delete ();%>

4. Document copy <% @ Page ContentType = "text / html; charset = GB2312"%> <% @ page import = "java.io. *"%> <% int Bytesum = 0; int Byteread = 0; File: // Read the stream InputStream Instream = New FileInputStream ("C: /AAA.DOC"); FileOutputStream Fs = New FileOutputStream ("D: /AAA.DOC"); Byte [] Buffer = New Byte [1444]; int LENGTH; While (Byteread = Instream.Read (buffer))! = - 1) {Out.println ("

" byteread " "); bytesum = byteread; system. Out.println; fs.write (buffer, 0, byterad);} instream.close ();%>

5. Whole folder copy <% @ Page ContentType = "text / html; charset = GB2312"%> <% @ page import = "java.io. *"%> <% string url1 = "c: / aaa"; string URL2 = "D: / java /"; (new file (URL2)). mkdirs (); file [] file = (new file (url1)). listFiles (); for (int i = 0; i 6. File download <% @ Page ContentType = "text / html; charset = GB2312"%> <% @ page import = "java.io. *"%> <% string filename = "zsc104.swf" .tostring (); / / Read InputStream Instream = New FileInputStream ("C: /ZSC104.SWF"); // Setting the output format response.reset (); response.setContentType ("bin"); response.addheader ("Content-Disposition "," attachment; filename = / "" filename "/"); // looped data byte [] b = new byte [100]; int Len; while ((len = instream.read) b)> 0) Response.getOutputStream (). Write (b, 0, len); instream.close ();%> 7.

File in the database field download <% @ page contenttype = "text / html; charset = GB2312"%> <% @ page import = "java.sql. *"%> <% @ Page import = "java.lang. * "%> <% @ page import =" java.io. * "%> <% @ page import =" com.jspsmart.upload. * "%> <% @ page import =" dbstep.idbManager2000. * "%> <% int bytesum = 0; int byteread = 0; // open the database ResultSet result = null; String Sql = null; PreparedStatement prestmt = null; DBstep.iDBManager2000 DbaObj = new DBstep.iDBManager2000 (); DbaObj.OpenConnection (); / / Get the data in the database SQL = "SELECT * from t_local_zhongzhuan"; Result = DBAOBJ.EXECUTEQUERY (SQL); Result.next (); file: // Read data in the database InputStream Instream = Result.getBinaryStream "content"); fileoutputstream fs = new fileoutputStream ("c: /dffdsafd.doc"); byte [] buffer = new byte [1444]; int layout; while ((byteread = instream.read (buffer))! = - 1) {OUT.PRINTLN ("

" byteread " "); bytesum = Byteread; system.out.println (bytesum); fs.write (buffer, 0, Byteread);}%>

8. Save web pages into files <% @ Page Import = "Java.Text. *"%> <% @ Page import = "java.util. *"%> <% @ Page import = "java.io. *"%> <% @ page import = "java.net. *"%> <% URL stdurl = null; bufferedreader stdin = null; printwriter stdout = null; try {stdurl = new url (http://www.163.com); } catch (MalformedURLException e) {throw e;} try {stdIn = new BufferedReader (new InputStreamReader (stdURL.openStream ())); stdOut = new PrintWriter (new BufferedWriter (new FileWriter ( "c: /163.html")) );} catch (ioException e) {} / *** Read the page specified by the URL in the form of stream, write a specified file *** / try {string strHtml = ""; while ((strHtml = stdin.readline ())! = null) {stdout.println (strHTML);}}} catch (ooException e) {throw e;} finally {=} stdin.close (); if (stdout! = null ) stdout.close ();} catch (exception e) {system.out.println (e);}}%>

转载请注明原文地址:https://www.9cbs.com/read-120233.html

New Post(0)