zoukankan      html  css  js  c++  java
  • 5.9字节流与字符流

    字符流与字节流

    OutputStream

    修饰符和类型

    方法

    描述

    void

    close​()

    关闭此输出流并释放与此流关联的所有系统资源。

    void

    flush​()

    刷新此输出流,并强制写出所有缓冲的输出字节。

    void

    write​(byte[] b)

    将b.length指定字节数组中的字节写入此输出流。

    void

    write​(byte[] b, int off, int len)

    将len指定字节数组中从偏移量开始的字节写入off此输出流。

    abstract void

    write​(int b)

    将指定的字节写入此输出流。

    因为OutputStream类是抽象类,所以要通过向上转型来获取实例化对象,

    FileOutputStream子类

    FileOutputStream

    建设者

    描述

    [覆盖】FileOutputStream​(File file)

    创建文件输出流以写入由指定File对象表示的文件。

    FileOutputStream​(FileDescriptor fdObj)

    创建文件输出流以写入指定的文件描述符,该文件描述符表示到文件系统中实际文件的现有连接。

    【追加】FileOutputStream​(File file, boolean append)

    创建文件输出流以写入由指定File对象表示的文件。

    FileOutputStream​(String name)

    创建文件输出流以写入具有指定名称的文件。

    FileOutputStream​(String name, boolean append)

    创建文件输出流以写入具有指定名称的文件。

     
     
     
    package 字节流与字符流;
    
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    
    public class LIu {
    
        public static void main(String[] args) throws Exception {
            // TODO 自动生成的方法存根
            File file=new File("/Users/jacky/Desktop/Java深度学习/hello12.txt");//向上转型
            if(!file.getParentFile().exists()) {//判断文件不存在
                file.getParentFile().mkdirs();//创建父目录
            }
            //OutputStream output=new FileOutputStream(file);
    //        String str="要输出的内容";
    //        output.write(str.getBytes());//将字符串变成字节数组
    //        output.close();
            
            
            //若想要实行一个自动关闭用try(){}catch(){}
            
            try(OutputStream output=new FileOutputStream(file,true)){
                String str="要输出的内容
    	";
                output.write(str.getBytes());//将字符串变成字节数组
                output.close();
            }catch(IOException e) {
                e.printStackTrace();
            }
            
            
        }
    
    }
    OutputStream
  • 相关阅读:
    leetcode 18 4Sum
    leetcode 71 Simplify Path
    leetcode 10 Regular Expression Matching
    leetcode 30 Substring with Concatenation of All Words
    leetcode 355 Design Twitte
    leetcode LRU Cache
    leetcode 3Sum
    leetcode Letter Combinations of a Phone Number
    leetcode Remove Nth Node From End of List
    leetcode Valid Parentheses
  • 原文地址:https://www.cnblogs.com/yu0824/p/12859001.html
Copyright © 2011-2022 走看看