java 中IO 流的知识

来源:互联网 发布:知乎怎么匿名 编辑:程序博客网 时间:2024/05/21 06:49

字节流:一次读入或读出是8位二进制。

字符流:一次读入或读出是16位二进制。

1,字节流   字符流 :


 1.1  字节流 :

        InputStream 输入流  抽象类  :

InputStream  的方法

 available()  返回输入流中可以读到的字节个数,返回值为int

     close()  关闭此输入流并释放与流关联的任何系统资源,返回值为void 
      read()  从输入流读取下一个数据字节
     read(byte[] b) 从输入流中读取一定数量的字节数,并将它们存储到缓冲区数组中,返回值为int
     read(byte[] b, int off, int len) 从输入流中读取到数据的len个字节,存入数组中,返回值为int 返回总字节数或者-1,若为-1,则表示文件以读取完毕
文件输入流:FileInputStream 

     FileInputStream  fs = new FileInputStream  (File  f)   //  f传入一个文件对象
     Byte[]  b = new Byte[1024]
      int   len =  fs.read(Byte[]  b);
        len 此乃文件内容的直接数,如果内容读取完毕就返回-1
   
OutputStream 输出流  抽象类

OutputStream  的方法

    close()  关闭此输入流并释放与流关联的任何系统资源,返回值为void 
     flush()  刷新输出流,使缓存数据被写出来。
    write(byte[] b) 将指定b长度的字节数组写入到输出流。
    write(byte[] b, int off, int len) 将off到len个字节写入字节数组中
    write(int b) 将指定字节写入该输出流中。

OutputStream 的子类: 

FileOutputStream 

  FileOutputStream(File file, boolean append)
  //append表示所添加的数据不会覆盖之前的已经存在的数据
 
   OutputStream  os = newOutputStream  (File  f) //  f传入一个文件对象  如果对象存在直接使用没有就直接创建
   OutputStream  os = new OutputStream  (File  f,boolean b)//f同上,b  再次向文件中写入数据是不是覆盖之前已有的文件内容是追加已有内容的后面
    Byte[]  b = new Byte[1024]
  os.write(Byte[]  b)
