首页 > 其他 > 详细

文件字节流

时间:2018-08-22 14:43:41      阅读:192      评论:0      收藏:0      [点我收藏+]
FileOutputStream 输出流,写出内容到文件
FileInputStream 输入流,从文件读入内容到缓冲区
public class Demo {
    public static void main(String[] args) {
        File f = new File("D:/word.txt");//指定路径、文件(夹)名
        try {//异常处理
            FileOutputStream out = new FileOutputStream(f, true);//默认不追加,而是覆盖。
            String str = "与众不同";
            byte b[] = str.getBytes();//字符串转字节
            out.write(b);
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
        try {
            FileInputStream in = new FileInputStream(f);
            byte b2[] = new byte[200];//设定缓冲区大小,200字节
            int length = in.read(b2);//读入文本到缓冲区,返回文本字节数。
            System.out.println("文件内容是:" + new String(b2, 0, length));//0,length可以去除空格(未使用缓冲区)
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
    }
}

 



文件字节流

原文:https://www.cnblogs.com/xixixing/p/9517312.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!