通过Scanner读两句话,并写到一个文件中.通过PrintWriter来实现
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
//通过Scanner读两句话,并写到一个文件中.通过PrintWriter来实现
public class TestPrintWriter {
public static void main(String[] args) {
PrintWriter pw=null;
Scanner scanner=new Scanner(System.in);
try {
FileOutputStream fos=new FileOutputStream("D:\\MyWork\\MyProject\\temp.txt");
pw=new PrintWriter(fos,true);//自动冲刷
System.out.println("请输入两句话:");
String s1=scanner.next();
String s2=scanner.next();
pw.println(s1);
pw.println(s2);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}