java学习27天2020/8/1

一.

输入流代表从外部流入计算机的数据序列,输出流代表计算机流入外部的数据序列

对流的操作扥为字节流和字符流

字节流对二进制文件,图片及影响文件的操作;字符流针对文本文件;

字节流

输出字节流OutputStream,是所有输出字节流的父类,并且是一个抽象类;

常用的几种方法

关闭流:public void close() throws IOException

写一组数据:public void write(byte[] b) throws IOException.
写一个数据: public void write(int b) throws IOException。 但是要想将Outputream实例化,且进行文件操作,就要使用FileOutputStream 子类。
构造: public FileOutputStream(File file) throws FileNotFoundException.

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FileOutStreamDemo {
    public static void main(String[] args)throws IOException {
        out();
    }
    public static void out()throws IOException{
        OutputStream out=new FileOutputStream("D:/Hello.txt");
        String info="Hello Java!";
        byte[] buf=info.getBytes();
        out.write(buf);
        out.close();
    }
}

重新运行会将源文件的内容覆盖

在文件中追加内容   public FileOutputStream(File file,boolean append)throws FileNotFoundException;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FileOutStreamDemo {
    public static void main(String[] args)throws IOException {
        out();
    }
    public static void out(){
        OutputStream out=null;
        try {
           out=new FileOutputStream("D:/Hello.txt",true);
           String info="Hello PHP!";
           byte[] buf=info.getBytes();
           out.write(buf);
        }catch(IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(out!=null) out.close();
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

 二.字节数组不熟悉

三inputstream

原文地址:https://www.cnblogs.com/qiangini/p/13416570.html