简单的输入输出流(2)

从一个文本文件中读取数据

public class FileInputStreamDemo1 {
    /**
     * 使用InputStream抽象类的子类FileInputStream类
     * 从一个文件中读取流数据
     */
    public static void test1() {
        try {
            FileInputStream myStream = new FileInputStream("src/a.txt");
            int a = 0;
            while((a=myStream.read())!=-1){    //从a.txt读取内容一直到文件的末尾
                System.out.print((char)a);
            }
            myStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

}

简单的输出流
输入一个字符到文本中

public class Demo1 {
    /**
     * 输入一个字符到文件中
     */
    public static void test1() {
        try {
            OutputStream os = new FileOutputStream("src/b.txt");
            os.write(97);
            os.flush();
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
运行结果为a,因为ascii码97对应的字符为‘a’;

输出一个数组
/**
     * 输出一个数组
     */
    public static void test2() {
        byte myArray[] = new byte[5];
        myArray[0]=97;
        myArray[1]=98;
        myArray[2]=99;
        myArray[3]=100;
        myArray[4]=101;
        try {
            OutputStream os = new FileOutputStream("src/b.txt");
            os.write(myArray);
            os.flush();
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

输出结果为abcde
输出字符串

public static void test3() {
        String myContent = "I am a good man!";
        try {
            OutputStream os = new FileOutputStream("src/b.txt");
            os.write(myContent.getBytes());
            os.flush();
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

运行结果为:I am a good man! 。

将一个文件的内容输出到另一个文件中

public static void copy(){

            try
            {
                FileInputStream myStream = new FileInputStream("src/1.png");
                OutputStream os = new FileOutputStream("2.jpg");
                int a = 0;
                while((a = myStream.read())!=-1){  //一直读到文件的末尾
                    os.write((char)a);

                }
                os.flush();
                os.close();
                } catch (FileNotFoundException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值