Java I/O流(输入/输出流) IO:Java对数据的操作是通过流的方式,IO流用来处理设备之间的数据传输,上传文件和下载文件,Java用于操作流的对象都在IO包中。
InputStream 和 OutputStream 两个类都是抽象类,抽象类不能进行实例化。在实际应用中我们要用到一系列基本数据流类,都是他们的子类,在实现其父类方法的同时又都定义了其特有的功能。
字节流基类 InputStream:
abstract int read () int read (byte [] b) int read (byte [] b, int off, int len)
long skip (long n) void close ()
OutputStream:
abstract void write (int b) void write (byte [] b) void write (byte [] b, int off, int len)
文件字节流 FileInputStream:
FileInputStream(File file) FileInputStream(String name)
InputStream inputStream = new FileInputStream(new File("f://hello//test.txt" )); int i = 0 ;while ((i = inputStream.read()) != -1 ) { System.out.print((char ) i + " " ); } inputStream.close();
InputStream inputStream2 = new FileInputStream("f://hello/test.txt" ); byte [] b = new byte [2 ];int i2 = 0 ;while ((i2 = inputStream2.read(b)) != -1 ) { System.out.print(new String(b, 0 , i2) + " " ); } inputStream2.close();
注: 一次读取一个字节数组,提高了操作效率,IO流使用完毕一定要关闭。
FileOutputStream:
FileOutputStream(File file) FileOutputStream(File file, boolean append) FileOutputStream(String name) FileOutputStream(String name, boolean append)
OutputStream outputStream = new FileOutputStream(new File("test.txt" )); outputStream.write("ABCD" .getBytes()); outputStream.close(); OutputStream outputStream2 = new FileOutputStream("test.txt" , true ); outputStream2.write("\r\n" .getBytes()); outputStream2.write("hello" .getBytes()); outputStream2.close();
注1:输出的目的地文件不存在,则会自动创建,不指定盘符的话,默认创建在项目目录下;
注2:输出换行符时一定要写\r\n不能只写\n,因为不同文本编辑器对换行符的识别存在差异性。
字节缓冲流 BufferedInputStream
BufferedInputStream(InputStream in) BufferedInputStream(InputStream in, int size)
InputStream in = new FileInputStream("test.txt" ); BufferedInputStream bis = new BufferedInputStream(in); byte [] bs = new byte [20 ];int len = 0 ;while ((len = bis.read(bs)) != -1 ) { System.out.print(new String(bs, 0 , len)); } bis.close();
BufferedOutputStream
BufferedOutputStream(OutputStream out) BufferedOutputStream(OutputStream out, int size)
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("test.txt" , true )); bos.write("\r\n" .getBytes()); bos.write("Hello Android" .getBytes()); bos.flush(); bos.close();
字符流基类 Reader:
int read () int read (char [] cbuf) abstract int read (char [] cbuf, int off, int len)
long skip (long n) abstract void close ()
Writer:
void write (char [] cbuf) abstract void write (char [] cbuf, int off, int len) void write (int c) void write (String str) void write (String str, int off, int len)
Writer append (char c) Writer append (CharSequence csq) Writer append (CharSequence csq, int start, int end)
abstract void close () abstract void flush ()
字符转换流 InputStreamReader
InputStreamReader(InputStream in) InputStreamReader(InputStream in, Charset cs) InputStreamReader(InputStream in, CharsetDecoder dec) InputStreamReader(InputStream in, String charsetName)
特有方法:
InputStreamReader reader = new InputStreamReader(new FileInputStream("test.txt" )); int len;while ((len = reader.read()) != -1 ) { System.out.print((char ) len); } reader.close(); InputStreamReader reader = new InputStreamReader(new FileInputStream("test.txt" ),"utf-8" ); int len;while ((len = reader.read()) != -1 ) { System.out.print((char ) len); } reader.close();
OutputStreamWriter
OutputStreamWriter(OutputStream out) OutputStreamWriter(OutputStream out, Charset cs) OutputStreamWriter(OutputStream out, CharsetEncoder enc) OutputStreamWriter(OutputStream out, String charsetName)
特有方法:
字符缓冲流(高效流) BufferedReader
BufferedReader(Reader in) BufferedReader(Reader in, int sz)
特有方法:
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt" ))); String str; while ((str = reader.readLine()) != null ) { System.out.println(str); } reader.close();
BufferedWriter
BufferedWriter(Writer out) BufferedWriter(Writer out, int sz)
特有方法:
文件字符流 FileReader
FileReader:InputStreamReader类的直接子类,用来读取字符文件的便捷类,使用默认字符编码。
FileWriter
FileWriter:OutputStreamWriter类的直接子类,用来写入字符文件的便捷类,使用默认字符编码。
高效流效率对比 读取方式一
FileInputStream inputStream = new FileInputStream("f://滑板//HEEL_FLIP.mp4" ); FileOutputStream outputStream = new FileOutputStream("HEEL_FLIP.mp4" ); int len;long begin = System.currentTimeMillis();while ((len = inputStream.read()) != -1 ) { outputStream.write(len); } System.out.println(System.currentTimeMillis() - begin); inputStream.close(); outputStream.close();
读取方式二
FileInputStream inputStream = new FileInputStream("f://滑板//HEEL_FLIP.mp4" ); FileOutputStream outputStream = new FileOutputStream("HEEL_FLIP.mp4" ); int len;byte [] bs = new byte [1024 ];long begin = System.currentTimeMillis();while ((len = inputStream.read(bs)) != -1 ) { outputStream.write(bs, 0 , len); } System.out.println(System.currentTimeMillis() - begin); inputStream.close(); outputStream.close();
读取方式三
FileInputStream inputStream = new FileInputStream("f://滑板//HEEL_FLIP.mp4" ); BufferedInputStream bis = new BufferedInputStream(inputStream); FileOutputStream outputStream = new FileOutputStream("HEEL_FLIP.mp4" ); BufferedOutputStream bos = new BufferedOutputStream(outputStream); int len;byte [] bs = new byte [1024 ];long begin = System.currentTimeMillis();while ((len = bis.read(bs)) != -1 ) { bos.write(bs, 0 , len); } System.out.println(System.currentTimeMillis() - begin); bis.close(); bos.close();
注:由此可以看出高效缓冲流读写速度是非常快的,建议使用