字节流与字符流

流的概念:

在程序中全部的数据都是以流的形式进行传输和保存的,程序须要数据的时候要使用输入流读取数据,而当程序须要将数据保存起来的时候,就要使用输出流完成。

字节流与字符流

在Java.io中操做文件内容的主要有两大类:字符流和字节流,两类都分为输入和输出操做。
字节流:
输出-------OutputStream
输入-------InputStream
字符流:
输出-------Writer
输入-------Reader

操做流程-----以文件操做为例

1.使用File类打开一个文件
2.经过字节流或字符流的子类,指定输出的位置(由于其父类为抽象类)
3.进行读/写操做
4.关闭输入/输出
注意:
io操做属于资源操做,在操做结束后必须关闭。
 

字节流的使用概述

字节流主要操做的是byte类型数据,以byte数组为准,主要操做类就是:
字节流:
输出流-------OutputStream
输入流-------InputStream

写文件:

import java.io.File ;
import java.io.OutputStream ;
import java.io.FileOutputStream ;
public class OutputStreamDemo01{
   public static void main(String args[]) throws Exception{   // 异常抛出,不处理
     // 第1步、使用File类找到一个文件
    File f= new File( "d:" + File.separator + "test.txt") ;   // 声明File对象
     // 第2步、经过子类实例化父类对象
    OutputStream out = null ;   // 准备好一个输出的对象
    out = new FileOutputStream(f)    ;   // 经过对象多态性,进行实例化
     // 第3步、进行写操做
    String str = "Hello World!!!" ;     // 准备一个字符串
     byte b[] = str.getBytes() ;      // 只能输出byte数组,因此将字符串变为byte数组
    out.write(b) ;             // 将内容输出,保存文件
     // 第4步、关闭输出流
    out.close() ;             // 关闭输出流
  }
};
import java.io.File ;
import java.io.OutputStream ;
import java.io.FileOutputStream ;
public class OutputStreamDemo02{
   public static void main(String args[]) throws Exception{   // 异常抛出,不处理
     // 第1步、使用File类找到一个文件
    File f= new File( "d:" + File.separator + "test.txt") ;   // 声明File对象
     // 第2步、经过子类实例化父类对象
    OutputStream out = null ;   // 准备好一个输出的对象
    out = new FileOutputStream(f)    ;   // 经过对象多态性,进行实例化
     // 第3步、进行写操做
    String str = "Hello World!!!" ;     // 准备一个字符串
     byte b[] = str.getBytes() ;       // 只能输出byte数组,因此将字符串变为byte数组
     for( int i=0;i<b.length;i++){     // 采用循环方式写入
       out.write(b[i]) ;  // 每次只写入一个内容
    }
     // 第4步、关闭输出流
    out.close() ;             // 关闭输出流
  }
};
import java.io.File ;
import java.io.OutputStream ;
import java.io.FileOutputStream ;
public class OutputStreamDemo03{
   public static void main(String args[]) throws Exception{   // 异常抛出,不处理
     // 第1步、使用File类找到一个文件
    File f= new File( "d:" + File.separator + "test.txt") ;   // 声明File对象
     // 第2步、经过子类实例化父类对象
    OutputStream out = null ;   // 准备好一个输出的对象
     out = new FileOutputStream(f,true)    ;  // 此处表示在文件末尾追加内容
     // 第3步、进行写操做
    String str = "Hello World!!!" ;     // 准备一个字符串
     byte b[] = str.getBytes() ;       // 只能输出byte数组,因此将字符串变为byte数组
     for( int i=0;i<b.length;i++){     // 采用循环方式写入
      out.write(b[i]) ;   // 每次只写入一个内容
    }
     // 第4步、关闭输出流
    out.close() ;             // 关闭输出流
  }
};
import java.io.File ;
import java.io.OutputStream ;
import java.io.FileOutputStream ;
public class OutputStreamDemo04{
   public static void main(String args[]) throws Exception{   // 异常抛出,不处理
     // 第1步、使用File类找到一个文件
    File f= new File( "d:" + File.separator + "test.txt") ;   // 声明File对象
     // 第2步、经过子类实例化父类对象
    OutputStream out = null ;   // 准备好一个输出的对象
    out = new FileOutputStream(f, true)    ;   // 此处表示在文件末尾追加内容
     // 第3步、进行写操做
     String str = "\r\nHello World!!!" ;    // 准备一个字符串 \r\n--表示换行
     byte b[] = str.getBytes() ;       // 只能输出byte数组,因此将字符串变为byte数组
     for( int i=0;i<b.length;i++){     // 采用循环方式写入
      out.write(b[i]) ;   // 每次只写入一个内容
    }
     // 第4步、关闭输出流
    out.close() ;             // 关闭输出流
  }
};
 

读文件:
import java.io.File ;
import java.io.InputStream ;
import java.io.FileInputStream ;
public class InputStreamDemo03{
   public static void main(String args[]) throws Exception{   // 异常抛出,不处理
     // 第1步、使用File类找到一个文件
    File f= new File( "d:" + File.separator + "test.txt") ;   // 声明File对象
     // 第2步、经过子类实例化父类对象
    InputStream input = null ;   // 准备好一个输入的对象
    input = new FileInputStream(f)    ;   // 经过对象多态性,进行实例化
     // 第3步、进行读操做
     byte b[] = new byte[(int)f.length()] ;    // 数组大小由文件决定
     int len = input.read(b) ;     // 读取内容
     // 第4步、关闭输出流
    input.close() ;             // 关闭输出流\
    System.out.println( "读入数据的长度:" + len) ;
    System.out.println( "内容为:" + new String(b)) ;   // 把byte数组变为字符串输出
  }
};

