字节流和字符流的去别:
字节流直接操作文件本身
字符流操作使用了缓冲区,关闭字符流时会强制输出缓存区内容,否则缓冲区内容无法输出(也可以用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();
}
}