import java.util.Scanner;
import java.io.*;
class SystemInDemo {
public static void main(String[] args) throws Exception {
SystemInDemo sid = new SystemInDemo();
sid.haha();
}
public void haha() throws Exception{
while (true){
System.out.println("输入i/I对TXT文件进行写入,输入p/P察看,输入其他字符退出");
String ss = (new Scanner(System.in)).next();
if (ss.equalsIgnoreCase("i")){
System.out.println("输入的是i/I,执行输入,请输入文字.");
BufferedReader br = null;
BufferedWriter bw = null;
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new FileWriter("e:/MyCode/Day8/FileCopy_bak.java",true));
String s =null;
s = br.readLine();
while (s!=null) {
if (s.equalsIgnoreCase("quit")) {
br.close();
bw.flush();
bw.close();
break;
}
bw.write(s);
s = br.readLine();
bw.newLine();
bw.flush();
}
//continue;
}else if(ss.equalsIgnoreCase("p")){
System.out.println("输入的是p/P,执行打印到屏幕,您将看到文本文档内的内容.");
FileReader fr = new FileReader("e:/MyCode/Day8/FileCopy_bak.java");
BufferedReader br = new BufferedReader(fr);
int k;
System.out.print((char)br.read());
while ((k=br.read())!=-1){
System.out.print((char)k);
}
br.close();
//continue;
}else
System.exit(-1);
}
}
}
我输入i之后,一旦输入quit,就报错。。 |
|