Java基础IO流

IO流IOFile输入字节流FileInputStream 和 输出字节流FileOutputStreamBufferedInputStream BufferedOutputStream字符BufferedReader BufferedWriter字符,从控制台输入,一行一行复制到文档总结:

IO流

IO

输入(input)与输出(output)

Java的IO主要包含三个部分:

流式部分――IO的主体部分

非流式部分――主要包含一些辅助流式部分的类

文件读取部分的与安全相关的类以及与本地操作系统相关 的文件系统的类

Java中的流操作分为两种

基于字节流(InputStream读取,OutputStream写入)

字符流(Reader读取,Writer写入)

Java IO流可以概括为:两个对应、一个桥梁(字节流向字符流的转换)。两个 对应指字节流(Byte Stream)和字符流(Char Stream)的对应,输入流和输出流的对应。一个桥 梁指从字节流到字符流的桥梁(字节流向字符流的转换)

使用的时候都是它们的子类

 

File

对文件和文件夹做操作

​
import java.io.File;
import java.io.IOException;
​
/**
 * File创建文件、文件夹
 */
public class FileDemo {
    public static void main(String[] args) {
​
        File file1=new File("love.md");
        File file2=new File("d:\\love.md");
        File file3=new File("d:\\love");
        File file4=new File("d:\\love\\ai");
​
        try {
           boolean f1= file1.createNewFile();
           boolean f2= file2.createNewFile();
           boolean f3= file3.mkdir();
           boolean f4= file4.mkdirs();
​
           if(f1 && f2 && f3 && f4){
               System.out.println("ok");
           }
​
​
        } catch (IOException e) {
            e.printStackTrace();
        }
​
​
    }
​
}
​

输入字节流FileInputStream 和 输出字节流FileOutputStream

FileInputStream提供了测试文件大小的方法 available(),提供了关闭流的方法close(),使用 read()方法从数据源中读取数据。可以使用缓冲区, 通过指定byte[]b的大小来提高效率——如果不设计 缓冲区,那么只能一个字节一个字节的读取,效率太 低

FileOutputStream类中提供了三种写入数据的write() 方

​
import java.io.*;
​
/**
 * 字节码
 * 不带缓冲区复制图片
 */
public class FileStreamDemo {
    public static void main(String[] args) {
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            fis=new FileInputStream("d:\\知鬼而上.png");
            fos=new FileOutputStream("d:\\zgrs.png");
​
            byte []b= new byte[1024];
            int lenght=0;
            while ((lenght=fis.read(b))!=-1){
               fos.write(b);
​
            }
            fos.flush();
​
​
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
​
​
    }
​
}

BufferedInputStream BufferedOutputStream

​
import java.io.*;
​
/**
 * 字节码
 * 带缓冲区的复制图片
 */
public class BufferedReaderDemo {
    public static void main(String[] args) {
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;
​
        try {
            bis=new BufferedInputStream(new FileInputStream(new File("d:\\知鬼而上.png")));
            bos=new BufferedOutputStream(new FileOutputStream(new File("d:\\zgrs.png")));
            byte b[]=new byte[1024];
            int lenght=0;
            while ((lenght=bis.read(b))!=-1){
                bos.write(b);
​
            }
            bos.flush();
​
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bos.close();
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
​
​
    }
}
​

字符BufferedReader BufferedWriter

package com.exercises.lesson17;
import java.io.*;
​
/**
 * 字符
 * 一行一行复制文档内的内容
 *
 */
public class BufferedDemo {
    public static void main(String[] args) {
        BufferedReader br=null;
        BufferedWriter bw=null;
        try {
            br=new BufferedReader(new FileReader("d:\\love.txt"));
            bw=new BufferedWriter(new FileWriter("d:\\no.txt"));
​
            String temp=null;
            while ((temp=br.readLine())!=null){
                bw.write(temp);
                bw.newLine();
            }
            bw.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bw.close();
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
​
        }
​
​
    }
}
​

字符,从控制台输入,一行一行复制到文档

package com.exercises.lesson17;
​
import java.io.*;
​
/**
 * 字符
 * 从控制台输入,一行一行复制到文档
 *
 */
public class Zjzzw {
    public static void main(String[] args) {
        BufferedReader br=null;
        BufferedWriter bw=null;
​
        br=new BufferedReader(new InputStreamReader(System.in));
        try {
            bw=new BufferedWriter(new FileWriter("d:\\niubi.txt"));
            String temp=null;
            while ((temp=br.readLine())!=null){
                    if (temp.equals("end")){
                        break;
                    }
                bw.write(temp);
                bw.newLine();
            }
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bw.close();
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

总结:

File类的用来创建文件、文件夹,并实现对它们属性 的读取及修改

字节流InputStream、OutputStream可以用来读取 二进制文件

字符流Reader、Writer能够高效的读取文本文件

Java提供了字节流向字符流的转换: InputStreamReader、OutputStreamWriter

PrintWriter能够对通往输出流的数据进行格式化

<-个人笔记->

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值