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.exists ())
Myfilepath.createnewfile ();
FileWriter Resultfile = New FileWriter (MyFilePath);
PrintWriter myfile = new printwriter (resultfile);
String strcontent = "Chinese test" .tostring ();
myfile.println (strContent);
Resultfile.close ();
%>
3. Delete Files
<% @ Page ContentType = "text / html; charset = GB2312"%>
<%
String filepath = "c: / spending certificate. 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 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 ("
Bytesum = byteread;
System.out.println (Bytesum);
fs.write (buffer, 0, byteread);
}
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 IF (file [i] .isfile ()) { File [i] .tostring (); FileInputStream Input = New FileInputStream (file [i]); FileOutputStream Output = New FileoutputStream (URL2 "/" (file [i] .getname ()). TOSTRING ()); Byte [] b = new byte [1024 * 5]; Int Len; While ((len = INPUT.READ (B))! = - 1) { Output.write (B, 0, LEN); } Output.flush (); Output.close (); INPUT.CLOSE (); } } %> 6. file download <% @ Page ContentType = "text / html; charset = GB2312"%> <% @ Page Import = "java.io. *"%> <% String filename = "zsc104.swf" .tostring (); // Read the stream InputStream Instream = New FileInputStream ("C: /ZSC104.SWF"); // Set the format of the output Response.reset (); Response.setContentType ("bin"); Response.addheader ("Content-Disposition", "Attachment; FileName = /" " FileName " / ""); / / Loop the data in the stream Byte [] b = new byte [100]; Int Len; While ((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 (); / / Get the data in the database SQL = "SELECT * from T_Local_zhongzhuan"; Result = DBAOBJ.EXECUTEQUERY (SQL); Result.next (); File: // Read the data in the database into the stream InputStream Instream = Result.getBINARYSTREAM ("Content"); FileOutputStream Fs = New FileOutputStream ("C: /Dffdsafd.doc"); Byte [] buffer = new byte [1444]; Int length; While ((Byteread = Instream.read (buffer)! = - 1) { Out.println ("
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 (Malformedurlexcection E) {
Throw e;
}
Try {
Stdin = New BufferedReader (Stdurl.OpenStream ()));
Stdout = New PrintWriter (New FileWriter (New FileWriter ("C: C: /163.html")))))
}
Catch (IOException E) {
}
/ *** Take the page specified by the URL in the form of stream, write into a specified file *** /
Try {
String strHtml = "";
While ((strHtml = stdin.readline ())! = null) {
STDOUT.Println (strHTML);
}
}
Catch (IOException 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];
Int length;
While ((Byteread = Instream.read (buffer)! = - 1)
{
Out.println ("
Bytesum = byteread;
System.out.println (Bytesum);
fs.write (buffer, 0, byteread);
}
%>
10. Read file by operator
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% @ Page Import = "java.io. *"%>
<% FileReader myfilereader = new fileReader ("c: / haha.txt");
BufferedReader myBufferedReader = New BufferedReader (MyFileRead);
String mystring = NULL;
String resultstring = new string ();
While ((MyString = MyBufferedReader.Readline ())! = null) {resultString = resultString mystring "
";
Out.println (ResultString); myfilereader.close ();
%>