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

(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:/ha.txt .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 ();

%>

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.