我们都知道流分为 字节流和字符流

 输出流又分:字节输出流,字符输出流

 输入流又分:字节输入流,字符输入流

/*在java中OutputStream表示字节输出流,可以将java程序中的数据写到文件中。
         OutputStream是所有字节输出流的顶层父类,是一个抽象类,如果要用,需要使用子类。
         常用的子类FileOutputStream
         构造方法:
         FileOutputStream​(File file): 传递File类型的文件。
         FileOutputStream​(String name): 传递一个字符串类型的文件路径。
         常用方法:
         void write​(int b): 向文件中写入一个字节。
         void write​(byte[] b): 向文件中写入一个字节数组。
         void write​(byte[] b, int off, int len): 向文件中写入字节输入的一部分。
         void close​(): 释放资源.
         FileOutputStream的使用步骤
         1. 创建一个FileOutputStream对象,并且指定一个目的地文件
         2. 调用write方法,向文件中写入数据。
         3. 释放资源(关闭流)
         注意: 在java中,一个字符占两个字节,但是如果这个字符在ASCII码范围内
         ,那么这个字符在计算机
         中是占一个字节的,可以以一次写一个字节的方式写入到文件中.
         中文在操作系统中占多个字节的。 如果文件采用的是GBK编码,那么这个中文就占2个字节。
         如果文件采用的是UTF-8编码,那么这个中文占3个字节。
         */
public class Demo02OutputStream {
    public static void main(String[] args) throws IOException {
        byte arr[] = new byte[]{12, 13, 13, 45};
        //创建一个FileOutputStream对象,并且指定一个目的文件
        FileOutputStream fs = new FileOutputStream("file01.text");
        //调用write方法,向文件中写入数据
        //写入byte【】类型
        fs.write(arr);
        //写入BYTE【】类型,从几号开始到几号索引结束
        fs.write(arr, 1, arr.length - 1);
        // System.out.println(fs);
        //如果不释放资源,那么如果这个程序不结束的话,这个资源会一直处于被占用的状态.
        //3. 释放资源(关闭流)
        fs.close();
 
    }
}
/*
        字符串和字节输出的相互转换。
        字符串->字节数组
        byte[] getBytes​(): 使用平台默认的编码方式将字符串转成字节数组。
        字节数组 -> 字符串
        String​(byte[] bytes): 将字节数组转成一个字符串
        String​(byte[] bytes, int offset, int length): 将字节数组的一部分转成String。
        参数bytes: 表示要转换的字节数组
        参数offset: 表示从哪个位置开始转
        参数length: 表示转几个.
        */
public class Demo03Parse {
    public static void main(String[] args) {
        //定义一个字符串
        String s="abcde";
        //调用方法把这个字符串转换为字节数组
        byte[] arr=s.getBytes();
        System.out.println(Arrays.toString(arr));
 
        String s1="java爱好者";
        byte[]arr1=s1.getBytes();
        System.out.println(Arrays.toString(arr1));//从打印结果可以看到文字占了三个字节
 
        System.out.println("---------------------");
 
        //字节数组转换为字符串
 
        String str1=new String(arr);
        System.out.println("str1:"+str1);
 
        String str2=new String(arr,1,arr.length-2);
        System.out.println(str2);
    }
}
/*
    使用字节输出流向文件中一次写一个字节数组
    方法:
        void write​(byte[] b): 向文件中写入一个字节数组。
        void write​(byte[] b, int off, int len): 向文件中写入字节输入的一部分。
 */
public class Demo04OutputStream {
    public static void main(String[] args) throws IOException {
        //创建FileOutputStream对象,并指定目标文件
        FileOutputStream fs=new FileOutputStream("java265.txt");
        //使用字节输出流向文件写一个字节数组
        //创建一个字符串,转字节数组
        String s="abcde";
        byte[] b = s.getBytes();
//把字节数组输出到指定文件
//        fs.write(b);
        //向文件中写入字节输出的一部分
        fs.write(b,0,b.length-1);
//关闭流
        fs.close();
    }
/*
        如果想要在原来文件的后面进行续写,可以使用另外的构造方法创建对象。
        FileOutputStream​(File file, boolean append): 第二个参数append表示是否要续写。
        true表示续写。
        FileOutputStream​(String name, boolean append): 第二个参数append表示是否要续写。
        true表示续写。
        */
public class Demo05OutputStream {
    public static void main(String[] args) throws IOException {
        FileOutputStream fs = new FileOutputStream("fiel02.text", true);
        //字符串转字节数组
        fs.write("我是java265".getBytes());
        //释放资源
        fs.close();
    }
}
/*
    换行写
    如果要实现换行,可以使用换行符。
    每个操作系统的换行符都不一样。
    windows: rn
    linux: n
    mac: r
 */
public class Demo06WriteLine {
    public static void main(String[] args) throws IOException {
        FileOutputStream fs = new FileOutputStream("fiel03", true);
        fs.write("我是一个java爱好者rn".getBytes());
        fs.write("我来自java265rn".getBytes());
        fs.write("rnrnjava265.com".getBytes());
        fs.close();
    }
}

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/java265/p/16294464.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!