JAVA——53.输入输出流

【流】数据流,一串数据,像流水一样,是有方向的。输入输出就是它的方向,是输入数据,还是输出数据。站在计算机的角度,输入数据流,就是读,读数据;计算机在输出数据,就是写
【输入输出流】也称为读写数据流。有方向的流动的一串数据,这串数据有起始点,也有目的地。
【FileOutputSteam】文件输出流。输出,对计算机来说,就是写的意思。File就代表写数据的一个目的地,也就是一个文件
练习一、写一个hello Zora到文件a.txt上

package org.zhaiyujia.pkg1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestFile {
    File file;
    public TestFile() {
        file=new File("f://a.txt");
        boolean b=file.exists();
        if(!b) {                      //之前报错是因为要强制try{}catch{}
            try {
              file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        try {
            FileOutputStream fos=new FileOutputStream(file);
            String info="hello Zora";
            fos.write(info.getBytes());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    public static void main(String[] args) {
        new TestFile();
    }

}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/zhaiyujia15195383763/article/details/81234529