学习笔记6

来源:互联网 发布:淘宝网购物帽子 编辑:程序博客网 时间:2024/06/05 06:03

IO流
文件的创建
String path = “D:”+File.separator+”text”+File.separator+”test.text”;
fdir.delete();//删除文件夹

输入字节流
nputStream in = new FileInputStream(f);

输出字节流
OutputStream out = new FileOutputStream(f);
F后面加true表示追加

输出字符流
Writer out = new FileWriter(f);

输入字符流
FileReader in = new FileReader(f);

字节流和字符流的区别
字符流使用了缓冲区(即内存中的一块区域),可以用不关闭流。
out.flush():强制清空缓冲区,这样在不关闭字符流的情况下也可以将数据写入到文件中。
开发中字节流使用较多

内存操作流
通过ByteArrayInputStream的构造方法向内存写入数据
ByteArrayInputStream bis = new ByteArrayInputStream(t.getBytes());

创建读内存的流
ByteArrayOutputStream bos = new ByteArrayOutputStream();

转换流
将输出的字符流转化为字节流
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(f));

将输入的字节流转化为字符流
InputStreamReader isw = new InputStreamReader(new FileInputStream(f));

管道流
//发送端

class Person implements Runnable{    private PipedOutputStream pos= null;    public  Person(){        pos = new PipedOutputStream();    }    public void run() {        String l = "师姐好漂亮";        pos.write(l.getBytes());        pos.close();                            }    public PipedOutputStream getPos(){        return pos;     }}

//接收端

class Spook implements Runnable{    PipedInputStream pis = null;    public Spook() {        this.pis = new PipedInputStream();    }    public void run() {        byte[] b = new byte[1024];        int length = 0;        length = pis.read(b);        pis.close();        System.out.print(new String(b,0,length));    }    public PipedInputStream getPis(){        return pis;    }   }

打印流

字节打印流
PrintStream ps = new PrintStream(new FileOutputStream(f));

字符打印流
PrintWriter pw = new PrintWriter(new FileWriter(f));

System.out“标准”输出流
定义:static PrintStream out
OutputStream ops = System.out
ops.write(“wo shi haoren”.getBytes());//向显示器输出
err在IDE中一般显示为红色

输入输出的重定向
static void setErr(PrintStream err) 重新分配“标准”错误输出流。
static void setIn(InputStream in) 重新分配“标准”输入流。
static void setOut(PrintStream out) 重新分配“标准”输出流。

合并流
SequenceInputStream
合并两个文件
SequenceInputStream(InputStream s1, InputStream s2)
合并多个需要使用集合

Properties集合类配置文件

Properties prs = new Properties();        prs.setProperty("os-name", "windows");        prs.setProperty("os-version", "win7");        prs.setProperty("os-time", "2012-12-2");

序列化必须实现Serializable接口