`
红桃貳
  • 浏览: 39547 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java 学习笔记- I/O - Byte Stream

阅读更多

java basic IO 主要分 字节输入输出流,字符输入输出流,数据输入输出流,对象输入输出流:

 其大概的类接口结构图如下:


 关于字节输入输出流,所以字节输入输出流的类都继承了FileInput 或 FileOutput 这两个父类,常用的类为 FileInputStream 和 FileOutputStream,常用的构造方法为 FileInputStream(String name), FileOutputStream(String name),或 FileInputStream(File name),FileOutPutStream(File  name).

此外还有类 BufferedInputStream 与 BufferedOutputStream 也为常用的字节输入输出流类,其构造方法为 BufferedInputStream(InputStream in), BufferedOutputStream(OutputStream out);使用这两个类进行I/O操作可以减少读写的次数,从而提高程序的效率。

 

Oracle官网上用字节I/O读写文件的示例代码:

 

package io.bytestream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * IO Stream
 * 
 * 1. Byte Streams
 * 
 * All byte stream class are descended from InputStream and OutputStream.
 * 
 * @author PENGGR
 * 
 */
public class CopyBytes {

    public static void main(String[] args) {
        FileInputStream in = null;
        FileOutputStream out = null;

        try {
            in = new FileInputStream("D:\\360云盘\\javase\\src\\io\\xanadu.txt");
            out = new FileOutputStream("D:\\360云盘\\javase\\src\\io\\byteStreamOut.txt");
            int c;
            while ((c = in.read()) != -1) {
                out.write(c);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                    if (out != null) {
                        out.close();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

 

 

 

 

  • 大小: 190.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics