IO流(二)

public class BufferWriterText01 {
    public static void main(String[] args) {
        //文件字节输出流 ----> 使用缓冲流(BufferReader) ----> 转换为文件字符输出流
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream("D:\\Text\\java01.text",true);
            OutputStreamWriter outputStream = new OutputStreamWriter(fileOutputStream);
            BufferedWriter bufferedWriter = new BufferedWriter(outputStream);
            bufferedWriter.write("HEllo,world\n");
            bufferedWriter.write("Number N0.1\n");

            bufferedWriter.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
public class FileInoutStreamText02 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("D:\\Text\\联系.txt");
            //一下程序有冗余
            /*while (true){
                int read = fis.read();
                if (read == -1){
                    break;
                }
                System.out.println(read);
            }*/

            int reaData = 0;
            while ((reaData = fis.read()) != -1){
                System.out.println(reaData);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
/*
* 1.文件字节输出流
* 2.内存---->硬盘
* 3.void	close()------关闭此文件输出流并释放与此流相关联的任何系统资源。
* 4.protected void	finalize()------清理与文件的连接,并确保当没有更多的引用此流时,将调用此文件输出流的 close方法。
* 5.FileChannel	getChannel()------返回与此文件输出流相关联的唯一的FileChannel对象。
* 6.FileDescriptor	getFD()------返回与此流相关联的文件描述符。
* 7.void	write(byte[] b)------将 b.length个字节从指定的字节数组写入此文件输出流。
* 8.void	write(byte[] b, int off, int len)------将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。
* 9.void	write(int b)------将指定的字节写入此文件输出流。
* */
public class FileOutputStreamText01 {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
            //加入true表示不更新源文件直接在源文件后面加入需要写入内容,不加true,表示需要先将源文件清空然后在重新写入新的字符
            fos = new FileOutputStream("D:\\Text\\联系.txt",true);
            //开始写文件
            String s = "我爱你,但是我现在需要变得更好!!!";
            //将字符串转换成byte数组
            byte[] byt = s.getBytes();
            fos.write(byt);
            //输出流必须加入刷新
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

/*
* 1.字符流,只能读取.text文件,读取文本文件方便快捷
* */
public class FileReaderText01 {
    public static void main(String[] args) {
        FileReader fir = null;
        try {
            //创建字符输入流对象
            fir = new FileReader("D:\\Text\\联系.txt");
            int ReadCount = 0;
            char[] chars = new char[4];
            while((ReadCount = fir.read(chars)) != -1){
                System.out.print(new String(chars,0,ReadCount));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fir != null) {
                try {
                    fir.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值