Java中的输入输出流

流的概念:

1.对字节进行操作的 InputStream.OutputStream 类

  in是进行读数据,out是往出输出数据,这个号容易进行搞混的.

  InputStream和OutputStream最常用的子类

  FileInputStream 和 FileOutputStream对字节数据进行操作

  DataInputStream可以进行二进制操作

2.对字符串操作的 Reader.Writer类

  Reader.Writer的子类BufferedReader.BufferedWrite可以对字符串操作

-----------------------接下来对两个概念详细解说----------------------------

  • InputStream的体系结构
  • InputStream方法
  • OutputStream的体系结构
  • OutputStream方法

InputStream体系结构

InputStream方法

InputStream方法
int read() 从输入流中读取单个字节,返回读取字节数据
int read(byte[] b)  从输入流中读取最多b.leng长度的字节,储存在字节数组b中,返回时间读取的字节数.
int read(byte[] b, int off, int len)  从输入流中读取做多len长度的字节,保存到字节数组b中,保存的位置从off开始
void close() 关闭输入流
int available() 返回可以从输入流读取的字节书面
skip(long n) 从输入流中跳过参数n制定数目的字节
mark(int readlimit) 标记输入流当前位置,一遍用reset()方法复位到改标记的位置
void reset() 讲当前位置复位为上次调用mark()方法标记的位置
   

FileInputStream的构造方法

FileInputStream(File file)

file指定文件数据源头
FileInputStream(String name) name指定文件数据源,包含路径信息

示例代码

 1 /* package whatever; // don't place package name! */
 2 
 3 import java.util.*;
 4 import java.lang.*;
 5 import java.io.*;
 6 
 7 /* Name of the class has to be "Main" only if the class is public. */
 8 class InputStream
 9 {
10     public static void main (String[] args) throws java.lang.Exception
11     {
12         FileInputStream fis = new FileInputStream("H:\app\test.txt");
13         
14         int data;
15         
16         System.out.println("可读取的字节数:"+fis.available());
17         
18         //将字节转换成字符串
19         while((data = fis.read())!=-1){
20             
21             char c = (char)data;
22             
23             System.out.println(c);
24         }
25         
26         fis.close();
27     }
28 }

OutputStream体系结构

OutputStream方法

void write(int c) 将指定的字节输出到输入流中

void write(byte[] buf) 将字节数组的输出到输出流中

void write(byte[] b, int off, int len) 将字节数组中off位置开始,长度为len的字节数据输出到输出流中

void close() 关闭输出流

void flush() 强制吧任何被缓冲的已写数据输出到输出流

FileOutputStream方法

FileOutputStream(File file)  file文件名指定文件接收数据对象

FileOutputStream(String name) name指定接收对象,包含路径信息

FileOutputStream(String name, boolean append) append 若为 true,则在文件末尾添加数据.

示例代码

 1 import java.util.*;
 2 import java.lang.*;
 3 import java.io.*;
 4 
 5 /* Name of the class has to be "Main" only if the class is public. */
 6 class OutputStream
 7 {
 8     public static void main (String[] args)
 9     {
10     
11     try{
12         
13         String str = "这是输出流的示例"
14         
15         byte[] words = str.getBytes();//将str字符串解码放入字节数组中.
16         
17         FileOutputStream fos;
18         
19         fos = new FileOutputStram("H:\app\test.txt");
20         
21         fos.write(words,0,words.length);
22         
23         System.out.println("提示文件已经更新");
24         //记得关闭输出流
25         fos.close();
26         
27     }catch(FileNotFoundException e){
28         e.printStackTrace();
29     }catch(IOException e){
30         Sysout.out.println("创建文件时出错");
31     }
32     
33     }
34 }

Reader体系结构

Reader方法

int read() 从输入流中读取单个字符,返回所读取的字符数据

int read(char[] c) 从输入流中读取c.length长度的字符,保存到字符数组c中,返回实际读取的字符数

int read(char[] c, int off, int len) 从输入流中读取最多len的长度字符,保存到字符数组c中,保存的位置从

               off位置开始,返回实际读取的字符串长度

void close() 关闭流

boolean ready() 如果要读的流已经准备好,返回true,否则false

skip(long n) 从输入流中跳过参数 n 指定数目的字符

mark( int readlimit ) 标记输入流中当前的位置,以便用reset()复位到该标记

void reset() 将当前位置复位为上次调用mark()方法标记的位置

BufferedReader的构造方法

BufferedReader(Reader in) 参数in指定被装饰的Reader类

BufferedReader(Reader in, int sz) 参数sz指定缓冲区的大小,字符 为单位.

示例代码

 1 class CharReder
 2 {
 3     public static void main (String[] args)
 4     {
 5     try{
 6         
 7         FileReader fr = new FileReader("H:\app\test.txt");
 8         
 9         BuferedReader br = new BufferedReader(fr);
10         
11         String line = br.readLine();
12         while(line!=null){
13             
14             System.out.println(line);
15             line = br.readLine();
16             
17         }
18         br.close();
19         fr.close();
20     }catch(IOException e){
21         Sysout.out.println("文件不存在");
22     }
23     }
24 }

BufferedWriter的构造方法

BufferedWriter(Writer out) 参数out指定被装饰的Writer类

BufferedWriter(Writer out, int sz) 参数sz 指定缓存区的大小,字符为单位

示例代码

 1 class CharWriter
 2 {
 3     public static void main (String[] args)
 4     {
 5     try{
 6         
 7     FileWriter fw;
 8     
 9     fw = new FileWriter("H:\app\test.txt");
10     
11     BufferedWriter bw = new BufferedWriter(fw);
12     bw.write("你好,骚年");
13     bw.write("我是....");
14     bw.flush();
15     fw.close();
16     }catch(IOException e){
17         Sysout.out.println("文件不存在");
18     }
19     }
20 }

原文地址:https://www.cnblogs.com/vawa/p/4009820.html