除非你打算做一些有用的事情,否則千萬不要發生異常。
我得到它的工作。這非常直截了當。
Enter path to save your file : myfile.bin
Enter Employee ID : 99
Enter Employee Name : Rick Hightower
Enter Employee Salary : 99
Add more records [true/false]? true
Enter Employee ID : 77
Enter Employee Name : Dippy Do
Enter Employee Salary : 88
Add more records [true/false]? false
以下是我有: ...
public static class Employee implements Serializable {
int id;
String name;
int salary;
public Employee(int id, String name, int salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
}
static Employee getData() throws IOException {
BufferedReader rdr = new BufferedReader(
new InputStreamReader(
new DataInputStream(System.in)
));
System.out.printf("Enter Employee ID : ");
int tmpid = Integer.parseInt(rdr.readLine());
System.out.printf("Enter Employee Name : ");
String tmpname = rdr.readLine();
System.out.printf("Enter Employee Salary : ");
int tmpsalary = Integer.parseInt(rdr.readLine());
//rdr.close(); this is why... you broke it :)
return new Employee(tmpid, tmpname, tmpsalary);
}
public static void main(String []args) throws Exception {
boolean moreRecords = true;
String path = null;
Scanner scanner = new Scanner(System.in);
File file = null;
System.out.printf("Enter path to save your file : ");
path = scanner.next();
file = new File(path);
while (moreRecords) {
Employee rec = Main.getData();
ObjectOutputStream dos = new ObjectOutputStream(new FileOutputStream(file));
dos.writeObject(rec);
dos.close();
System.out.printf("Add more records [true/false]? ");
moreRecords = scanner.nextBoolean();
}
這主要是與帶走一些地區代碼。
您遇到的最大問題是您正在關閉輸入流。
static Employee getData() throws IOException {
BufferedReader rdr = new BufferedReader(
new InputStreamReader(
new DataInputStream(System.in)
));
System.out.printf("Enter Employee ID : ");
int tmpid = Integer.parseInt(rdr.readLine());
System.out.printf("Enter Employee Name : ");
String tmpname = rdr.readLine();
System.out.printf("Enter Employee Salary : ");
int tmpsalary = Integer.parseInt(rdr.readLine());
//rdr.close(); this is why... you broke it :) <-------------------SEE
return new Employee(tmpid, tmpname, tmpsalary);
}
Java I/O流使用裝飾器模式,因此它只是將關閉調用委託給內部流。
解決了這個問題。你的代碼有很多問題。
如果您使用JDK 1.7或更高版本,它將爲您關閉該文件。
while (moreRecords) {
Employee rec = Main.getData();
try (ObjectOutputStream dos =
new ObjectOutputStream(
new FileOutputStream(file))) {
dos.writeObject(rec);
}
System.out.printf("Add more records [true/false]? ");
moreRecords = scanner.nextBoolean();
}
如果你正在使用JDK 1.6或JDK 1.5:
while (moreRecords) {
Employee rec = Main.getData();
ObjectOutputStream dos = null;
try {
dos = new ObjectOutputStream(
new FileOutputStream(file));
dos.writeObject(rec);
} finally {
if (dos!=null ) {
dos.close();
}
}
System.out.printf("Add more records [true/false]? ");
moreRecords = scanner.nextBoolean();
}
而且,你的程序應該做的用戶輸入更多的驗證。掃描儀可以做到這一點,如下所示:
public static class Employee implements Serializable {
private int id;
private String name;
private BigDecimal salary;
public Employee(int id, String name, BigDecimal salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
}
static Employee getData(Scanner scanner) throws IOException {
System.out.printf("Enter Employee ID : ");
while (!scanner.hasNextInt()) {
System.out.println("Employee IDs are numbers only");
scanner.next();
}
int employeeId = scanner.nextInt();
System.out.printf("Enter Employee Name : ");
String name = scanner.next();
System.out.printf("Enter Employee Salary : ");
while (!scanner.hasNextBigDecimal()) {
System.out.println("Employee salaries are decimals " +
"not random gak");
scanner.next();
}
BigDecimal salary = scanner.nextBigDecimal();
return new Employee(employeeId, name, salary);
}
public static void main(String []args) throws Exception {
boolean moreRecords = true;
String path = null;
Scanner scanner = new Scanner(System.in);
File file = null;
System.out.printf("Enter path to save your file : ");
path = scanner.next();
file = new File(path);
while (moreRecords) {
Employee rec = Main.getData(scanner);
try (ObjectOutputStream dos =
new ObjectOutputStream(
new FileOutputStream(file))) {
dos.writeObject(rec);
}
System.out.printf("Add more records [true/false]? ");
moreRecords = scanner.nextBoolean();
現在輸入/輸出更像是這樣的:
Enter path to save your file : asdfasdf
Enter Employee ID : 9a
Employee IDs are numbers only
99
Enter Employee Name : Rick
Enter Employee Salary : aa
Employee salaries are decimals not random gak
99.99
Add more records [true/false]? false
掃描器迫使終端用戶在正確類型的數據輸入。 你可以結合它與正則表達式來匹配名稱等模式。
我擴展了這個例子,並添加了一些關於掃描器的討論。
http://rick-hightower.blogspot.com/2013/10/java-scanner-example.html
你問'添加更多的記錄[真/假]'所以對於什麼是你進入的迴應? – Maximin
請參閱@Adaam在他自己的問題中發佈的評論。在這裏我發現我的情況與環境問題無關。 – iammurtaza
對不起'添加更多記錄[true/false]?'這是我實際上想要編寫的程序的一部分,但採取's.nextBoolean()'也失敗了,所以我嘗試了's.nextInt()' – iammurtaza