IO streams are divided into byte stream and character stream, and each stream is divided into input and output and read and write.
The two base classes for a byte stream are InputStream and outputstream.
Character stream for reader and writer
General buffered Stream read-write characters:
BufferedReader br = new BufferedReader (New FileReader ("B.txt"));
If you have an encoded format, use:
BufferedReader br = new BufferedReader (new InputStreamReader (New FileInputStream ("D:\\test\\xiong.txt"), "GBK"));
Reads the contents of a text, and randomly outputs a line of content.
Public Static voidMain (string[] args)throwsIOException {bufferedreader br=NewBufferedReader (NewInputStreamReader (NewFileInputStream ("D:\\test\\xiong.txt"), "GBK")); ArrayList<String> list =NewArraylist<string>(); String Line=NULL; while((Line=br.readline ())! =NULL) {List.add (line); } intRandom =NewRandom (). Nextint (List.size ()); System.out.println (List.get (random)); }
Copy the files under the folder, there are photos in the file, video so use the byte stream:
Packagecn;ImportJava.io.*; Public classFileDemo2 { Public Static voidMain (string[] args)throwsIOException {File Srcfolder=NewFile ("D:\\demo"); File Destfolder=NewFile ("D:\\test"); if(!destfolder.exists ()) {Destfolder.mkdir (); } file[] Files=Srcfolder.listfiles (); for(file file:files) {file NewFile=NewFile (Destfolder, File.getname ()); Copy (file, newFile); } } Private Static voidCopy (file file, file NewFile)throwsioexception{Bufferedinputstream bis=NewBufferedinputstream (Newfileinputstream (file)); Bufferedoutputstream Bos=NewBufferedoutputstream (NewFileOutputStream (newFile)); intLen = 0; byte[] Bys =New byte[1024]; while((Len=bis.read (bys))!=-1) {bos.write (bys,0, Len); }
Bis.close ()
Bos.close ()
}}
Copy the text file that ends in. Java to another folder under one folder and end with a. Jad instead
Packagecn;ImportJava.io.*;Importjava.util.ArrayList;ImportJava.util.Random;/*** Created by Administrator on 2015/3/11.*/ Public classFiledemo { Public Static voidMain (string[] args)throwsIOException {File Srcfolder=NewFile ("C:\\java"); File Destfolder=NewFile ("C:\\jad"); if(!(Destfolder.exists ())) {Destfolder.mkdir (); } file[] Filearray= Srcfolder.listfiles (NewFilenameFilter () {@Override Public BooleanAccept (File dir, String name) {return NewFile (dir, name). Isfile () &&name.endswith (". Java"); } }); for(File file:filearray) {String name=File.getname (); File NewFile=NewFile (Destfolder, name); Copy (file, newFile); } file[] Destfilearray=Destfolder.listfiles (); for(File destfile:destfilearray) {System.out.println (destfile); String name=Destfile.getname (); String NewName= Name.replace (". Java", ". Jad"); File NewFile=NewFile (Destfolder, newName); Destfile.renameto (NewFile); } } Private Static voidCopy (file file, file NewFile)throwsioexception{Bufferedinputstream bis=NewBufferedinputstream (Newfileinputstream (file)); Bufferedoutputstream Bos=NewBufferedoutputstream (NewFileOutputStream (newFile)); intLen = 0; byte[] Bys =New byte[1024]; while((Len=bis.read (bys))!=-1) {bos.write (bys,0, Len); } bis.close (); Bos.close (); }}
Copy all content under a folder to the specified directory
Packagecn;ImportJava.io.*;Importjava.util.ArrayList;ImportJava.util.Random;/*** Created by Administrator on 2015/3/11.*/ Public classFiledemo { Public Static voidMain (string[] args)throwsIOException {File srcfile=NewFile ("D:\\demos"); File DestFile=NewFile ("c:\\"); CopyFolder (Srcfile, destfile); } Private Static voidCopyFolder (file srcfile, file destfile)throwsioexception{if(Srcfile.isdirectory ()) {File NewFolder=NewFile (DestFile, Srcfile.getname ()); Newfolder.mkdir (); File[] Filearray=Srcfile.listfiles (); for(file File:filearray) {copyfolder (file, NewFolder); } }Else{File NewFile=NewFile (DestFile, Srcfile.getname ()); CopyFile (Srcfile, newFile); } } Private Static voidCopyFile (file srcfile, file newFile)throwsioexception{bufferedoutputstream Bos=NewBufferedoutputstream (NewFileOutputStream (newFile)); Bufferedinputstream bis=NewBufferedinputstream (NewFileInputStream (srcfile)); intLen = 0; byte[] Bys =New byte[1024]; while((Len=bis.read (bys))!=-1) {bos.write (bys,0, Len); } bis.close (); Bos.close (); }}
Java Read and write