Operation of files in Java

zhaozj2021-02-17  54

Operation of files in Java

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. create a new file

<% @ Page ContentType = "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 Files

<% @ Page ContentType = "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. File copy

<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> <% @ Page Import = "Java.io. *"%> <% int Bytesum = 0; int Byteread = 0; file: // Read InputStream Instream = New FileInputStream ("c: /aaa.doc"); fileoutputstream fs = new fileoutputstream ("d: /aaa.doc"); Byte [] buffer = new byte [1444]; int lay; (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 Input 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; if ((len = instream.read (b) )> 0) Response.getOutputStream (). Write (b, 0, len); instream.close ();%>

7. Document download in the database field

<% @ 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 (); // acquired data in the database Sql = "select * from t_local_zhongzhuan"; result = DbaObj.ExecuteQuery (Sql); result.next (); file: // read the data in the database stream InputStream inStream = result.getBinaryStream ( "content"); FileOutputStream FS = New FileOutputStream ("c: /dffdsafd.doc");

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);}%>

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) {} / *** Take out the page specified by the URL in the form of stream, write to the specified file *** / try {string strHtml = ""; while ((strHtml = stdin.readline ())! = Null) {stdout.println (STRHTML);}} catch (ooException e) {throw e;} finally {try {if (stdin! = null) stdin.close (); if (stdout! = null) stdout.close ();} catch (Exception e) {system.out.println (e);}}%>

9. Download the online file directly

<% @ 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);}%>

These are some of my accumulated Java's operations on files, I hope to be a little inspirated to everyone.

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

New Post(0)