zoukankan      html  css  js  c++  java
  • Java IO 字节流与字符流 (五)

     Java的IO流分为字符流(Reader,Writer)和字节流(InputStream,OutputStream),字节流顾名思义字节流就是将文件的内容读取到字节数组,然后再输出到另一个文件中。而字符流操作的最小单位则是字符。可以先看一下IO流的概述:

    下面首先是通过字符流对文件进行读取和写入:

    复制代码
     1 package lib;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.File;
     5 import java.io.FileNotFoundException;
     6 import java.io.FileReader;
     7 import java.io.FileWriter;
     8 import java.io.IOException;
     9 import java.io.PrintWriter;
    10 
    11 public class Test {
    12     // 定义文件路径
    13     File f = new File("F:\test.txt");
    14     //字符流写入的方法
    15     public String writeInFile() throws IOException{
    16         String str = "";
    17         String count = "";
    18         try {
    19             // 使用字符流对文件进行读取
    20             BufferedReader bf = new BufferedReader(new FileReader(f));
    21             while (true) {
    22                 //读取每一行数据并将其赋值给str
    23                 if ((count = bf.readLine()) != null) {
    24                     str += count;
    25                 } else {
    26                     break;
    27                 }
    28             }
    29             // 关闭流
    30             bf.close();
    31         } catch (FileNotFoundException e) {
    32             e.printStackTrace();
    33         }
    34         return str;
    35     }
    36     //字符流读取的方法
    37     public  void getReader(){
    38         try {
    39             //其中true表示在原本文件内容的尾部添加,若不写则表示清空文件后再添加内容
    40             PrintWriter pw=new PrintWriter(new FileWriter(f,true));
    41             pw.write("测试输入字符串到文件中2");
    42             pw.close();
    43         } catch (IOException e) {
    44             e.printStackTrace();
    45         }
    46     }
    47     public static void main(String[] args) throws IOException {
    48         Test test=new Test();
    49         //将字符串输入到文件中
    50         test.getReader();
    51         //读取相对应的字符串
    52         String str=test.writeInFile();
    53         //将文件中内容在控制台输出
    54         System.out.println("文件内容为:"+str);
    55     }
    56 }
    复制代码

    上述代码的关键地方都有注释,就不再一一赘述了,主要就是在使用完流之后不要忘记关闭就好(行30,行42)

    然后是通过字节流的方式对文件进行操作,将一个文件中的内容复制到另一个文件中:

    复制代码
     1 package com.file.test2;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.FileOutputStream;
     7 import java.io.IOException;
     8 
     9 public class TestFile2 {
    10     //使用字节流读取并写入文件,将一个文件复制到另一个文件中
    11     public static void main(String[] args) throws IOException {
    12         //要复制的源文件
    13         File f=new File("D:\test.txt");
    14         //目标文件
    15         File f2=new File("D:\test2.txt");
    16         //定义一个byte类型的数组,用于存储读取到的内容
    17         byte [] b=new byte[1024];
    18         int length;
    19         try {
    20             //定义读取的流
    21             FileInputStream in=new FileInputStream(f);
    22             //定义输出到文件的流
    23             FileOutputStream out=new FileOutputStream(f2);
    24             //将文件内容输出到另一个文件中
    25             while((length=in.read(b))!=-1){
    26                 out.write(b, 0, length);
    27             }
    28             out.close();
    29             in.close();
    30         } catch (FileNotFoundException e) {
    31             e.printStackTrace();
    32         }
    33     }
    34 }
    复制代码

    在字节流的操作中,第13行的源文件必须存在,可以根据需要自行更改文件路径,只需要存在即可,否则会报文件找不到的错误,同时若想在控制台输出读取到的字节流的内容则可以在第27和28行之间加两句代码:in.read(b, 0, b.length);System.out.println(new String(b));

    以上就是字符流和字节流的相关操作,其实代码不难,主要是自己的理解,相同的问题每个人都会有不同的理解方式,当然,对于我们编程人员来说,除了要多思考之外还要多动手。最后希望以上内容能对大家有所帮助

  • 相关阅读:
    c语言命名规则 [转载]
    [转贴]C编译过程概述
    [转贴]漫谈C语言及如何学习C语言
    Semaphore源码分析
    如何快速转行大数据
    web前端到底怎么学?
    Code Review怎样做好
    SDK与API的理解
    分析消费者大数据
    程序员的搞笑段子
  • 原文地址:https://www.cnblogs.com/jiangzhaowei/p/7391278.html
Copyright © 2011-2022 走看看