Common Java file read and write operations

Source: Internet
Author: User
Tags file copy

Now that the calculation has been done Java development for two years, back to think it is really not easy, Java things are more complex but if the basic work of the ability to upgrade quickly, this special finishing a bit about the file operation of the common code and everyone to share

1. Read the file (normal mode)

(1) The first method

[Java]View PlainCopy 
  1. File f=new file ("D:" +file.separator+"test.txt");
  2. InputStream in=New FileInputStream (f);
  3. Byte[] b=new byte[(int) f.length ()];
  4. int len=0;
  5. int temp=0;
  6. while ((Temp=in.read ())!=-1) {
  7. b[len]= (byte) temp;
  8. len++;
  9. }
  10. System.out.println (new String (b,0,len,"GBK"));
  11. In.close ();


This method seems to use a little more

(2) The second method

[Java]View PlainCopy 
    1. file f=new file ( "D:" +file.separator+
    2. inputstream in=new fileinputstream (f);   
    3. byte[] b=new byte[ 1024];  
    4. int len =0;  
    5. while ((len= In.read (b))!=-1) {  
    6.      System.out.println (new string (b,0,len, " GBK ")   
    7.  }  
    8. in.close ();   


2. File read (memory mapped mode)

[Java]View PlainCopy 
  1. File f=new file ("D:" +file.separator+"test.txt");
  2. FileInputStream in=New FileInputStream (f);
  3. FileChannel Chan=in.getchannel ();
  4. Mappedbytebuffer Buf=chan.map (FileChannel.MapMode.READ_ONLY, 0, F.length ());
  5. Byte[] b=new byte[(int) f.length ()];
  6. int len=0;
  7. while (Buf.hasremaining ()) {
  8. B[len]=buf.get ();
  9. len++;
  10. }
  11. Chan.close ();
  12. In.close ();
  13. System.out.println (new String (b,0,len,"GBK"));

The efficiency of this method is the best, the speed is also the fastest, because the program is directly operating the memory

3. File copy (side-read-write) operation

(1) The more common way

[Java]View PlainCopy 
  1. Package org.lxh;
  2. Import Java.io.File;
  3. Import Java.io.FileInputStream;
  4. Import Java.io.FileOutputStream;
  5. Import Java.io.InputStream;
  6. Import Java.io.OutputStream;
  7. Public class ReadAndWrite {
  8. public static void Main (string[] args) throws Exception {
  9. File f=new file ("D:" +file.separator+"test.txt");
  10. InputStream in=New FileInputStream (f);
  11. OutputStream out=New FileOutputStream ("e:" +file.separator+"test.txt");
  12. int temp=0;
  13. While ((Temp=in.read ())!=-1) {
  14. Out.write (temp);
  15. }
  16. Out.close ();
  17. In.close ();
  18. }
  19. }


(2) Implementation of using memory mapping

[Java]View PlainCopy 
    1. Package org.lxh;
    2. Import Java.io.File;
    3. Import Java.io.FileInputStream;
    4. Import Java.io.FileOutputStream;
    5. Import Java.nio.ByteBuffer;
    6. Import Java.nio.channels.FileChannel;
    7. Public class ReadAndWrite2 {
    8. public static void Main (string[] args) throws Exception {
    9. File f=new file ("D:" +file.separator+"test.txt");
    10. FileInputStream in=New FileInputStream (f);
    11. FileOutputStream out=New FileOutputStream ("e:" +file.separator+"test.txt");
    12. FileChannel Fin=in.getchannel ();
    13. FileChannel Fout=out.getchannel ();
    14. //Open buffer
    15. Bytebuffer buf=bytebuffer.allocate (1024);
    16. While ((Fin.read (BUF))!=-1) {
    17. //Reset buffer
    18. Buf.flip ();
    19. //Output buffers
    20. Fout.write (BUF);
    21. //Clear buffer
    22. Buf.clear ();
    23. }
    24. Fin.close ();
    25. Fout.close ();
    26. In.close ();
    27. Out.close ();
    28. }
    29. }

Common Java file read and write operations

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.