当前位置:   article > 正文

文件读写中的inputStream和outputStream_asinputstream

asinputstream

InputStream(输入流)用来读取数据

OutputStream(输出流)用来写出数据

 

public int read();//从此输入流中读取一个数据字节

public int read(byte[] b);//从此输入流中将最多b.length个字节数据读取到byte数组中

  1. public int read(byte b[]) throws IOException {
  2. Object traceContext = IoTrace.fileReadBegin(path);
  3. int bytesRead = 0;
  4. try {
  5. bytesRead = readBytes(b, 0, b.length);
  6. } finally {
  7. IoTrace.fileReadEnd(traceContext, bytesRead == -1 ? 0 : bytesRead);
  8. }
  9. return bytesRead;
  10. }

public int read(byte[] b,int off,int len);//从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。off:目标数组 b 中的起始偏移量。

  1. public int read(byte b[], int off, int len) throws IOException {
  2. Object traceContext = IoTrace.fileReadBegin(path);
  3. int bytesRead = 0;
  4. try {
  5. bytesRead = readBytes(b, off, len);
  6. } finally {
  7. IoTrace.fileReadEnd(traceContext, bytesRead == -1 ? 0 : bytesRead);
  8. }
  9. return bytesRead;
  10. }
  1. package fileDo;
  2. import java.io.FileInputStream;
  3. /**
  4. * @auther **
  5. * @date 7/31/2018 3:02 PM
  6. */
  7. public class FileReadStream {
  8. public static void main(String[] args){
  9. String content = null;
  10. try{
  11. int size = 0;
  12. //定义一个字节缓冲区,该缓冲区的大小根据需要来定义
  13. byte[] buffer = new byte[1024];
  14. FileInputStream fis = new FileInputStream("d:/read.txt");
  15. //循环读取文件中的数据
  16. while((size = fis.read(buffer)) != -1){
  17. content = new String(buffer, 0, size);
  18. System.out.println(content);
  19. }
  20. fis.close();
  21. }catch (Exception e){
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/147193
推荐阅读
相关标签
  

闽ICP备14008679号