第20天-15-IO流(打印输出流)
打印输出流:PrintWriter与PrintStream
两者的区别:Since JDK 1.4 it's possible to specify the character encoding for a PrintStream. Thus, the differences between PrintStream and PrintWriter are only about auto flushing behavior and that a PrintStream cannot wrap a Writer.
package bxd; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; /* 打印输出流: 该流提供了打印方法, 可以将各种类型的数据都原样打印 PrintStream构造函数可以接受的参数类型: 1. File对象 File 2. 字符串路径 String 3. 字节输出流 OutputStream PrintWriter构造函数可以接受的参数类型(由于可接受的参数类型更多, 因而更常用一些): 1. File对象 File 2. 字符串路径 String 3. 字节输出流 OutputStream 4. 字符输出流 Writer */ public class PrintStreamDemo { public static void main(String[] args) throws IOException { BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out, true); String line = null; while ((line = bufr.readLine()) != null) { if ("over".equals(line)) break; out.println(line.toUpperCase()); } out.close(); bufr.close(); } }
第20天-16-IO流(合并流-SequenceInputStream)
package bxd; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.SequenceInputStream; import java.util.Enumeration; import java.util.Vector; public class SequenceDemo { public static void main(String[] args) throws IOException { Vector<FileInputStream> v = new Vector<FileInputStream>(); v.add(new FileInputStream("info.txt")); v.add(new FileInputStream("list.txt")); Enumeration<FileInputStream> en = v.elements(); SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("uni.txt"); byte[] buf = new byte[1024]; int len = 0; while ((len = sis.read(buf)) != -1) { fos.write(buf, 0, len); } fos.close(); sis.close(); } }
第20天-17-IO流(切割文件)
package bxd; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.SequenceInputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.Iterator; public class SplitFile { public static void splitFile() throws IOException { FileInputStream fis = new FileInputStream("ts.png"); FileOutputStream fos = null; byte[] bytes = new byte[600 * 1024]; int len = 0; int count = 1; while ((len = fis.read(bytes)) != -1) { fos = new FileOutputStream((count++) + ".part"); fos.write(bytes, 0, len); fos.close(); } fis.close(); } public static void merge() throws IOException { ArrayList<FileInputStream> al = new ArrayList<FileInputStream>(); for (int x = 1; x <= 4; x++) al.add(new FileInputStream(x + ".part")); final Iterator<FileInputStream> it = al.iterator(); Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() { @Override public boolean hasMoreElements() { return it.hasNext(); } @Override public FileInputStream nextElement() { return it.next(); } }; SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("rets.png"); byte[] bytes = new byte[1024]; int len = 0; while ((len = sis.read(bytes)) != -1) fos.write(bytes, 0, len); fos.close(); sis.close(); } public static void main(String[] args) throws IOException { merge(); } }