io流

1.打印流(PrintStream、PrintWriter)
String name = "apq"; int age = 100;
PrintStream out = new PrintStream(new FileOutputStream(new File("d:/1.dat")));
out.printf("Name: %s, Age: %s", name, age);



2.字节流(InputStream、OutputStream)
InputStream in = new FileInputStream("d:/in.txt");
OutputStream out = new FileOutputStream("d:/out.txt");
byte[] buff = new byte[1024];
int len = -1;
while ((len = in.read(buff)) != -1) {
	out.write(buff, 0, len);
}
out.flush(); out.close(); in.close();



3.缓冲字符流(BufferedReader)
File f = new File("d:/in.txt");
InputStream in = new FileInputStream(f);       
BufferedReader br = new BufferedReader(new InputStreamReader(in, "gbk"),10*1024*1024);
String data = "";
while((data = br.readLine()) != null) {
	System.out.println(data);
}
br.close(); in.close();



4.对象序列化

public class SerializableTest {

	public static void main(String[] args) throws Exception {
		OutputStream out = new FileOutputStream("d:/out.txt");
		ObjectOutputStream oos = new ObjectOutputStream(out);
		oos.writeObject(new Persion("apq"));
		oos.close(); out.close();
		
		InputStream in = new FileInputStream("d:/out.txt");
		ObjectInputStream ois = new ObjectInputStream(in);
		Persion p = (Persion) ois.readObject();
		System.out.println(p); //Persion [name=apq]
		ois.close(); in.close();
	}
	
	private static class Persion implements Serializable{
		private String name;

		public Persion(String name) {
			this.name = name;
		}

		public String toString() {
			return "Persion [name=" + name + "]";
		}
	}
}




5.合并流(SequenceInputStream)
InputStream ina = new FileInputStream("d:/a.htm");
InputStream inb = new FileInputStream("d:/b.htm");
OutputStream out = new FileOutputStream("d:/ab.html");
SequenceInputStream sis = new SequenceInputStream(ina, inb);
int c = 0;
while((c = sis.read()) != -1) {
	out.write(c);
}
out.flush();
sis.close();
ina.close(); inb.close();




6.压缩流(ZipOutputStream)
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

File file = new File("d:/zip_dir");
File zipFile = new File("d:/zip_dir.zip");
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
zipOut.setEncoding("gbk");
zip(zipOut, file, "zip_dir");
zipOut.close();

private static void zip(ZipOutputStream out, File f, String base) throws Exception {    
    if (f.isDirectory()) {    
        File[] fl = f.listFiles();    
        System.out.println("新增目录元素   " +base+ "/");    
        out.putNextEntry(new ZipEntry(base + "/"));    
        base = base.length() == 0 ? "" : base + "/";    
        for (int i = 0; i < fl.length; i++) {    
            zip(out, fl[i], base + fl[i].getName());    
        }    
    } else {    
        System.out.println("新增普通文件元素   " +base);    
        out.putNextEntry(new ZipEntry(base));    
        FileInputStream in = new FileInputStream(f);    
        int b;    
        while ((b = in.read()) != -1) {    
            out.write(b);    
        }    
        in.close(); 
    }    
}    


你可能感兴趣的:(java,apache,C++,c,F#)