以上使用的是byte读取数据,以下使用read()方法,看代码
import java.io.File ;
import java.io.InputStream ;
import java.io.FileInputStream ;
public class InputStreamDemo04{
   public static void main(String args[]) throws Exception{   // 异常抛出,不处理
     // 第1步、使用File类找到一个文件
    File f= new File( "d:" + File.separator + "test.txt") ;   // 声明File对象
     // 第2步、经过子类实例化父类对象
    InputStream input = null ;   // 准备好一个输入的对象
    input = new FileInputStream(f)    ;   // 经过对象多态性,进行实例化
     // 第3步、进行读操做
     byte b[] = new byte[( int)f.length()] ;     // 数组大小由文件决定
     for( int i=0;i<b.length;i++){
       b[i] = (byte)input.read() ;    // 读取内容
    }
     // 第4步、关闭输出流
    input.close() ;             // 关闭输出流\
    System.out.println( "内容为:" + new String(b)) ;   // 把byte数组变为字符串输出
  }
};
在不是数据长度的状况下,要使用一下方法读取数据
import java.io.File ;
import java.io.InputStream ;
import java.io.FileInputStream ;
public class InputStreamDemo05{
   public static void main(String args[]) throws Exception{   // 异常抛出,不处理
     // 第1步、使用File类找到一个文件
    File f= new File( "d:" + File.separator + "test.txt") ;   // 声明File对象
     // 第2步、经过子类实例化父类对象
    InputStream input = null ;   // 准备好一个输入的对象
    input = new FileInputStream(f)    ;   // 经过对象多态性,进行实例化
     // 第3步、进行读操做
     byte b[] = new byte[1024] ;     // 数组大小由文件决定
     int len = 0 ;    
     int temp = 0 ;       // 接收每个读取进来的数据
     while((temp=input.read())!=-1){
      // 表示还有内容,文件没有读完
      b[len] = (byte)temp ;
      len++ ;
    }
     // 第4步、关闭输出流
    input.close() ;             // 关闭输出流\
    System.out.println( "内容为:" + new String(b,0,len)) ;   // 把byte数组变为字符串输出
  }
};

字符流的使用概述

在程序中一个字符等于两个字节,Java提供了Writer和Reader两个专门操做字符流的类。
字符输出流:Writer
字符输入流:Reader

字符输出流

 
import java.io.File ;
import java.io.Writer ;
import java.io.FileWriter ;
public class WriterDemo01{
   public static void main(String args[]) throws Exception{   // 异常抛出,不处理
     // 第1步、使用File类找到一个文件
    File f= new File( "d:" + File.separator + "test.txt") ;   // 声明File对象
     // 第2步、经过子类实例化父类对象
     Writer out = null ;  // 准备好一个输出的对象
    out = new FileWriter(f)    ;  // 经过对象多态性,进行实例化
    // 第3步、进行写操做
    String str = "Hello World!!!" ;    // 准备一个字符串
    out.write(str) ;            // 将内容输出,保存文件
     // 第4步、关闭输出流
    out.close() ;             // 关闭输出流
  }
};
 

字符输入流

 
 
import java.io.File ;
import java.io.Reader ;
import java.io.FileReader ;
public class ReaderDemo01{
   public static void main(String args[]) throws Exception{   // 异常抛出,不处理
     // 第1步、使用File类找到一个文件
    File f= new File( "d:" + File.separator + "test.txt") ;   // 声明File对象
     // 第2步、经过子类实例化父类对象
    Reader input = null ;   // 准备好一个输入的对象
    input = new FileReader(f)    ;   // 经过对象多态性,进行实例化
     // 第3步、进行读操做
     char c[] = new char[1024] ;     // 全部的内容都读到此数组之中
     int len = input.read(c) ;     // 读取内容
     // 第4步、关闭输出流
    input.close() ;             // 关闭输出流
    System.out.println( "内容为:" + new String(c,0,len)) ;   // 把字符数组变为字符串输出
  }
};
import java.io.File ;
import java.io.Reader ;
import java.io.FileReader ;
public class ReaderDemo02{
   public static void main(String args[]) throws Exception{   // 异常抛出,不处理
     // 第1步、使用File类找到一个文件
    File f= new File( "d:" + File.separator + "test.txt") ;   // 声明File对象
     // 第2步、经过子类实例化父类对象
    Reader input = null ;   // 准备好一个输入的对象
    input = new FileReader(f)    ;   // 经过对象多态性,进行实例化
     // 第3步、进行读操做
     char c[] = new char[1024] ;     // 全部的内容都读到此数组之中
     int temp = 0 ;   // 接收每个内容
     int len = 0 ;     // 读取内容
     while((temp=input.read())!=-1){
       // 若是不是-1就表示还有内容,能够继续读取
      c[len] = ( char)temp ;
      len++ ;
    }
     // 第4步、关闭输出流
    input.close() ;             // 关闭输出流
    System.out.println( "内容为:" + new String(c,0,len)) ;   // 把字符数组变为字符串输出
  }
};
 

字符流与字节流的区别

字节流在操做的时候,自己是不会用到缓存区的,是与文件自己直接操做( 验证方法就是将close()省略试试看效果如何),而字符流在操做的时候会用到缓存区,即会使用内存。

在开发中使用字符流和字节流哪一个好?

在全部的硬盘上保存文件或是进行数据传输的时候,都是以字节的方式进行的,包括图片也是用字节完成,而字符是只有在内存中才会产生的,全部使用字节的时候是最多的。
相关文章
相关标签/搜索