Summary in 10.26, pipeline stream usage, RandomAccessFile, DataStream to be further explored., randomaccessstream

Source: Internet
Author: User

Summary in 10.26, pipeline stream usage, RandomAccessFile, DataStream to be further explored., randomaccessstream

1 package test; 2 import java. io. *; 3 import java. nio. channels. fileChannel; 4 import java. util. *; 5 public class Test10_26 6 {7 public static void main (String [] args) throws Exception 8 {9 10} 11 public static void method_delete () 12 {13 File dir = new File ("D: \ testdir \ 111"); 14 removeDir (dir); 15} 16 private static void removeDir (File dir) 17 {18 File [] files = dir. listFiles (); 19 for (Fil E f: files) 20 {21 if (f. isDirectory () 22 {23 removeDir (f); 24} 25 else 26 {27 System. out. println (f. toString () + "-file-" + f. delete (); 28; 29} 30} 31 System. out. println (dir + "-dir-" + dir. delete (); 32; 33} 34 public static void method_PrintWriter () throws IOException 35 {36 BufferedReader bufr = new BufferedReader (new InputStreamReader (System. in); 37 PrintWriter out = new PrintWri Ter (new FileWriter ("d:/a.txt"), true); // refresh in real time, 38 for (String line = null; (line = bufr. readLine ())! = Null;) 39 {40 if ("over ". equals (line) 41 break; 42 System. out. println (line. toUpperCase (); 43 out. println (line. toUpperCase (); // do not worry about writing flush () 44} 45 bufr. close (); 46 out. close (); 47} 48 public static void method_SequenceInputStream_ArrayList () throws IOException 49 {50 ArrayList <InputStream> list = new ArrayList <InputStream> (); 51 File dir = new File ("D:/splitfiles"); 52 for (int I = 0; I <4; I ++) 53 {54 list. add (new FileInputStream (new File (dir, I + ". part "); 55} 56 final Iterator <InputStream> it = list. iterator (); 57 Enumeration <InputStream> en = new Enumeration <InputStream> () 58 {59 public InputStream nextElement () 60 {61 return it. next (); 62} 63 public boolean hasMoreElements () 64 {65 return it. hasNext (); 66} 67}; 68 SequenceInputStream sis = new SequenceInputStre Am (en); 69 BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream ("D:/splitfiles/1.mp4"); 70 int len = 0; 71 for (byte [] buf = new byte [1024*1024]; (len = sis. read (buf ))! =-1;) 72 {73 bos. write (buf, 0, len); 74 bos. flush (); 75} 76 sis. close (); 77 bos. close (); 78} 79 public static void method_splitfile () throws IOException 80 {81 File file = new File ("D:/222.mp4 "); 82 FileInputStream FCM = new FileInputStream (file); 83 FileOutputStream fos = null; 84 int len = 0; 85 int count = 0; 86 q: while (true) 87 {88 File f = new File ("D:/splitfiles/" + (count ++) + ". part "); 89 fos = new FileOutputStream (f); 90 for (byte [] buf = new byte [1024*1024]; (len = FCM. read (buf ))! =-1;) 91 {92 fos. write (buf, 0, len); 93 if (f. length ()> = 1024*1024*200) 94 {95 continue q; 96} 97} 98 break; 99} 100} 101 public static void method_ByteArrayOutputStream () throws Exception // 102 {103 FileInputStream FCM = new FileInputStream ("D:/111.bmp"); 104 BufferedInputStream bis = new BufferedInputStream (FCM); 105 ByteArrayOutputStream baos = new bytes (); 106 for (int ch = 0; (ch = bis. read ())! =-1;) 107 {108 baos. write (ch); 109} 110 bis. close (); 111 System. out. println (baos. size (); 112 BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream ("D:/111_copy.bmp"); 113 baos. writeTo (bos); // images are always stored in baos. Advantages !! 114 byte [] retArr = baos. toByteArray (); // you can use ByteArrayInputStream to read data. dumb 115 System. out. println (retArr. length); 116} 117 public static void method_SequenceInputStream () throws Exception118 {119 Vector <InputStream> vector = new Vector <InputStream> (); 120 vector. add (new FileInputStream ("D:/33.mp3"); 121 vector. add (new FileInputStream ("D:/44.mp3"); 122 vector. add (new FileInputStream ("D:/55.mp3"); 123 Enu Meration <InputStream> et = vector. elements (); 124 SequenceInputStream sis = new SequenceInputStream (et); 125 BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream ("D:/66.mp3"); 126 int len = 0; 127 for (byte [] buf = new byte [1024*1024]; (len = sis. read (buf ))! =-1;) 128 {129 bos. write (buf, 0, len); 130 bos. flush (); 131} 132 sis. close (); 133 bos. close (); 134} 135 public static void method_FileChannel () throws Exception136 {137 FileChannel in = new FileInputStream ("D:/171.bmp "). getChannel (); 138 FileChannel out = new FileOutputStream ("D:/172.16copy.bmp "). getChannel (); 139 out. transferFrom (in, 0, in. size (); 140 in. close (); 141 out. close (); 142} 143}

 




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.