zoukankan      html  css  js  c++  java
  • io工具类

    文件读写工具类

    public class TextFile extends ArrayList<String> {
      // Read a file as a single string:
      public static String read(String fileName) {
        StringBuilder sb = new StringBuilder();
        try {
          BufferedReader in= new BufferedReader(new FileReader(
            new File(fileName).getAbsoluteFile()));
          try {
            String s;
            while((s = in.readLine()) != null) {
              sb.append(s);
              sb.append("
    ");
            }
          } finally {
            in.close();
          }
        } catch(IOException e) {
          throw new RuntimeException(e);
        }
        return sb.toString();
      }
      // Write a single file in one method call:
      public static void write(String fileName, String text) {
        try {
          PrintWriter out = new PrintWriter(
            new File(fileName).getAbsoluteFile());
          try {
            out.print(text);
          } finally {
            out.close();
          }
        } catch(IOException e) {
          throw new RuntimeException(e);
        }
      }
      // Read a file, split by any regular expression:
      public TextFile(String fileName, String splitter) {
        super(Arrays.asList(read(fileName).split(splitter)));
        // Regular expression split() often leaves an empty
        // String at the first position:
        if(get(0).equals("")) remove(0);
      }
      // Normally read by lines:
      public TextFile(String fileName) {
        this(fileName, "
    ");
      }
      public void write(String fileName) {
        try {
          PrintWriter out = new PrintWriter(
            new File(fileName).getAbsoluteFile());
          try {
            for(String item : this)
              out.println(item);
          } finally {
            out.close();
          }
        } catch(IOException e) {
          throw new RuntimeException(e);
        }
      }
      // Simple test:
      public static void main(String[] args) {
        String file = read("TextFile.java");
        write("test.txt", file);
        TextFile text = new TextFile("test.txt");
        text.write("test2.txt");
        // Break into unique sorted list of words:
        TreeSet<String> words = new TreeSet<String>(
          new TextFile("TextFile.java", "\W+"));
        // Display the capitalized words:
        System.out.println(words.headSet("a"));
      }
    }

    二进制文件读取工具类:

    public class BinaryFile {
      public static byte[] read(File bFile) throws IOException{
        BufferedInputStream bf = new BufferedInputStream(
          new FileInputStream(bFile));
        try {
          byte[] data = new byte[bf.available()];
          bf.read(data);
          return data;
        } finally {
          bf.close();
        }
      }
      public static byte[]
      read(String bFile) throws IOException {
        return read(new File(bFile).getAbsoluteFile());
      }
    } 
  • 相关阅读:
    使用Word发布文章到 WordPress 博客
    Wordpress上传到阿里云服务器
    IntelliJ设置鼠标悬浮提示和修改快捷键
    梅塔幻灯片如何设置图片高度不被裁减
    更改XAMPP中MySQL数据库的端口号
    PHP开启cURL功能
    Android Studio使用百度地图示例BaiduMapsApiASDemo
    CocosCreator反射在Android中的使用
    Android Studio新建一个HelloWorld 程序(App)
    无法中止进程无法访问操作拒绝访问
  • 原文地址:https://www.cnblogs.com/qilong853/p/6516412.html
Copyright © 2011-2022 走看看