//通过字节输入流对文件内容进行读取 ctrl+z 一次性将文件内容读取完成        public static void readFile (File f)        {            InputStream is = null;            try {                //获取基于指定文件的输入流对象                is = new FileInputStream(f);                //获取当前流中的可读字节数(流中的指定输入源的字节)                int len = is.available();                //声明一个字节数组用于存储从流中读取到的字节信息,长度为流中的可读字节数                byte[] b = new byte[len];                //将流中读取到的所有字节装入字节数组;返回本次读取的字节总数                is.read(b);                //根据字节数组将字节数组中的数据(字节)解码为String对象                String s = new String(b);                System.out.println(s);            } catch (FileNotFoundException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            } finally {                try {                    //判断当输入流对象不为null时才关闭资源,否则会出现空指针异常                    if (is != null) {                        is.close();                    }                } catch (IOException e) {                    e.printStackTrace();                }            }        }        //分布读取文件内容        public static void readFile2 (File f)        {            InputStream is = null;            try {                //获取基于指定文件的输入流对象                is = new FileInputStream(f);                //初始化每次读取的字节数(标记每一次读取的数据的真实长度)                int len = 0;                //声明一个字节数组用于存储从流中读取到的字节信息(字节缓冲区)                byte[] b = new byte[1024];                //循环读取流中的数据并且装入字节数组,一旦读取后返回值为-1则说明流中的数据全部读取完成                while ((len = is.read(b)) != -1) {                    //将每一次读取的字节数组(字节数组中的字节)转码成String对象                    //参数1:需要转码的字节数组;参数2:开始解码的位置;参数3:数组中的有效字节长度                    String s = new String(b, 0, len);                    System.out.println(s);                }            } catch (FileNotFoundException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            } finally {                try {                    //判断当输入流对象不为null时才关闭资源,否则会出现空指针异常                    if (is != null) {                        is.close();                    }                } catch (IOException e) {                    e.printStackTrace();                }            }        }        //将指定的字符串写入到指定的文件中        public static void writeFile (String s, File f){            OutputStream os = null;            try {                //获取针对于指定文件的输出流(追加模式)                os = new FileOutputStream(f, true);                //将需要写入的文本内容转换为字节数组                byte[] byt = s.getBytes();                //将字节输入通过输出流写入指定输出目标                os.write(byt);            } catch (FileNotFoundException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            } finally {                try {                    if (os != null) {                        os.close();                    }                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }
1.2  字符流 :
  reader  :字符输入流
   writer :字符输出流
 //使用字符输入流读取文件中的内容
 public static void readFile(File file)
 {
 Reader reader = null;
 try {
//创建基于指定文件的字符输入流
reader = new FileReader(file);
//定义一个字符缓冲区,用于存储每次读取到的字符
char[] c = new char[10];
//标记每次读取的真实字符个数
int len = 0;
//循环读取
while((len = reader.read(c)) != -1){
//将指定的字符数组转换为String对象
String s = new String(c,0,len);
System.out.println(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {

File file = new File("E:/book/books/鲁迅.txt");
CharStreamDemo.readFile(file);
}

2,高级流:
 2.1 BufferedReader
 2.2 BufferedWriter
 2.3 BufferedInputStream
 2.4 BufferedOutputStream 

//使用字符缓冲输入流读取指定文件
public static void readFile(File f) throws IOException{
//将一个节点流通过缓冲流进行包装,目的在于提高读取效率
BufferedReader br = new BufferedReader(new FileReader(f));
String str = "";
while((str = br.readLine()) != null)
{
System.out.println(str);
}
}

//使用字符缓冲输出流向文件写入内容
public static void writeFile(String msg,File f) throws IOException{
FileWriter fw = new FileWriter(f,true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(msg);
//bw.flush();
bw.close();
}

/**
* 将一个文件拷贝到一个指定目录中去
* @param source源文件
* @param targeDir目标目录
*/
public static void copy(File source,File targetDir){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
//根据目标目录组合文件名称,形成一个新的文件对象(目标文件)
File targetFile = new File(targetDir,source.getName());
try {
//获取源文件的输入流并包装为高级流
bis = new BufferedInputStream(new FileInputStream(source));
//获取目标文件的输出流
bos = new BufferedOutputStream(new FileOutputStream(targetFile));
byte[] b = new byte[1024];
int len = 0;
System.out.println("开始拷贝....");
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
}
System.out.println("拷贝完成!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(bos != null)bos.close();
if(bis != null)bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}



3,打印流:
3.1 printStream
3.2 printWriter


public static void main(String[] args) throws FileNotFoundException {

String msg = "为you服务!!!!!!!";
PrintWriter pw = new PrintWriter(new FileOutputStream("t.txt",true));
pw.println(msg);
pw.close();

System.out.println(msg);

}

4,任意访问流:
RandomAccessFile 
File file = new File("test.txt");
RandomAccessFile raf = new RandomAccessFile(file,"rw");
raf.writeUTF("helloworld");
// raf.close();
raf.seek(0);

String i = raf.readUTF();
System.out.println(i);


public static void write(String s,File f) throws IOException{
RandomAccessFile raf = new RandomAccessFile(f,"rw");
raf.writeUTF(s);
raf.close();
}

public static void read(File f) throws IOException{
RandomAccessFile raf = new RandomAccessFile(f, "r");
// byte[] b = new byte[1024];
// int len = 0;
raf.seek(0);
String msg = "";
while((msg = raf.readUTF()) != null){
System.out.println(msg);
}
raf.close();
}

public static void main(String[] args) throws IOException {

File f = new File("c:/raf.txt");
// Demo2.write("用一下试试!",f);
Demo2.read(f);
}
5,获取系统流:
//获取系统输入流(标准输入)
InputStream is = System.in;
byte[] b = new byte[128];
int len = 0;
while((len = is.read(b)) != -1){
String s = new String(b,0,len);
if("quit".equals(s.trim())){
System.exit(0);
break;
}
System.out.println(s.trim());
}
6,转换流:
  6.1 InputStreamReader :用于输入流,将字节转换为字符
  6.2  OutputStreamWriter :用于输出流,将字符转换为字节
/********字节流转换为字符流并包装为字符缓冲流********/
InputStream is = System.in;
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
//System.out.println(br.readLine());
//br.close();

/********把字符转换为字节流输出到指定目标************/
OutputStream os = System.out;
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
//BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write("所谓伊人,在水一方!");
bw.close();
}

2 0