IO流输入流Propertis集合 用法及概述

    xiaoxiao2025-06-14  14

    1 字节流 换行 字节输入流 复制图片

    write();flush();close(); 输出流 FileOutputStream FileInputStream 该类是文件的输入流 从文件中读取数据 read();

    字节的输入流

    java.io.InputStream 是所有字节流输入流的超类

    read(); 读取下一个字节 read(byte[] bytes); close(); FileInputStream extends InputStream FileInputStream 文件字节输入流 作用:把硬盘上的数据 读取到内存里 如何用: FileInputStream(String name); String name 是路径 FileInputStream(File name); 字节输入流的使用步骤(重点) 1 创建FileInputStream对象 构造方法中要绑定读取的数据源 2 使用对象 read() 读文件 3 释放资源

    2 字符流 输入 输出 异常处理(1.7 1.9)

    GBK 一个汉字 两个字节

    utf-8 一个汉字是三个字节

    字符输入流 Reader —>java.io Reader

    close(); read(); FileReader extends Reader

    3 Properties 重点

    4 缓冲流—》提高效率

    具体分析方法步骤及举例: package cn.kgc.demo01.InputStream;

    import java.io.FileInputStream; import java.io.IOException;

    /**

    字节的输入流java.io.InputStream 是所有字节流输入流的超类 read(); 读取下一个字节 read(byte[] bytes); close(); FileInputStream extends InputStream FileInputStream 文件字节输入流 作用:把硬盘上的数据 读取到内存里 如何用: FileInputStream(String name); String name 是路径 FileInputStream(File name); 字节输入流的使用步骤(重点) 1 创建FileInputStream对象 构造方法中要绑定读取的数据源 2 使用对象 read() 读文件 3 释放资源

    */

    public class Demo01InputStream { public static void main(String[] args)throws IOException { // 1 创建FileInputStream对象 构造方法中要绑定读取的数据源 FileInputStream fis = new FileInputStream("d:\\bobo.txt"); // 2 s使用对象 read() 读文件 // int len = fis.read(); // int len1 = fis.read(); // int len2= fis.read(); // System.out.println(len); // System.out.println(len1); // System.out.println(len2); /* * 读取是一个重复过程 用循环 fis.read(); 读不到返回-1 * */ int len = 0; while((len=fis.read())!=-1){ System.out.println(len); } // 3 释放资源 fis.close(); } }

    字节输入流 一次读取多个字节

    package cn.kgc.demo01.InputStream; import java.io.FileInputStream; import java.io.IOException; /** * 字节输入流 一次读取多个字节 * read(byte[] bytes); * byte[]参数作用 * 起到缓冲作用 读取是多个字节 数组长度 1024(1kb) */ public class Demo02FileInputSrtream { public static void main(String[] args) throws IOException{ FileInputStream fis = new FileInputStream("d:\\bobo.txt"); byte[] bytes= new byte[1024]; int len = 0; while((len=fis.read(bytes))!=-1){ //字节数组转字符串 new String(byte[] bytes,int off,int length) // bytes={97,98,99} new String(bytes,0,bytes.length)--->abc System.out.println(new String(bytes,0,len)); } } }

    * 文件复制

    步骤: 1 创建输入流对象 读的数据源 2 创建输出流对象 写入的目的 3 使用字节输入流中 read 读取文件 4 使用字节输出流 write 写到目的文件中 5 释放资源 先开的后关

    public class Demo03Copy { public static void main(String[] args) throws IOException{ long s = System.currentTimeMillis(); //1 创建输入流对象 读的数据源 FileInputStream fis = new FileInputStream("d:\\kgc.jpg"); //2 创建输出流对象 写入的目的 FileOutputStream fos = new FileOutputStream("D:\\my_java\\kgc.jpg"); //3 使用字节输入流中 read 读取文件 // int len =0; // while((len=fis.read())!=-1){ // //4 使用字节输出流 write 写到目的文件中 // fos.write(len); // } byte[] bytes=new byte[1024]; int len=0; while((len=fis.read(bytes))!=-1){ fos.write(bytes,0,len); } //5 释放资源 先开的后关 long ss = System.currentTimeMillis(); fos.close(); fis.close(); System.out.println("复制图片用了"+(ss-s)+"毫秒"); } }

    字符输出流,将内存中的数据 写入到文件中

    ``package cn.kgc.Demo02.Reader;

    import java.io.FileWriter; import java.io.IOException;

    java.io.Writer :字符输出流 抽象类write();flush();close();FileWriter extends OutputStreamWriter extends Writer作用 :将内存中的数据 写入到文件中步骤 1 创建FileWriter对象 数据目的地 2 用对象中的方法write() 3 flush()--->将缓冲区当中数据 写到文件中 4 释放资源 flush();和 close()区别 flush() 刷新 流对象可以继续使用 close();刷新缓冲区 然后通过释放资源的方式 流不能再使用 public class Demo02Writer { public static void main(String[] args) throws IOException{ FileWriter fw = new FileWriter("d:\\d.txt"); fw.write(97); fw.write("我爱java"); fw.flush(); fw.close(); } }

    字符流写数据的其它方法

    public class Demo03Write { public static void main(String[] args) throws IOException{ FileWriter fw =new FileWriter("d:\\dd.txt"); char[] cs = {'a','b','c','d','e'}; fw.write(cs); fw.write(cs,1,3); fw.write("课工场"); fw.write("我爱英语但是英语不爱我",2,3);// **最后一个参数 代表输出的个数** fw.close(); } }

    持久的属性集 Properties可以保存在流中 或者从流中加载

    package cn.kgc.demo04.pro; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; import java.util.Set; /** * java.util.Properties 集合 extends Hashtable<k,v>implements Map<k,v> * 持久的属性集 Propertis可以保存在流中 或者从流中加载 * 唯一的一个和IO流相结合的集合 * store 把流中临时的数据 持久化到硬盘中存储 * load把硬盘中的文件(键值对) 读取到 集合中使用 * */ public class Demo01Properties { public static void main(String[] args) throws IOException{ show03(); } // 3 使用Properties集合中的方法 load 把硬盘上的文件(键值对) 读取到集合中使用 private static void show03()throws IOException{ // 1 创建集合 Properties po = new Properties(); // 2 load方法读取数据 并保存到对应的集合中 po.load(new FileReader("d:\\kgc11.txt")); //3 遍历集合po Set<String> s = po.stringPropertyNames(); for(String key:s){ String value = po.getProperty(key); System.out.println(key+"="+value); } } //2 把集合中的临时数据写到硬盘上 store 把流中临时的数据 持久化到硬盘中存储 // load把硬盘中的文件(键值对) 读取到 集合中使用 private static void show02()throws IOException{ Properties po = new Properties(); po.setProperty("PWD","dou6666"); po.setProperty("user","ruirui"); po.setProperty("周慧敏","168"); po.setProperty("古丽娜扎","160"); //1 创建字节输出流 //字符输出流 构造方法中要绑定输出的目的地 // FileWriter fw = new FileWriter("d:\\kgc1.txt"); // po.store(fw,"save data",true);//true 不替换 false 替换 // fw.close(); po.store(new FileWriter("d:\\kgc11.txt",true),"");//true 不替换 false 替换 } //1 使用properties 集合存储数据 遍历取出 /*propertes 集合有一些操作字符串的方法 setProperties(String key,Strign value) * getProperties(String key); * stringPropertyNames();----->keySet方法 * */ private static void show01(){ //1 存值 Properties po = new Properties(); po.setProperty("赵丽颖","168"); po.setProperty("迪丽热巴","165"); po.setProperty("周慧敏","168"); po.setProperty("古丽娜扎","160"); // 2 取值 Set<String> set = po.stringPropertyNames(); for(String key:set){ String value = po.getProperty(key); System.out.println(key+"= "+value); } } }

    字节缓冲输出流 BufferedOutputStream

    字节缓冲输出流 BufferedOutputStream extends OutputStream close(); flush(); write(); 使用步骤 1 创建FileOutputStream 对象 2 创建 BufferedOutputStream对象 3 使用 BufferedOutputStream中的方法 write(); 写到缓冲区 4 使用 BufferedOutputStream中的方法 flush(); 5 释放

    public class Demo01BufferedOutputStream { public static void main(String[] args)throws IOException { // 1 创建FileOutputStream 对象 FileOutputStream fos = new FileOutputStream("d:\\ruirui.txt"); // * 2 创建 BufferedOutputStream对象 BufferedOutputStream bos = new BufferedOutputStream(fos); // * 3 使用 BufferedOutputStream中的方法 write(); 写到缓冲区 bos.write("我把数据写入到内部缓冲区".getBytes()); // * 4 使用 BufferedOutputStream中的方法 flush(); bos.flush(); // * 5 释放 bos.close(); }

    BufferedInputStream缓冲流读取数据

    read(); close(); 构造方法 new BufferedInputStream(InputStream in); //用缓冲流读取数据的步骤 1 创建FileInputStream对象 2 创建BufferedInputStream对象 3 调用read(); 4 释放资源

    public class Demo02BufferedInputStream { public static void main(String[] args) throws IOException{ // 1 创建FileInputStream对象 FileInputStream fis = new FileInputStream("d:\\ruirui.txt"); // 2 创建BufferedInputStream对象 BufferedInputStream bis = new BufferedInputStream(fis); // 3 调用read(); byte[] bytes = new byte[1024]; int len= 0; while((len=bis.read(bytes))!=-1){ System.out.println(new String(bytes,0,len)); } // 4 释放资源 bis.close(); }
    最新回复(0)