Java 流

[size=medium]1.打印流(PrintStream、PrintWriter)[/size]

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);


[size=medium]2.字节流(InputStream、OutputStream)[/size]

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();


[size=medium]3.缓冲字符流(BufferedReader)[/size]

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();


[size=medium]4.对象序列化[/size]

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 + "]";
}
}
}


[size=medium]5.合并流(SequenceInputStream)[/size]

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();


[size=medium]6.压缩流(ZipOutputStream)[/size]

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();
}
}


[size=medium]7.org.apache.commons.io.FileUtils[/size]
API:[url]http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值