package number;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class WriteFile {
public static void main(String[] args) {
File file=new File("temp.txt");//txt文档是E:\workspace\study(你自己的工作区目录)下的temp.txt文档
try{
//先写入内容到指定的文档
System.out.println("请输入文件的内容:");
InputStreamReader isr=new InputStreamReader(System.in );
BufferedReader br=new BufferedReader(isr);
String str=br.readLine();
FileWriter fw=new FileWriter(file);
PrintWriter pw=new PrintWriter(fw);
while(!str.equals("")){
pw.println(str);
str=br.readLine();
}
br.close();
pw.close();
System.out.println("写入内容成功!");
//读取文档里面的内容
FileReader fr=new FileReader(file);
BufferedReader br2=new BufferedReader(fr);
String s=br2.readLine();
System.out.println("文档内容为:");
while(s!=null){
System.out.println(s);
s=br2.readLine();
}
br2.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class WriteFile {
public static void main(String[] args) {
File file=new File("temp.txt");//txt文档是E:\workspace\study(你自己的工作区目录)下的temp.txt文档
try{
//先写入内容到指定的文档
System.out.println("请输入文件的内容:");
InputStreamReader isr=new InputStreamReader(System.in );
BufferedReader br=new BufferedReader(isr);
String str=br.readLine();
FileWriter fw=new FileWriter(file);
PrintWriter pw=new PrintWriter(fw);
while(!str.equals("")){
pw.println(str);
str=br.readLine();
}
br.close();
pw.close();
System.out.println("写入内容成功!");
//读取文档里面的内容
FileReader fr=new FileReader(file);
BufferedReader br2=new BufferedReader(fr);
String s=br2.readLine();
System.out.println("文档内容为:");
while(s!=null){
System.out.println(s);
s=br2.readLine();
}
br2.close();
}catch(IOException e){
e.printStackTrace();
}
}
}