File回顾


字节流和字符流的去别:

字节流直接操作文件本身

字符流操作使用了缓冲区,关闭字符流时会强制输出缓存区内容,否则缓冲区内容无法输出(也可以用flush)

//FileWrite 不是 Write的子类,而是转换流的子类 是 OutputStreamWrite的子类
//FileOutputStream 是 OutputStream 的直接子类
// out = new OutputStreamWrite(new FileOutputStream(file))
// out = new OutputStreamWrite(new FileOutputStream(file))
//不管是使用字节流还是字符流 实际上都是以字节的形式 操作输入输出的


内存操作流(ByteArrayInputStream)一般在生成临时信息才用

管道流 (PipedInputStream) 主要用于线程间的通信 输出流是管道的发送端


package com.xzl.files;


import java.io.IOException;


public class PipleInputStreamTest {
public static void main(String[] args){
PipeOutputStreamSend send = new PipeOutputStreamSend();
PipeInputStreamReceive receive = new PipeInputStreamReceive();
try {
send.getPos().connect(receive.pis());//连接管道
} catch (IOException e) {
e.printStackTrace();
}
new Thread(send).start();
new Thread(receive).start();
}
}


public class WriteTest {
public static void main(String[] args) throws Exception{
//第1步:使用file 找到文件
File file = new File("E:"+File.separator+"Test"+File.separator+"writeTest.txt");
//第2步:通过子类实例化父类对象
Writer out = null;
out = new FileWriter(file,true);
//第3步:写操作
String str ="合肥市的房顶上";
out.write(str);
//第4步:关闭输出流
out.close();
}
}


public class FileIO {
public static void main(String[] args) throws Exception{
/**
* IO操作的步骤
* 1.File打开一个文件
* 2.字节流 或 字符流  指定输出位置
* 3.进行读写操作
* 4.关闭输入输出
*/
//1.通过file 找到 一个文件
File file = new File("E:"+File.separator+"Test"+File.separator+"test3.txt");
//file.createNewFile();
//2.通过子类实例化父类对象
OutputStream ops = null; //准备输出对象流
ops = new FileOutputStream(file,true); //实例化,true表示追加内容
String str = "\r\n换行测试";
byte b[] = str.getBytes(); //只能输出byte数组,将字符串变为byte数组
ops.write(b);
ops.close();

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值