JAVA输入输出流

输入流

从硬盘上读取文件到内存
1,准备文件和输入流
2,准备字节数组开始读取
3,边读取边转换
4,关闭流

public static void main(String[] args) {
    File file=new File("D:\\File.txt");
    FileInputStream fis=null;
    try {
         fis=new FileInputStream(file);
         byte []b=new byte[1024];
         int len=fis.read(b);
         while(len!=-1){
             String str=new String(b,0,len);
             System.out.println(str);
             len=fis.read(b);
         }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        if(fis!=null){ 
            try {
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

输出流

从内存上写入文件到硬盘
1,准备文件和输出流
2,边写入边转换
3,关闭流

    File file=new File("D:\\File.txt");
            FileOutputStream fis=null;
            try {
                fis=new FileOutputStream(file);
                String str="中国梦";
                byte b[]=str.getBytes();
                fis.write(b);
                System.out.println(b);
            } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

}

关于文件的复制
硬盘→内存→硬盘
代码参照上面代码修改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值