Blog: Http://blog.csdn.net/muyang_ren
1. Quickly read a text file and output the contents of the file to a new file;
PackageLhy.java_day3.oop;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.FileNotFoundException;ImportJava.io.FileReader;ImportJava.io.FileWriter;ImportJava.io.IOException; Public class test_bufferead { Private StaticBufferedReader Bfreader;Private StaticBufferedWriter Bfwriter;Private StaticString str;/** * @param args * * Public Static void Main(string[] args) {//TODO auto-generated method stub Try{Bfreader =NewBufferedReader (NewFileReader ("F:/java_day3.txt")); Bfwriter =NewBufferedWriter (NewFileWriter ("F:/write.txt")); while((Str=bfreader.readline ())! =NULL) {System.out.println (str); Bfwriter.write (str+"\ r \ n"); Bfwriter.flush (); } }Catch(FileNotFoundException e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); } }}
2. List of files in a directory with a suffix of ". Java"
PackageLhy.java_day3.oop;ImportJava.io.File; Public class test_file { Private StaticFile FS;/** * @param args * * Public Static void Main(string[] args) {String pathname ="E:\\test";Try{fs =NewFile (pathname); String[] Str=fs.list (); for(String string:str) {if(String.endswith (". Java")) System.out.println (string); } }Catch(NullPointerException e) {//Todo:handle exceptionSystem.out.println ("failed to open Directory!"); } }}
3. Copying picture files
PackageLhy.java_day3.oop;ImportJava.io.FileInputStream;ImportJava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.IOException; Public class test_picture { Private StaticFileInputStream fis;Private StaticFileOutputStream Fos;Private Static intRetPrivate Static byte[] B =New byte[2048];/** * @param args * * Public Static void Main(string[] args) {//TODO auto-generated method stub Try{FIS =NewFileInputStream ("F:\\tu1.jpg"); FOS =NewFileOutputStream ("F:\\tu2.jpg"); while(true) {ret = Fis.read (b,0, b.length);if(ret!=-1) {Fos.write (b,0, ret); }Else{ Break; } } }Catch(FileNotFoundException e) {//TODO auto-generated catch blockE.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); } }}
Java learning "File IO"