Java reads TXT file copy file Write File

Source: Internet
Author: User
Tags file copy

/**
* Read TXT files
*/
Public void readertxt (){
List <string> emaillist = new arraylist <string> ();
List <string> emaillocationlist = new arraylist <string> ();
File file = new file ("C: // Documents and Settings // administrator // desktop // email2.txt ");
Try
{
Bufferedreader input = new bufferedreader (New filereader (File ));
String text;
While (text = input. Readline ())! = NULL)
// System. Out. println (text );
Emaillist. Add (text );
} Catch (exception ex ){}
If (emaillist! = NULL ){
For (INT I = 0; I <emaillist. Size (); I ++ ){
String emailstr = emaillist. Get (I );
If (emailstr! = NULL &&! "". Equals (emailstr )){
String [] emailarray = emailstr. Split (";");
For (Int J = 0; j <emailarray. length; j ++ ){
If (emailarray [J]! = NULL &&! "". Endswith (emailarray [J]) {
Emaillocationlist. Add (emailarray [J]);
}
}
}
}
}


For (INT I = 0; I <emaillocationlist. Size (); I ++ ){
System. Out. println (emaillocationlist. Get (I ));

}

}

 

 

/**
* Writing files
*/
Public void writerfile (){
File file = new file ("C: // Documents and Settings // administrator // desktop // successfully sent. txt ");
Try {
Filewriter fw = new filewriter (File );
FW. Write ("wangwei <wangwei.0822>/R/N ");
FW. Write ("wangwei <wangwei.0822>/R/N ");
FW. Flush ();
FW. Close ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

}
 
/**
* File copy
*/
Public void copyfile (){
File filei = new file ("D: // aaa.txt ");
File fileo = new file ("D: // bbb.txt ");
Try {
Fileinputstream in = new fileinputstream (filei );
Fileoutputstream out = new fileoutputstream (fileo );
Byte [] B = new byte [1024];
While (in. Read (B)>-1 )){
Out. Write (B );
}
In. Close ();
Out. Close ();
} Catch (filenotfoundexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
 

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.