Java-->PrintStream
--> 用过之后都不想用read、write了,感觉还是挺方便的...
package com.dragon.java.hwletter; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Scanner; /* * 编写程序来实现如下功能 a:在C盘下创建一个目录Letter b:在控制台显示下列选项:1 查看请假条 2 撰写请假条 c:如果用户选择2,则提示用户撰写请假条,并把撰写的内容存入到Letter文件夹下。 格式如下: 请假人:王宝强 请假日期:2016年8月15日 请假原因:向法院起诉马蓉离婚.....先请假一天等等 d:如果用户选择1,则在控制台输出请假条的内容。 */ public class Test { private static String filePath = "D:/workspace/08-15/src/com/dragon/java/hwletter/Letter/letter.txt"; private static String line = null; public static void main(String args[]) { Scanner scanner = new Scanner(System.in); new File(filePath.replace("/letter.txt", "")).mkdirs(); while (true) { System.out.println("1 查看请假条 2 撰写请假条"); switch (scanner.nextInt()) { case 1: if (!(new File(filePath).exists())) { System.out.println("目前还没有请假条!"); continue; } readFile(); break; case 2: writeFile(); break; default: System.out.println("输入有误!..."); break; } } } private static void readFile() { System.out.println("请假条:"); BufferedReader br = null; try { br = new BufferedReader(new FileReader(filePath)); while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { System.out.println(e); } } private static void writeFile() { System.out.println("请写请假条:"); BufferedWriter bw = null; try { BufferedReader br = new BufferedReader(new InputStreamReader( System.in, "gbk")); new File(filePath).createNewFile(); bw = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(filePath))); while (true) { line = br.readLine(); if ("end".equals(line)) { break; } else if (line == null) { br.close(); break; } bw.write(line); bw.newLine(); } } catch (IOException e) { System.out.println(e); } finally { try { // br.close(); bw.close(); } catch (IOException e) { System.out.println(e); } } } }
--> 但是以后Android 都没有PrintStream 了啊...