日期:2014-05-20 浏览次数:20741 次
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Test1 {
public static void main(String [] args) throws IOException{
File f = new File("c:\\MyDoc","test.txt");
if(f.exists()){
System.out.println("文件已存在。覆盖内容");
}else{
System.out.println("文件不存在,创建新文件");
}
FileWriter fos = new FileWriter(f);
BufferedWriter bos = new BufferedWriter(fos);
Scanner input = new Scanner(System.in);
System.out.println("请输入文本内容,输入end结束输入:");
System.out.println("==========================");
boolean flag = true;
do{
String line = input.next();
if(line.equals("end")){
flag = true;
}else{
bos.write(line);
bos.flush();
bos.newLine();
flag = false;
}
}while(!flag);
System.out.println("输出文件内容:");
System.out.println("==========================");
FileReader fr = new FileReader("c:/MyDoc/test.txt");
BufferedReader br = new BufferedReader(fr);
String line1 ;
while((line1 = br.readLine())!= null){
System.out.println(line1);
}
}
}