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 );
}
}