Operations on files in Java
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.
1. Create 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 file
<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "java. io. *" %>
<%
String filePath = "c:/harhat.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 an object
<% @ Page contentType = "text/html; charset = gb2312" %>
<%
String filePath = "c:/withdrawal ticket .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 to 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 ("<DT> <B>" + byteread + "</B> </DT> ");
Bytesum + = byteread;
System. out. println (bytesum );
Fs. write (buffer, 0, byteread );
}
InStream. close ();
%>
5. Copy the entire folder
<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "java. io. *" %>
<% String url1 = "C:/aaa ";
String url2 = "d:/java /";