Java File Operations

Source: Internet
Author: User

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: // readTo 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 /";
(New file (url2). mkdirs ();
File [] file = (new file (url1). listfiles ();
For (INT I = 0; I <file. length; 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 [1, 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 ();
// Reading the stream
Inputstream instream = new fileinputstream ("C:/zsc104.swf ");
// Set the output format
Response. Reset ();
Response. setcontenttype ("bin ");
Response. addheader ("content-disposition", "attachment; filename =/" "+ filename + "/"");
// Cyclically retrieve 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. Download files from database fields

<% @ 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 ();
// Obtain data in the database
SQL = "select * From t_local_zhongzhuan ";
Result = dbaobj. executequery (SQL );
Result. Next ();

File: // read data from the database to 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 ("<DT> <B>" + byteread + "</B> </DT> ");
Bytesum + = byteread;
System. Out. println (bytesum );
FS. Write (buffer, 0, byteread );
}
%>

8. Save a webpage as a file

<% @ 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 as a stream and write it as 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 online files 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 ("<DT> <B>" + byteread + "</B> </DT> ");
Bytesum + = byteread;
System. Out. println (bytesum );
FS. Write (buffer, 0, byteread );
}
%>

10. Read files by row
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. Io. *" %>
<%
Filereader myfilereader = new filereader ("C:/harhat.txt ");
Bufferedreader mybufferedreader = new bufferedreader (myfilereader );
String mystring = NULL;
String resultstring = new string ();
While (mystring = mybufferedreader. Readline ())! = NULL)
{Resultstring = resultstring + mystring + "<br> ";
}
Out. println (resultstring );
Myfilereader. Close ();
%>

11. The field files in the database are directly downloaded to the client.
<% @ Page import = "Java. SQL. *" %>
<% @ Page import = "Java. Lang. *" %>
<% @ Page import = "Java. Io. *" %>
<% @ Page import = "com. jspsmart. Upload. *" %>
<% @ Page import = "dbstep. idbmanager2000. *" %>
<%
String filename = "bb.doc". tostring ();
// Open the database
Resultset result = NULL;
String SQL = NULL;
Preparedstatement prestmt = NULL;
Dbstep. idbmanager2000 dbaobj = new dbstep. idbmanager2000 ();
Dbaobj. openconnection ();
// Obtain data in the database
SQL = "select * From marklist order by markdate DESC ";
Result = dbaobj. executequery (SQL );
Result. Next ();
// Read data from the database to the stream
Inputstream in = result. getbinarystream ("markbody ");
// Set the output format
Response. Reset ();
Response. setcontenttype ("application/MSWord ");
Response. addheader ("content-disposition", "attachment; filename =/" "+ filename + "/"");
// Cyclically retrieve the data in the stream
Byte [] B = new byte [1024];
Int Len;
While (LEN = in. Read (B)> 0)
Response. getoutputstream (). Write (B, 0, Len );
In. Close ();
%>

12. Folder Traversal
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. Io. *" %>
<%
String url1 = "C:/AAA ";
File F = (new file (url1 ));
If (F. isdirectory ()){
File [] Fe = f. listfiles ();
Go_on:
For (INT I = 0; I <Fe. length; I ++ ){
If (Fe [I]. isdirectory ()){
File [] fe1 = Fe [I]. listfiles ();
For (Int J = 0; j <fe1.length; j ++ ){
If (fe1 [J]. isdirectory ())
Continue go_on;
Out. println (fe1 [J]. tostring ());
}
}
Else Out. println (Fe [I]. tostring ());
}
}

%>
13. Move a file by character encoding
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. Io. *" %>
<%
String ret = new string ();
Try
{
Byte [] bytes = new byte [1, 102400];
Inputstream in = new fileinputstream ("C:/aaa.doc ");
In. Read (bytes );
Ret = new sun. Misc. base64encoder (). encode (bytes); // specific encoding method
In. Close ();
}
Catch (filenotfoundexception E)
{
E. printstacktrace ();
}
Catch (Java. Io. ioexception ex)
{
Ex. printstacktrace ();
}
Out. println (RET );

Byte [] bytes = new sun. Misc. base64decoder (). decodebuffer (RET );
Java. Io. bytearrayinputstream instream = new java. Io. bytearrayinputstream (bytes );
Byte [] buffer = new byte [1444];
Fileoutputstream FS = new fileoutputstream ("D:/aaa.doc ");
Int bytesum = 0;
Int byteread = 0;
While (byteread = instream. Read (buffer ))! =-1)
{
Bytesum + = byteread;
FS. Write (buffer, 0, byteread );
}
%>

14. Encode the file into a base64 string
<%
String ret = new string ();
Byte [] bytes = new byte [1, 1024];
String AA = "aaaa ";
Bytes = AA. getbytes ();
Ret = new sun. Misc. base64encoder (). encode (bytes); // specific encoding method
Bytes = new sun. Misc. base64decoder (). decodebuffer (RET );
AA = new string (bytes );
Out. println (AA );
%>

12. Folder Traversal
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. Io. *" %>
<%
String url1 = "C:/AAA ";
File F = (new file (url1 ));
If (F. isdirectory ()){
File [] Fe = f. listfiles ();
Go_on:
For (INT I = 0; I <Fe. length; I ++ ){
If (Fe [I]. isdirectory ()){
File [] fe1 = Fe [I]. listfiles ();
For (Int J = 0; j <fe1.length; j ++ ){
If (fe1 [J]. isdirectory ())
Continue go_on;
Out. println (fe1 [J]. tostring ());
}
}
Else Out. println (Fe [I]. tostring ());
}
}

%>
13. Move a file by character encoding
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. Io. *" %>
<%
String ret = new string ();
Try
{
Byte [] bytes = new byte [1, 102400];
Inputstream in = new fileinputstream ("C:/aaa.doc ");
In. Read (bytes );
Ret = new sun. Misc. base64encoder (). encode (bytes); // specific encoding method
In. Close ();
}
Catch (filenotfoundexception E)
{
E. printstacktrace ();
}
Catch (Java. Io. ioexception ex)
{
Ex. printstacktrace ();
}
Out. println (RET );

Byte [] bytes = new sun. Misc. base64decoder (). decodebuffer (RET );
Java. Io. bytearrayinputstream instream = new java. Io. bytearrayinputstream (bytes );
Byte [] buffer = new byte [1444];
Fileoutputstream FS = new fileoutputstream ("D:/aaa.doc ");
Int bytesum = 0;
Int byteread = 0;
While (byteread = instream. Read (buffer ))! =-1)
{
Bytesum + = byteread;
FS. Write (buffer, 0, byteread );
}
%>

14. Encode the file into a base64 string
<%
String ret = new string ();
Byte [] bytes = new byte [1, 1024];
String AA = "aaaa ";
Bytes = AA. getbytes ();
Ret = new sun. Misc. base64encoder (). encode (bytes); // specific encoding method
Bytes = new sun. Misc. base64decoder (). decodebuffer (RET );
AA = new string (bytes );
Out. println (AA );
%>

 

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.