File IO流

package test;

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

public class TestIO {
    public static void main(String[] args){
        //创建fileInputStream 对象
        FileInputStream fileinput=null;
        try {
            //实例化FileInputStream对象
            fileinput = new FileInputStream(new File("D://个人文档//一网通办测试.txt"));
            //设置每次从流中读取的字节数量
            byte[] bt=new byte[1024];
            int n = 0;
            String str="";
            //对读取文件的流做循环判断,如果还有数据则继续,否则退出循环
            while((n=fileinput.read(bt))!=-1){
                //从流中获取开始到结束的内容,将字符编码设置成UTF-8防止数据乱码
                str+=new String(bt,0,n,"utf-8");
                System.out.println(str);
            }
            //创建FileOutputStream类的对象,并初始化文件位置
            FileOutputStream out=new FileOutputStream("D://个人文档//测试.txt");
            String str1=str;
            //将字符串变为byte类型数组。
            byte[] bts=str1.getBytes();
            //使用FileOutputStream 类对象调用write方法将byte数组中的数据写到实例化的文件中。
            out.write(bts);
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                fileinput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值