io流-文件的读取与写入

字节流

读取文件中的内容

public class Demo01 {
    public static void main(String[] args) {
        try {
            File file = new File("D:\\WorkPlace\\something\\520.txt");//创建一个file对象
           //1.在文件和程序中铺设管道
            FileInputStream f1 = new FileInputStream(file);
           //FileInputStream fis = new FileInputStream("D:\\WorkPlace\\something\\520.txt")
           //打开水龙头
            int a = 0;
            while ((a=f1.read())!=-1){//读到最后返回一个-1,返回的是一个整数类型
                System.out.print((char)a);
            }
            f1.close();//关闭水龙头
        } catch (Exception e) {
            System.out.println("file can not find");
        }
    }
}

向磁盘写入

 public static void main(String[] args) {
        //1.创建水厂
        String data = "\ni have loved";
        try {
            //2.铺设通往硬盘的管道
            //append为true,则追加
            FileOutputStream fout = new FileOutputStream("D:\\WorkPlace\\something\\521.txt",true);
            //3.打开水龙头放水
            byte[] foutTemp=data.getBytes();
            fout.write(foutTemp);//文件不存在则自动创建
            fout.close();//关闭
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

/*
	FileOutputStream && BufferedOutputStream extend OutputStream
	
*/

文件复制

public static void main(String[] args) {
        int temp =0;//用于接收read()返回的值
        try {
            FileInputStream fis = new FileInputStream("D:\\WorkPlace\\something\\520.txt");//连接用于复制的文件
            FileOutputStream fos = new FileOutputStream("D:\\WorkPlace\\something\\522.txt");//连接盘符

            while((temp=fis.read())!=-1){//read()读取到最后会返回-1
                fos.write(temp);//写入内容
            }
            fis.close();//关闭
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

缓冲复制

public class Demo04 {
    public static void main(String[] args) {

        try {
            long starTime = System.currentTimeMillis();
			//建立水厂与管道
            InputStream ips = new FileInputStream("D:\\__easyHelper__\\jdk api 1.8_China.zip");
            BufferedInputStream bis = new BufferedInputStream(ips);
			//建立与盘符的管道
            OutputStream fos = new FileOutputStream("D:\\WorkPlace\\something\\api 1.8_China.zip");
            BufferedOutputStream bos = new BufferedOutputStream(fos);
			//建立小车用于装载数据
            byte[] car = new byte[1024*1024];
            int len=0;
            while ((len=bis.read(car))!=-1){//打开与水厂之间的水龙头
                bos.write(car,0,len);//写入盘符,ch代表具体的数量
            }
            bis.close();//关闭水龙头
            ips.close();
            bos.close();
            long endTime = System.currentTimeMillis();
            System.out.println("花费"+(endTime-starTime)+"ms");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /*
    	public void write(byte[] b,
                  int off,
                  int len)
           throws IOException从指定的字节数组写入len个字节,从偏移off开始到缓冲的输出流。 
通常,该方法将给定数组的字节存储到此流的缓冲区中,根据需要将缓冲区刷新到底层输出流。 然而,如果请求的长度至少与此流的缓冲区一样大,那么这个方法将刷新缓冲区并将字节直接写入底层的输出流。 因此冗余BufferedOutputStream不会不必要地复制数据。
    */

字符流

读取纯文本文件比较方便,但不能读取图片,视频,音频等。

输入

public static void main(String[] args) {
        try {
            //1.建立水厂
            //2.铺设管道
            FileReader fr = new FileReader("D:\\WorkPlace\\something\\520.txt");
            //3.打开水龙头
            int ch = 0;
            char[] car = new char[1024];

            while ((ch=fr.read(car))!=-1){
                String str = new String(car);
                System.out.println(str);
            }
            //4关闭水龙头
            fr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

输出

public static void main(String[] args) {
        //1.水厂
        String str = "   java很简单!!!";
        //2.建立管道
        try {
            FileWriter fw= new FileWriter("D:\\WorkPlace\\something\\520.txt",true);
         //3.打开水龙头
            fw.write(str);
         //4.关闭
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值