java-API之数据的读取与写入的总结

数据的读取与写入

数据写入分为字节流和字符流,字符流常用于纯文本的操作,字节流除了纯文本,还可以用于图片、视频等,使用范围更宽泛。

字节流

  • 读取使用的类:InputStream --> FileInputStream --> BufferedInputStream
InputStream in1 = new BufferedInputStream( new FileInputSteam( "F://ioTest//1.txt" ) );
InputStream in2 = new BufferedInputStream( new FileInputSteam( new File( "F://ioTest//1.txt") ) );
  • 写出使用的类:OutputStream --> FileOutputStream --> BufferedOutputStream
OutputStream out1  = new BufferedOutputStream( new FileOutputStream( "F://ioTest//1.txt" ) );
OutputStream out2 = new BufferedOutputStream( new FileOutputStream( new File( "F://ioTest//1.txt") ) );

案例

System.out.println("请输入源文件的路径");
String source = new Scanner(System.in).nextLine();
File from  = new File(source);
		
System.out.println("请输入目标文件的路径");
String target = new Scanner(System.in).nextLine();
File to = new File(target);

try {
    // 1、创建字符流读对象和写对象
    InputStream in = new BufferedInputStream(new FileInputSteam(from));
    OutputStream out = new BufferedInputStream(new FileInputSteam(to));
    			
    // 2 边读边写 一个字节一个字节地读
   int b = 0;  //定义变量,记录读到的数据
   //while( (b = in.read()) != -1 ) { // 数据读取完毕返回 -1
        //out.write(b);
   // }
    // 读取字节数组
   byte[] bs = new byte[8*1024];
    // 字节流是字节数组byte[] bs = new byte[8*1024]
    // 字符流是字符数组 char[] c = new char[8*1024]
   while( (b = in.read(bs)) != -1 ) { // 数据读取完毕返回 -1
        out.write(bs);
   }
				
    // 3、释放资源
    in.close();
    out.close();
    System.out.println("文件复制完成!");
			
    } catch (IOException e) {
        System.out.println("文件复制失败!!!");  
        e.printStackTrace();  //调试阶段出现,上线时需要删除
    }

字符流

  • 读取使用的类: Reader --> FileReader --> BufferedReader
Reader r = new BufferedReader( new FileReader( "F://ioTest//1.txt" ) );
Reader r2 = new BufferedReader( new FileReader( new File( "F://ioTest//1.txt") ) );
  • 写出使用的类:Writer --> FileWriter --> BufferedWriter
Writer r = new BufferedWriter( new FileWriter( "F://ioTest//1.txt" ) );
Writer r2 = new BufferedWriter( new FileWriter( new File( "F://ioTest//1.txt") ) );

案例:

System.out.println("请输入源文件的路径");
String source = new Scanner(System.in).nextLine();
File from  = new File(source);
		
System.out.println("请输入目标文件的路径");
String target = new Scanner(System.in).nextLine();
File to = new File(target);

try {
    // 1、创建字符流读对象和写对象
    Reader in = new BufferedReader(new FileReader(from));
    Writer out = new BufferedWriter(new FileWriter(to));
    			
    // 2 边读边写 read()一个字节一个字节地读
   // int b = 0;  //定义变量,记录读到的数据
   // while( (b = in.read()) != -1 ) { // 数据读取完毕返回 -1
        // out.write(b);
   // }
    
    // 批量读取 一个数组地读取  
    // 字节流是字节数组byte[] bs = new byte[8*1024]
    // 字符流是字符数组 char[] c = new char[8*1024]
    char[] bs = new char[8*1024]; // 参考缓存区数组的大小
    while( ( ( b = in.read(bs) ) != -1 ) {
        out.write(bs);
    }
    
				
    // 3、释放资源
    in.close();
    out.close();
    System.out.println("文件复制完成!");
			
    } catch (IOException e) {
        System.out.println("文件复制失败!!!");  
        e.printStackTrace();  //调试阶段出现,上线时需要删除
    }

字节流与字符间的转换

字符流转字节流工具类——OutputStreamWriter

OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的 charset 将要写入流中的字符编码成字节。它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集。
创建对象:

  • OutputStreamWriter(OutputStream out) 创建使用默认字符编码的 OutputStreamWriter。
  • OutputStreamWriter(OutputStream out, String charsetName) 创建使用指定字符集的 OutputStreamWriter。
Writer w1 = new OutputStreamWriter( new FileOutputStream( "F:\\ioTest\\1.txt" ) );
Writer w2 = new OutputStreamWriter( new FileOutputStream( new File("F:\\ioTest\\1.txt") ) );

Writer w3 = new OutputStreamWriter( new FileOutputStream( "F:\\ioTest\\1.txt" ), "utf-8" );
Writer w4 = new OutputStreamWriter( new FileOutputStream( new File("F:\\ioTest\\1.txt"), "utf-8" ) );

字符流转字节流的流程图

字节流转字符流工具类——InputStreamReader

InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。
创建对象:

  • InputStreamReader(InputStream in) 创建一个使用默认字符集的 InputStreamReader。
  • InputStreamReader(InputStream in, String charsetName) 创建使用指定字符集的 InputStreamReader。
Reader r1 = new InputStreamReader( new FileInputStream( "F:\\ioTest\\1.txt" ) );
Reader r2 = new InputStreamReader( new FileInputStream( new File("F:\\ioTest\\1.txt") ) );

Reader r3 = new InputStreamReader( new FileInputStream( "F:\\ioTest\\1.txt" ), "utf-8" );
Reader r4 = new InputStreamReader( new FileInputStream( new File("F:\\ioTest\\1.txt") ), "utf-8" );

字符流转字节流的流程图

字符流转字节流的案例

 public static void main(String[] args) {
       outputStreamReaderTest();//输出的转换流
       inputStreamReaderTest();//读取的转换流
    }

    private static void inputStreamReaderTest() {
       try {
           //1,创建转换对象
           BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream("1.txt"),"gbk"));  // 第二个参数可以设置编码表,解决乱码现象
           //2,读取一行数据
           String line = in.readLine();//子类BufferedReader的特有方法
           System.out.println(line);
           //3,释放资源
       }catch(Exception e) {
           e.printStackTrace();
       }
    }

    private static void outputStreamReaderTest() {
       try {
            //1,创建转换对象
           OutputStreamWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("1.txt"), "gbk"));

           // 2,开始写出数据  
           out.write("Java,你好,我来了!");  // 当数据的保存方式和打开方式,就会出现乱码!!

           // 3,释放资源
           out.flush();
       } catch (Exception e) {
           e.printStackTrace();
       }
    }

posted @ 2020-03-06 22:14  技术狂-CYL  阅读(629)  评论(0编辑  收藏  举报