day18:字节流、字符流、属性集

IO的分类

根据数据的流向分为:输入流输出流

  • 输入流 :把数据从其他设备上读取到内存中的流。

  • 输出流 :把数据从内存 中写出到其他设备上的流。

根据数据的类型分为:字节流字符流

  • 字节流 :以字节为单位,读写数据的流。

  • 字符流 :以字符为单位,读写数据的流。

IO流顶层父类

含义
java.io.OutputStream字节输出流顶层父类,抽象类,定义了写出数据方法write()。
java.io.InputStream字节输入流顶层父类,抽象类,定义了读取数据方法read()。
java.io.Writer字符输出流顶层父类,抽象类,定义了写出数据方法write()。
java.io.Reader字符输入流顶层父类,抽象类,定义了读取数据方法read()。

写出字节数据

public static void main(String[] args) throws IOException {
    // 使用文件名称创建流对象
    FileOutputStream fos = new FileOutputStream("fos.txt");     
    // 写出数据
    fos.write(97); // 写出第1个字节
    fos.write(98); // 写出第2个字节
    fos.write(99); // 写出第3个字节
    // 关闭资源
    fos.close();
}

写出字节数组

public static void main(String[] args) throws IOException {
    // 使用文件名称创建流对象
    FileOutputStream fos = new FileOutputStream("fos.txt");     
    // 字符串转换为字节数组
    byte[] b = "abcdef".getBytes();
    // 写出字节数组数据
    fos.write(b);
    // 关闭资源
    fos.close();
}

续写

 public static void main(String[] args) throws IOException {
     // 使用文件名称创建流对象
     FileOutputStream fos = new FileOutputStream("fos.txt",true);     
     // 字符串转换为字节数组
     byte[] b = "abcde".getBytes();
     // 写出从索引2开始,2个字节。索引2是c,两个字节,也就是cd。
     fos.write(b); 
     // 关闭资源
     fos.close();
 }
public static void main(String[] args) throws IOException{
    // 使用文件名称创建流对象.
    FileInputStream fis = new FileInputStream("read.txt"); // 文件中为abcde
    // 定义变量,作为有效个数
    int len ;
    // 定义字节数组,作为装字节数据的容器   
    byte[] b = new byte[2];
    // 循环读取
    while (( len= fis.read(b))!=-1) {
    // 每次读取后,把数组的有效字节部分,变成字符串打印
    	System.out.println(new String(b,0,len));//  len 每次读取的有效字节个数
    }
    // 关闭资源
    fis.close();
}

 异常处理

public static void main(String[] args){
	FileOutputStream fos = null;
    try{
    	fos.write(100);
    }catch(IOException ex){
    	ex.PrintStackTrace();
    }finally{
        if(fos!=null)
            try{
                fos.close();
            }catch(IOException ex){
                ex.PrintStackTrace();
            }
    }
}

文件复制

 public static void main(String[] args) throws IOException {
     // 1.创建流对象
     // 1.1 指定数据源
     FileInputStream fis = new FileInputStream("D:\\test.jpg");
     // 1.2 指定目的地
     FileOutputStream fos = new FileOutputStream("test_copy.jpg");

     // 2.读写数据
     // 2.1 定义数组
     byte[] b = new byte[1024];
     // 2.2 定义长度
     int len;
     // 2.3 循环读取
     while ((len = fis.read(b))!=-1) {
         // 2.4 写出数据
         fos.write(b, 0 , len);
     }

     // 3.关闭资源
     fos.close();
     fis.close();
}

字符流读取

public class FISRead {
    public static void main(String[] args) throws IOException {
      	// 使用文件名称创建流对象
       	FileReader fr = new FileReader("read.txt");
      	// 定义变量,保存有效字符个数
        int len ;
        // 定义字符数组,作为装字符数据的容器
        char[] cbuf = new char[2];
        // 循环读取
        while ((len = fr.read(cbuf))!=-1) {
            System.out.println(new String(cbuf,0,len));
        }
    	// 关闭资源
        fr.close();
    }
}

写出字符数组

public class FWWrite {
    public static void main(String[] args) throws IOException {
        // 使用文件名称创建流对象
        FileWriter fw = new FileWriter("fw.txt");     
      	// 字符串转换为字节数组
      	char[] chars = "多易程序员".toCharArray();
      
      	// 写出字符数组
      	fw.write(chars); // 多易程序员
        
		// 写出从索引2开始,2个字节。索引2是'程',两个字节,也就是'程序'。
        fw.write(b,2,2); // 程序
      
      	// 关闭资源
        fos.close();
    }
}

Properties类

与流相关的方法

public class ProDemo2 {
    public static void main(String[] args) throws FileNotFoundException {
        // 创建属性集对象
        Properties pro = new Properties();
        // 加载文本中信息到属性集
        pro.load(new FileInputStream("read.txt"));
        // 遍历集合并打印
        Set<String> strings = pro.stringPropertyNames();
        for (String key : strings ) {
          	System.out.println(key+" -- "+pro.getProperty(key));
        }
     }
}
输出结果:
filename -- a.txt
length -- 209385038
location -- D:\a.txt

2.IO流都是重点

3.充实

4.复习前两天的知识

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值