Operation example of [File] in Java

xiaoxiao2021-03-05  52

The IO class library is provided in Java's operation Java to 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; 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 (bytesum); 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 the specified file *** / try {string strHtml = ""; while ((strHtml = stdin) .readline ())! = null) {stdout.println (strHtml);}} catch (ooException e) {throw E;} finally {try {i (stdin! = null) stdin.close (); if (stdout! = NULL) stdout.close ();} catch (exception e) {system.out.println (e);}}%> 9. Download the online file <% @ page import = "java.io. *"%> <% @ Page import = "java.net. *"%> <% Int Bytesum = 0; int BytereAd = 0; URL URL = new URL ( "http://pimg.163.com/sms/micheal/logo.gif"); URLConnection conn = url.openConnection (); InputStream inStream = conn.getInputStream (); FileOutputStream fs = new FileOutputStream ( "c : /Abc.gif "); Byte [] buffer = new byte [1444]; intleth; while (Byteread = instream.read (buffer))! = - 1) {Out.println ("

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

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

New Post(0)