JAVA,字节流文件读写

写入文件:

package com.java.day28OutputStream;

import java.io.FileOutputStream;
import java.io.IOException;

public class OutputStream01 {
    public static void main(String[] args){
        output();
    }

    public static void output(){
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("D:\\IdeaProjects\\StudyJava\\day01\\src\\com\\java\\day28OutputStream\\a.txt");
            String str = "Hello World";
            // todo getBytes方法将字符串转为byte数组
            fos.write(str.getBytes());
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if (fos !=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

运行效果:

JAVA,字节流文件读写

 

 读取文件:

package com.java.day29InputStream;

import java.io.FileInputStream;
import java.io.IOException;

public class InputStream {
    public static void main(String[] args) {
        input();
    }

    public static void input() {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("D:\\IdeaProjects\\StudyJava\\day01\\src\\com\\java\\day28OutputStream\\a.txt");
            int len = 0;
            while ((len = fis.read()) !=-1){
                System.out.print((char) len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

运行效果:

JAVA,字节流文件读写

 

 

上一篇:java网络 TCP使用案例


下一篇:JAVA篇:Java IO (五) 管道流