IO文件输入输出流

package IO2;

import java.io.*;
//文件字节输入
public class Test03 {
    public static void main(String[] args) {
        //创建源
        File src=new File("D:\\src\\IO2\\abc.txt");
        //选择流
        InputStream is=null;
        try {
            is=new FileInputStream(src);
            byte[]flush=new byte[3];//每次读三个字符
            int len=-1;//接收长度
            while((len=is.read(flush))!=-1){
                //字节数组-->字符串
                String str=new String(flush,0,len);//取值是从第0个开始 长度为数值的长度 取的是flush数组
                System.out.println(str);

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
package IO2;

import java.io.*;
import java.nio.charset.StandardCharsets;

//文件字节输出流
//创建源
//选择流
//操作 写内容
//释放资源
public class Test04 {
    public static void main(String[] args) {
        //创建源
        File src=new File("D:\\src\\IO2\\dest.txt");
        //选择流
        OutputStream os=null;
        try{
            os=new FileOutputStream(src,false);//true追加 false重新写
            //操作 写出
            String msg="IO";
            byte[]datas=msg.getBytes();
            os.write(datas,0,datas.length);
            os.flush();

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }finally {
            //释放资源
            try{
                if(null!=os){
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }



}
package IO2;

import java.io.*;

//文件拷贝 文件字节输入输出流
public class Copy {
    public static void main(String[] args) {
        //创建源
        File src=new File("D:\\src\\IO\\VCG211294347471.png");
        File dest=new File("D:\\src\\IO2\\copy.png");
        //选择流
        InputStream is=null;
        OutputStream os=null;
        try {
            is=new FileInputStream(src);
            os=new FileOutputStream(dest);
            //操作 分段读取
            byte[] flush=new byte[1024];//缓冲容器
            int len=-1;
            while((len=is.read(flush))!=-1){
                os.write(flush,0,len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //释放资源 先打开的后关闭
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }


}
package IO2;

import java.io.*;
//文件字符输入流
public class Test05 {
    public static void main(String[] args)  {
        //创建源
        File src=new File("D:\\src\\IO2\\abc.txt");
        //选择流
        Reader reader=null;
        try {
            reader=new FileReader(src);
            //操作
            char[] flush=new char[1024];//缓冲容器 每次读1024个字节
            int len=-1;//接收长度
            while((len=reader.read(flush))!=-1){
                //字符数组-->字符串
                String str=new String(flush,0,len);
                System.out.println(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //释放资源
                if(null!=reader){
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
}
package IO2;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
//字符文件输出流
public class Test06 {
    public static void main(String[] args) {
        //创建源
        File dest=new File("D:\\src\\IO2\\dest2.txt");
        //选择流
        Writer writer=null;
        try {
            writer=new FileWriter(dest);
            //写法一
//            String msg="IO";
//            char[] datas=msg.toCharArray();//字符串-->字节数组
//            writer.write(datas,0,datas.length);
            //写法二
//            String msg="IOStudy";
//            writer.write(msg);
//            writer.flush();
            //写法三
            writer.append("iostudy").append("\niostudy");
            writer.flush();



        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值