第15章练习题第三题答案不符合题意,并且运行结果不对,打开word.txt,里边是空白的,有一行代码格式过时了
import java.io.*;
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.err.println("请输入一个名字:");
String name = sc.nextLine();
if ("done".equalsIgnoreCase(name)) {
break;
}
System.err.println("请输入密码:");
String phone = sc.nextLine();
writeFile(name + "," + phone);
}
System.out.println("--程序结束--");
sc.close();
}
static private void writeFile(String message) {
try (FileOutputStream fo = new FileOutputStream("word.txt", true);
OutputStreamWriter ows = new OutputStreamWriter(fo);
BufferedWriter bw = new BufferedWriter(ows);) {
bw.write(message);
bw.newLine();
bw.flush();
System.out.println("信息已经写入文件");
} catch (IOException e) {
e.printStackTrace();
}
}
}
根号申 发表于2018-12-04 08:56
import java.io.*;
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.err.println("请输入一个名字:");
String name = sc.nextLine();
if ("done".equalsIgnoreCase(name)) {
break;
}
System.err.println("请输入密码:");
String phone = sc.nextLine();
writeFile(name + "," + phone);
}
System.out.println("--程序结束--");
sc.close();
}
static private void writeFile(String message) {
try (FileOutputStream fo = new FileOutputStream("word.txt", true);
OutputStreamWriter ows = new OutputStreamWriter(fo);
BufferedWriter bw = new BufferedWriter(ows);) {
bw.write(message);
bw.newLine();
bw.flush();
System.out.println("信息已经写入文件");
} catch (IOException e) {
e.printStackTrace();
}
}
}
多谢多谢