赞
踩
InputStream(输入流)用来读取数据
OutputStream(输出流)用来写出数据
public int read();//从此输入流中读取一个数据字节
public int read(byte[] b);//从此输入流中将最多b.length个字节数据读取到byte数组中
- public int read(byte b[]) throws IOException {
- Object traceContext = IoTrace.fileReadBegin(path);
- int bytesRead = 0;
- try {
- bytesRead = readBytes(b, 0, b.length);
- } finally {
- IoTrace.fileReadEnd(traceContext, bytesRead == -1 ? 0 : bytesRead);
- }
- return bytesRead;
- }
public int read(byte[] b,int off,int len);//从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。off:目标数组 b 中的起始偏移量。
- public int read(byte b[], int off, int len) throws IOException {
- Object traceContext = IoTrace.fileReadBegin(path);
- int bytesRead = 0;
- try {
- bytesRead = readBytes(b, off, len);
- } finally {
- IoTrace.fileReadEnd(traceContext, bytesRead == -1 ? 0 : bytesRead);
- }
- return bytesRead;
- }
package fileDo; import java.io.FileInputStream; /** * @auther ** * @date 7/31/2018 3:02 PM */ public class FileReadStream { public static void main(String[] args){ String content = null; try{ int size = 0; //定义一个字节缓冲区,该缓冲区的大小根据需要来定义 byte[] buffer = new byte[1024]; FileInputStream fis = new FileInputStream("d:/read.txt"); //循环读取文件中的数据 while((size = fis.read(buffer)) != -1){ content = new String(buffer, 0, size); System.out.println(content); } fis.close(); }catch (Exception e){
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。