java.io(基本操作方式)

来源:互联网 发布:哈登什么水平知乎 编辑:程序博客网 时间:2024/06/05 06:59

in

public static String readText(String fileName){        StringBuilder stringBuilder=new StringBuilder();        try {            BufferedReader in = new BufferedReader(                    new FileReader(                            new File(fileName).getAbsoluteFile()));            try {                String s;                while ((s = in.readLine()) != null) {                    stringBuilder.append(s);                    stringBuilder.append('\n');                }            } finally {                in.close();            }        } catch (IOException e) {            throw new RuntimeException(e);        }        return stringBuilder.toString();    }
out

public static void writeText(String pathName,String text){        File file=new File(pathName);        try{            PrintWriter out=new PrintWriter(file.getAbsoluteFile());            try {                out.print(text);            }finally {                out.close();                System.out.println(pathName+" creating");            }        } catch (IOException e){            throw new RuntimeException(e);        }    }
优先使用Reader以防格式错误,老式的stream处理8位数据,新的可解决unicode编码。


原创粉丝点击