风中小郎君

导航

AVA I/O字节流

字节输入流

 

构造方法摘要
FileInputStream(File file)
          通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的File 对象 file 指定。
FileInputStream(FileDescriptor fdObj)
          通过使用文件描述符 fdObj 创建一个 FileInputStream,该文件描述符表示到文件系统中某个实际文件的现有连接。
FileInputStream(String name)
          通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。

 

read()方法

 

此方法读取数据,有多种调用语法:

int read()    类InputStream的方法

int read(byte buffername[])

int read(byte buffername[], int offset, int len)

第一种形式:从输入流中读取下一个数据字节。返回一个 0 到 255 范围内的 int 字节值。如果因为已经到达流末尾而没有字节可用,则返回 -1。在输入数据可用、检测到流末尾或抛出异常之前,此方法将一直阻塞。

第二种形式:从输入流中将 byte.length 个字节的数据读入一个 byte 数组中。在某些输入可用之前,此方法将阻塞。此方法只执行 read(b, 0, b.length) 调用并返回结果。

第三种形式:从字节输入流中给定偏移量处开始将各字节读取到指定的 byte 数组中。这种迭代的read 会一直继续下去,直到满足以下条件之一:

  • 已经读取了指定的字节数,
  • 底层流的 read 方法返回 -1,指示文件末尾(end-of-file),或者
  • 底层流的 available 方法返回 0,指示将阻塞后续的输入请求。

如果第一次对底层流调用 read 返回 -1(指示文件末尾),则此方法返回 -1。否则此方法返回实际读取的字节数。

 

利用字节流读取文件中的内容

 

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*" %>
<html>
<title><%= application.getServerInfo() %></title>
<body>
<%
  int size;
  FileInputStream f=new FileInputStream("D:/test.txt");
  size=f.available();
  out.println("total bytes is:"+size);
  BufferedInputStream buffer =new BufferedInputStream(f);
  byte bufferArray[]=new byte[90];
  int n=0;
  out.println("the text is:");
  while((n=buffer.read(bufferArray))!=-1)
  {
    String temp =new String(bufferArray,0,n); 
    out.print(temp);
  }
  buffer.close();
  f.close();
%>
</body>
</html>

 

字节输出流

 

构造方法摘要
FileOutputStream(File file)
          创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
FileOutputStream(File file, boolean append)
          创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
FileOutputStream(FileDescriptor fdObj)
          创建一个向指定文件描述符处写入数据的输出文件流,该文件描述符表示一个到文件系统中的某个实际文件的现有连接。
FileOutputStream(String name)
          创建一个向具有指定名称的文件中写入数据的输出文件流。
FileOutputStream(String name, boolean append)
          创建一个向具有指定 name 的文件中写入数据的输出文件流。

 

write()方法

 

此方法向输出流中写入数据,有多种调用语法:

write(int b)

write(byte[] buffername)

write(byte [] buffername, int offset, int len)

第一种形式:将指定的字节写入此输出流,参数是一个整数,它允许不必把参数转换成字节型就可以调用

第二种形式:将 buffername.length 个字节从指定 byte 数组写入此文件输出流中

第三种形式:将指定 byte 数组中从偏移量 offset 开始的 len 个字节写入此文件输出流。

 

 利用字节流把内容写入文件

 

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*" %>
<html>
<title><%= application.getServerInfo() %></title>
<body>
<%
  int size;
 
 FileOutputStream outf=new FileOutputStream("D:/test.txt");
  BufferedOutputStream bufferout= new BufferedOutputStream(outf);
  byte b[]="这是写入文档的内容。".getBytes();
  bufferout.write(b);
  bufferout.flush();
  bufferout.close();
  FileInputStream f=new FileInputStream("D:/test.txt");
  size=f.available();
  out.println("total bytes is:"+size);
 
 BufferedInputStream buffer =new BufferedInputStream(f);
  byte bufferArray[]=new byte[90];
  int n=0;
  out.print("the text is:");
  while((n=buffer.read(bufferArray))!=-1)
  {
    String temp =new String(bufferArray,0,n); 
 out.print(temp);
  }
  buffer.close();
  f.close();
%>
</body>
</html>

posted on 2015-06-02 12:53  风中小郎君  阅读(99)  评论(0)    收藏  举报