Java Basics-Day 21st

Source: Internet
Author: User

IO stream:

FileOutputStream

FileInputStream

Byte stream Copy File

Binary stream with byte stream input and output

public void NewLine (): Write line delimiter based on system platform

Public String ReadLine (): reads one row of data at a time

Code manipulation "Exercise"

1  Packagecn.itcast01;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.BufferedWriter;5 ImportJava.io.File;6 ImportJava.io.FileReader;7 ImportJava.io.FileWriter;8 ImportJava.io.FilenameFilter;9 Importjava.io.IOException;Ten  One  A /* - * Requirements: Copy the specified type file to the specified directory in the specified directory -  *  the * Ideas: - * A: Package source directory - * B: Traverse the source directory for each file that matches the specified type - * C: Rename copy: + * A: Rename first, copy later; - * B: Unified renaming after copy +  *  A * Promotion: Replication in multi-level directory at  */ -  Public classFileCopyDemo2 { -      Public Static voidMain (string[] args)throwsIOException { -         //A: Package source directory -File File =NewFile ("D:\\java\\javacode\\day1"); -          in         //B: Traverse the source directory for each file that matches the specified type -file[] files = file.listfiles (NewFilenameFilter () { to @Override +              Public BooleanAccept (File dir, String name) { -                 return NewFile (Dir,name). Isfile () && name.endswith (". Java"); the             }     *         }); $         Panax Notoginseng         //Create a directory -File DestFile =NewFile ("D:\\jad"); the         if(!destfile.exists ()) { +             BooleanFlag =Destfile.mkdir (); ASYSTEM.OUT.PRINTLN ("Create directory when no target directory:" +flag); the         } +          -         //C: Renaming replication: Unified naming after Replication $          for(File filetemp:files) { $String name = Filetemp.getname ();//gets the original specified file name -String newName = Name.replace (". Java", ". Jad");//need to create target directory -File NewFile =NewFile (Destfile,newname);//Create target directory the             //Copy Content -BufferedReader br =NewBufferedReader (NewFileReader (filetemp));WuyiBufferedWriter BW =NewBufferedWriter (NewFileWriter (NewFile)); the              -             Char[] CHS =New Char[1024]; Wu             intLen = 0; -              while(len = Br.read (CHS))!=-1){ AboutBw.write (chs,0, Len); $ Bw.flush (); -             } -         //Close Resource - bw.close (); A br.close (); +         } the     } -}
1  Packagecn.itcast03;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.BufferedWriter;5 ImportJava.io.File;6 ImportJava.io.FileReader;7 ImportJava.io.FileWriter;8 Importjava.io.IOException;9 Ten /** One * Traversal replication of the specified directory A  * @authorItcast -  * -  */ the  Public classFilecopydemo { -      Public Static voidMain (string[] args)throwsIOException { -         //encapsulates a file object -File Srcfile =NewFile ("D:\\java\\javacode\\day2"); +File DestFile =NewFile ("D:\\test"); -         if(!destfile.exists ()) { + destfile.mkdirs (); A         } at         //gets an array of file objects under the specified directory -file[] Arrayfiles =srcfile.listfiles (); -         //Traverse, and make judgments -         Char[] CHS =New Char[1024]; -         intLen = 0; -          for(File arrayfile:arrayfiles) { in             if(Arrayfile.isfile ()) { -String name = Arrayfile.getname ();//gets the original specified file name toFile Finalfile =NewFile (destfile, name);//Create target directory +                 //Create a replication input and output stream -BufferedReader br =NewBufferedReader (NewFileReader (Arrayfile)); theBufferedWriter BW =NewBufferedWriter (NewFileWriter (Finalfile)); *                  $                  while(len = Br.read (CHS))!=-1){Panax NotoginsengBw.write (chs,0, Len); - Bw.flush (); the                 } +                  A                 //Close Resource the bw.close (); + br.close (); -             } $         } $     } -}

Java Basics-Day 21st

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.