Java读写文本文件。

来源:互联网 发布:网络童星公司 编辑:程序博客网 时间:2024/06/05 18:12
String path = "G:\\test.txt";BufferedWriter write = null;try {write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), "UTF-8"));for (int i = 0; i < 1000; i++) {write.append(String.valueOf(Math.random()));write.append(System.lineSeparator());}write.flush();} catch (IOException e) {e.printStackTrace();} finally {try {write.close();} catch (IOException e) {e.printStackTrace();}}BufferedReader reader = null;try {reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));String line;while ((line = reader.readLine()) != null) {System.out.print(line);System.out.print(System.lineSeparator());}} catch (IOException e) {e.printStackTrace();} finally {try {reader.close();} catch (IOException e) {e.printStackTrace();}}  

0 0