Java Read and write

Source: Internet
Author: User

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

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.