Java IO

package qs.javase.iotest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.RandomAccessFile;

public class Test {

  public static void main(String[] args) {
    try {
      BufferedInputStream in = new BufferedInputStream(new 
                                FileInputStream(new File("/home/qs/test/test.java")));
      byte[] buf = new byte[1024 * 2];
      in.read(buf, 0, buf.length);
      System.out.println(new String(buf));
    }
    catch (FileNotFoundException ex) {
      ex.printStackTrace();
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
  }
  
  // 
  public static void showAvailableBytes(InputStream is) {
    try {
      System.out.println("当前字节输入流中的字节数为:" + is.available());
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  // 以字节为单位读取文件, 常用于读取二进制文本, 如图片, 声音, 影像等文件。
  public static void readFileByBytes(String fileName) throws IOException {
    File f = new File(fileName);
    InputStream is = null;
    try {
      System.out.println("以字节为单位读取文件内容, 一次读一个字节: ");
      is = new FileInputStream(f);
      int tempByte;
      while ((tempByte = is.read()) != -1) {
        System.out.println(tempByte);
      }
    } catch (IOException e) {
      e.printStackTrace();
      return;
    }
    
    try {
      System.out.println("以字节为单位读取文件内容, 一次读取多个字节: ");
      byte[] tempBytes = new byte[200];
      int byteRead = 0;
      is = new FileInputStream(fileName);
      Test.showAvailableBytes(is);
      while ((byteRead = is.read(tempBytes)) != -1) {
        System.out.write(tempBytes, 0, byteRead);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
  
  // 以字符为单位读取文件, 常用语读取文本, 数字等类型的文件
  public static void readFileByChars(String fileName) throws IOException {
    File f = new File(fileName);
    Reader reader = null;
    try {
      System.out.println("以字符问单位读取文件内容, 一次读一个字符");
      reader = new InputStreamReader(new FileInputStream(f));
      int tempChar;
      while ((tempChar = reader.read()) != -1) {
        if (((char)tempChar) != '\r') {
          System.out.println((char)tempChar);
        }
      }
      reader.close();
    } catch(Exception e) {
      e.printStackTrace();
    } 
    
    try {
      System.out.println("以字符为单位读取文件内容, 一次读多个字符:");
      char[] tempChars = new char[32];
      int charRead = 0;
      reader = new InputStreamReader(new FileInputStream(fileName));
      while ((charRead = reader.read(tempChars)) != -1) {
        // 屏蔽 \r 不显示
        if ((charRead == tempChars.length)
            && (tempChars[tempChars.length - 1] != '\r')) {
          System.out.println(tempChars);
        } else {
          for (int i = 0; i < charRead; i++) {
            if (tempChars[i] == '\r')
              continue;
            else {
              System.out.print(tempChars[i]);
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e) {
        }
      }
    }
  }
  
  // 以行为单位读取文件, 常用语读取面向行的格式化文件
  public static void readFileByLines(String fileName) {
    File file = new File(fileName);
    BufferedReader br = null;
    try {
      System.out.println("以行为单位读取文件内容, 一次读取一整行: ");
      br = new BufferedReader(new FileReader(file));
      String tempString = null;
      int line = 1;
      // 一次读入一行, 直到读入null为文件结束
      while ((tempString = br.readLine()) != null) {
        System.out.println("line " + line + ": " + tempString);
        line++;
      }
      br.close();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (br != null) {
        try {
          br.close();
        } catch (IOException e) {
          
        }
      }
    }
  }
  
  // 随机读取文件
  public static void readFileByRandomAccess(String fileName) {
    RandomAccessFile rfile = null;
    try {
      System.out.println("随机读取一段文件内容:");
      rfile = new RandomAccessFile(fileName, "r");
      long fileLength = rfile.length();
      int beginIndex = fileLength > 4 ? 4 : 0;
      rfile.seek(beginIndex);
      byte[] buf = new byte[10];
      int byteRead = 0;
      while ((byteRead = rfile.read(buf)) != -1) {
        System.out.write(buf, 0, byteRead);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (rfile != null) {
        try {
          rfile.close();
        } catch (IOException e) {
          
        }
      }
    }
  }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值