字节打印流PrintStream,字符打印流PrintWriter,序列流SequenceInputStream、

PrintStream:字节打印流
  1 提供了打印方法可以对多种数据类型值进行打印。并保持数据的表示形式
  2 打印方法不抛IOException
  
  构造函数,接收三种类型的值:
  1 字符串路径
  2 File对象
  3 字节输出流
 PrintStream out = new PrintStream("print.txt");
out.write(610);//只写最低8位
out.print(97);//将97先变成字符保持原样将数据打印到目的地。
out.close();

PrintWriter:字符打印流。
 * 构造函数参数:
 * 1 字符串路径
 * 2 File对象
 * 3 字节输出流
 * 4 字符输出流

例:将键盘输入的值打印到控制台
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
String line = null;
while((line = buf.readLine())!=null){
if("over".equals(line))
break;
pw.println(line.toUpperCase());
pw.flush();
}
buf.close();
pw.close();

序列流:合并多个流

Vector<FileInputStream> v = new Vector<FileInputStream>(); 

 v.add(newFileInputStream("1.txt"));
  v.add(new FileInputStream("2.txt"));
  v.add(new FileInputStream("3.txt"));
  Enumeration<FileInputStream> en = v.elements();

SequenceInputStream sis = new SequenceInputStream(en);// 构造方法接收的是枚举
FileOutputStream fos = new FileOutputStream("4.txt");
byte[] bus = new byte[1024];
int len = 0;
while ((len = sis.read(bus)) != -1) {
fos.write(bus, 0, len);
}
sis.close();
fos.close();

迭代与枚举:

ArrayList<FileInputStream> a = new ArrayList<FileInputStream>();
for (int x = 0; x < 3; x++) {
a.add(new FileInputStream(x + ".txt"));
}
Enumeration<FileInputStream> en = Collections.enumeration(a);// 集合框架工具类中的方法


原理:
/*final Iterator<FileInputStream> it = a.iterator();
Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() {
public boolean hasMoreElements() {
return it.hasNext();
}
public FileInputStream nextElement() {
return it.next();
}
};*/



SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("4.txt");
byte[] bus = new byte[1024];
int len = 0;
while ((len = sis.read(bus)) != -1) {
fos.write(bus, 0, len);
}
sis.close();
fos.close();


文件切割器:
* 思路:用一个读取流,连接文件。
* 用多个输出流,按大小写入到别的文件

public static void splitFile(File file) throws IOException {
//用读取流关联源文件
FileInputStream fis = new FileInputStream(file);
//创建1m缓冲区
byte[] bus = new byte[1024*1024];
//创建目的
FileOutputStream fos = null;
int len = 0;
int count = 1;
while((len=fis.read(bus))!=-1){
fos = new FileOutputStream((count++)+".part");
fos.write(bus, 0, len);

}
fos.close();
fis.close();
}



序列化接口:

Serializable:用于给被序列化的类加入ID号。

用于判断类和对象是否是同一版本。

强烈建议显示声明一个ID号


静态数据与瞬态不能被序列化:

transient:非静态数据不想被序列化可以使用这个关键字修饰。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值