Operations on files in Java

Source: Internet
Author: User

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 /";

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.