在java.io包中流的操作主要有字节流、字符流两大类,并且两个都具备输入输出的操作。
在字节流中输出数据主要使用OutputStream类完成,输入则是InputStream类。
在字符流中输出数据主要使用Writer类完成,输入则是Reader类。
字节流:
字节流主要操作byte类型数据,以byte数组为准,主要操作类是OutputStream类和InputStream类
(1)字节输出流:OutputStreame
OutputStream类是一个抽象类,如若要使用此类,首先就必须要通过子类来实例化对象。可以使用FileOutputStream类,通过向上转型后,可以为OutputStream实例化,在OutputStream类中的主要操作方法如下:
1 public void close() throws IOException //关闭输出流 2 public void flush() throws IOException //刷新缓冲区 3 public void write(byte[] b) throws IOException //将一个byte数组写入数据流 4 public void write(byte[] b,int off,int len) throws IOException //将一个指定范围的byte数组写入数据流 5 public abstract void write(int b) throws IOException //将一个字节数据写入数据流
下面举一个例子:
1 import java.io.*; 8 public class OutputStreamDemo1 { 9 public static void main(String[] args) throws IOException { 10 /*使用File指定一个文件*/ 11 File f = new File("d:"+File.pathSeparator+"test.txt");//申明File对象。File.separator是分隔同一个路径字符串中的目录的,例如:C:/Program Files/Common Files,就是指“/”
12 /*通过子类实例化父类*/ 13 OutputStream out; //输出对象 14 out = new FileOutputStream(f); //通过向上转型后,可以为OutputStream实例化 15 /*写入操作*/ 16 String str = "Hello , World"; //字符串 17 byte b[] = str.getBytes(); //因为只能输出byte数组,所以将字符串变为byte数组 18 out.write(b); //将内容输出 19 /*关闭输出流*/ 20 out.close(); 21 } 22 }
(2)字节输入流 InputStream
既然程序可以向文件写入内容,则可以通过InputStream从文件中把数据读取进程序当中。
InputStream类中的主要方法如下:
public int availabel() throws IOException //可以取得输入文件的大小 public void close() throws IOException //关闭输入流 public abstract int read() throws IOException //读取内容,以数字方式读取 public int read(byte[] b) throws IOException //将内容读到byte数组中同时返回读入的个数
通过一个例子来看看如何从文件中读取内容:
import java.io.*; /** * Author : Chen Junren * Created on 2019/9/17 下午 4:43 * Description : */ public class InputStreamDemo2 { public static void main(String[] args) throws IOException { File f = new File("d:"+ File.pathSeparator+"test.txt"); InputStream is; is = new FileInputStream(f); //length()方法,此方法可以取得文件的大小,来开辟指定的byte数组空间 byte[] b = new byte[(int)f.length()]; is.read(b); is.close(); System.out.println("内容为:"+new String(b));//将数组变成字符串
}
}
字符流:
在程序中一个字符等于两个字节,那么Java提供了Reader和Writer两个专门操作字符流的类。
(1)字符输出流 Writer,Writer类的常用方法如下:
public abstract void close() throws IOException //关闭输出流 public void write(String str) throws IOException //将字符串输出 public void write(char[] cbuf) throws IOException //将字符数组输出 public abstract void flush() throws IOException //强制性清空缓存
例子如下:
public class WriterDemo1 { public static void main(String[] args) throws IOException { File f = new File("d:"+File.pathSeparator+"test.txt"); Writer out; out = new FileWriter(f); String str = "Hello,World"; out.write(str); out.flush(); out.close(); } }
整个代码看下来和OutputStream没有太大的区别,唯一的好处就是可以直接输出字符串,而不用再将字符串变为byte数组之后再输出。
(2)字符输入流Reader,Reader类的常用方法:
1 public abstract void close() throws IOException //关闭输出流 2 public int read() throws IOException //读取单个字符 3 public int read(char[] cbuf) throws IOException //将内容读到字符数组内,返回读入的长度
以下放出实例来读取文件中的文本内容:
public class ReaderDemo1 { public static void main(String[] args) throws IOException { File f = new File("d:"+File.pathSeparator+"test.txt"); Reader reader = new FileReader(f); char[] c = new char[1024]; int len = reader.read(c); reader.close(); System.out.println("内容为:"+new String(c,0,len)); } }