Java Io stream operations, copying (multiple files), deleting, cutting, and downloading Network Resources

Source: Internet
Author: User

Public class fileutil {

// Copy an object
Public void copyfile (string source, string DEST ){
File sourcefile = new file (source );
File destfile = new file (DEST + "/" + sourcefile. getname ());

Try {
Fileinputstream FCM = new fileinputstream (sourcefile );
Fileoutputstream Fos = new fileoutputstream (destfile );
Bufferedinputstream Bis = new bufferedinputstream (FCM );
Bufferedoutputstream Bos = new bufferedoutputstream (FOS );

Int Len = 0;
Byte [] B = new byte [1024];
While (LEN = bis. Read (B ))! =-1 ){
Bos. Write (B, 0, Len );
}

Bos. Close ();
Bis. Close ();
FOS. Close ();
FCM. Close ();

} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}

// Copy files and folders
Public void copyfield (string source, string DEST ){
File sourcefile = new file (source );
// Copy a folder
If (sourcefile. isdirectory ()){
String [] names = sourcefile. List ();
File file = new file (DEST + "/" + sourcefile. getname ());
If (! File. exists ()){
File. mkdir ();
}
For (string name: names ){
Copyfield (source + "/" + name, file. getpath ());
}
}
// Copy a file
Else {
This. copyfile (source, DEST );
}
}

// Delete files and folders
Public void deletefield (string source ){
File sourcefile = new file (source );
// Folder
If (sourcefile. isdirectory ()){
String [] names = sourcefile. List ();
// Delete the subfiles and subfolders in the current folder
For (string name: names ){
Deletefield (source + "/" + name );
}
// Delete the current folder
Sourcefile. Delete ();
}
// File
Else {
Sourcefile. Delete ();
}
}

// Cut the file and folder
Public void cutfield (string source, string DEST ){
// Copy a folder
// Delete a folder
}

// Download resources from the network
Public void download (URL ){
Try {
Inputstream is = URL. openstream ();
Fileoutputstream Fos = new fileoutputstream ("E:/123.gif ");
Bufferedinputstream Bis = new bufferedinputstream (is );
Bufferedoutputstream Bos = new bufferedoutputstream (FOS );

Byte [] B = new byte [1024];
Int Len = 0;
While (LEN = bis. Read (B ))! =-1 ){
Bos. Write (B, 0, Len );
}

Bos. Close ();
Bis. Close ();
FOS. Close ();
Is. Close ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

}

}

Import java. Io. bufferedreader;

Import java. Io. fileinputstream;

Import java. Io. fileoutputstream;

Import java. Io. filereader;

Import java. Io. filewriter;

Import java. Io. inputstreamreader;

Import java. Io. printwriter;

 

Public class iotest {

Public staticvoid main (string [] ARGs) throws exception {

Stringstr = "Chinese ";

/* Fileoutputstreamfos = newfileoutputstream ("1.txt ");

FOS. Write (Str. getbytes ("UTF-8 "));

FOS. Close ();*/

/* Filewriterfw = new filewriter ("1.txt ");

FW. Write (STR );

FW. Close ();*/

Printwriterpw = new printwriter ("1.txt"," UTF-8 ");

PW. Write (STR );

PW. Close ();

/* Filereaderfr = new filereader ("1.txt ");

Char [] Buf = new char [1024];

Int Len = Fr. Read (BUF );

Stringmystr = new string (BUF, 0, Len );

System. Out. println (mystr );*/

/* Fileinputstreamfr = new fileinputstream ("1.txt ");

Byte [] Buf = new byte [1, 1024];

Int Len = Fr. Read (BUF );

Stringmystr = new string (BUF, 0, Len, "UTF-8 ");

System. Out. println (mystr );*/

Bufferedreaderbr = new bufferedreader (

Newinputstreamreader (

Newfileinputstream ("1.txt")," UTF-8"

)

);

Stringmystr = Br. Readline ();

BR. Close ();

System. Out. println (mystr );

}

 

}

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.