zoukankan      html  css  js  c++  java
  • 读写文本文件

    早些时候曾提到从文件里面读取字符的方法调用的消耗可能是重大的。这个问题在计算文本文件的行数的另一个例子中也可以找到。:

    import java.io.*;

    public class line1 {

       public static void main(String args[]) {

            if (args.length != 1) {

               System.err.println("missing filename");

                System.exit(1);

            }

            try {

                FileInputStream fis = new FileInputStream(args[0]);

                BufferedInputStream bis = new BufferedInputStream(fis);

                DataInputStream dis = new DataInputStream(bis);

                int cnt = 0;

               while (dis.readLine() != null)

                     cnt++;

                dis.close();

                System.out.println(cnt);

            } catch (IOException e) {

                System.err.println(e);

            }

       }

    }这个程序使用老的DataInputStream.readLine 方法,该方法是使用用读取每个字符的 read 方法实现的。一个新方法是:

    import java.io.*;

    public class line2 {

       public static void main(String args[]) {

            if (args.length != 1) {

                System.err.println("missing filename");

                System.exit(1);

            }

            try {

                FileReader fr = new FileReader(args[0]);

                BufferedReader br = new BufferedReader(fr);

                int cnt = 0;

                while (br.readLine() != null)

                     cnt++;

                br.close();

                System.out.println(cnt);

            } catch (IOException e) {

                System.err.println(e);

            }

       }

    }这个方法更快。例如在一个有200,000行的 6 MB文本文件上,第二个程序比第一个快大约20%。

    但是即使第二个程序不是更快的,第一个程序依然有一个重要的问题要注意。第一个程序在JavaTM 2编译器下引起了不赞成警告,因为DataInputStream.readLine太陈旧了。它不能恰当的将字节转换为字符,因此在操作包含非ASCII字符的文本文件时可能是不合适的选择。(Java语言使用Unicode字符集而不是ASCII)

    这就是早些时候提到的字节流和字符流之间的区别。像这样的一个程序:

    import java.io.*;

    public class conv1 {

       public static void main(String args[]) {

            try {

                FileOutputStream fos = new FileOutputStream("out1");

                PrintStream ps = new PrintStream(fos);

                ps.println("uffffu4321u1234");

                ps.close();

            } catch (IOException e) {

                System.err.println(e);

            }

       }

    }向一个文件里面写,但是没有保存实际的Unicode字符输出。Reader/Writer I/O 类是基于字符的,被设计用来解决这个问题。OutputStreamWriter 应用于字节编码的字符。

    一个使用PrintWriter写入Unicode字符的程序是这样的:

    import java.io.*;

    public class conv2 {

       public static void main(String args[]) {

            try {

                FileOutputStream fos = new FileOutputStream("out2");

                OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");

                PrintWriter pw = new PrintWriter(osw);

                pw.println("uffffu4321u1234");

                pw.close();

            } catch (IOException e) {

                System.err.println(e);

            }

       }

    }

    这个程序使用UTF8编码,具有ASCII文本是本身而其他字符是两个或三个字节的特性。

     

  • 相关阅读:
    杂谈-论时间成本
    java笔记:排错5:误删maven target:恢复不了,怎么再生成
    十大经典排序(转发帖)
    java笔记(Idea,Maven):误删maven项目的target的class,怎么再生成target
    JDK解压版制作
    日常报错记录4:ssh工程复制粘贴顺序。
    欧朋Opera 浏览器(打不开百度)提示“您的连接不是私密连接”,解决办法
    日常报错记录2: MyBatis:DEBUG [main]
    JavaWeb-SQL-Servlet-JSP学做购物系统——日志一
    git 命令
  • 原文地址:https://www.cnblogs.com/borter/p/9434245.html
Copyright © 2011-2022 走看看