Java文件操作(完善)

import java.io.FileOutputStream;
 
public class FileOutputStreamDemo {
 
    public static void main(String[] args) throws Exception {
 
        // 覆盖的方式写数据
        FileOutputStream fos = new FileOutputStream(
                "D:/FileOpreat/abc.txt");
 
        String s = "老年你好";
        byte[] bytes = s.getBytes();
        fos.write(bytes);
        // 将字符串按指定编码集编码--》将信息转成二进制数 fos.write(bytes); // 这样写入的数据,会将文件中的原数据覆盖
 
        // 追加的方式写数据:如果要往一个文件中追加数据,则在FileOutputStream的构造参数中多传一个true
        FileOutputStream fos2 = new FileOutputStream(
                "D:/FileOpreat/abc.txt",true);
        fos2.write(",sb".getBytes("UTF-8"));//安全的加密方式
        fos2.close();
 
        /**
         * 第一句和后两句话写到文件中的数据完全相同
         */
        //fos.write("我用一生一世为你祈祷".getBytes()); // .getBytes()编码的过程
        //fos.write((byte) 49);
        //fos.write((byte) 51);
 
        /**
         * 这两句话写到文件中的数据完全相同
         */
        // fos.write((byte)13);
        // fos.write("\r".getBytes());
 
        fos.close();
 
    }
}

猜你喜欢

转载自www.cnblogs.com/DongChenYi/p/12017056.html
今日推荐