首页 > 代码库 > 10.26的总结, 管道流用法, RandomAccessFile, DataStream 有待深入.
10.26的总结, 管道流用法, RandomAccessFile, DataStream 有待深入.
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 (File 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 PrintWriter(new FileWriter("d:/a.txt"), true); // 实时刷新好啊, 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());// 不用操心 写不写 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 SequenceInputStream(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 fis = 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 = fis.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 fis = new FileInputStream("D:/111.bmp");104 BufferedInputStream bis = new BufferedInputStream(fis);105 ByteArrayOutputStream baos = new ByteArrayOutputStream();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); // baos里一直存着图片, 优点!!114 byte[] retArr = baos.toByteArray(); // 可以用ByteArrayInputStream 读取呀.笨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 Enumeration<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:/171_copy.bmp").getChannel();139 out.transferFrom(in, 0, in.size());140 in.close();141 out.close();142 }143 }
10.26的总结, 管道流用法, RandomAccessFile, DataStream 有待深入.
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。