datainputstream と dataoutputstream を使用してユーザーから入力されたデータを出力する必要があります
が、これは入力を適切に取得していません
。コードの何が問題なのか教えてもらえますか?
import java.io.*;
class Employee
{
int id;
String name;
double salary;
}
public class Ch8Ex2
{
public static void main (String[] args)
{
Employee emp = new Employee();
try
{
File f1 = new File("emp1.dat");
f1.createNewFile();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
DataInputStream da = new DataInputStream(new FileInputStream(f1));
DataOutputStream ad = new DataOutputStream(new FileOutputStream(f1));
System.out.println("Enter your ID:");
emp.id = br.read();
System.out.println("Enter your name:");
emp.name = br.readLine();
System.out.println("Enter your salary:");
String str = br.readLine();
emp.salary = Double.parseDouble(str);
ad.write(emp.id);
ad.writeUTF(emp.name);
ad.writeDouble(emp.salary);
ad.flush();
ad.close();
System.out.println("ID:"+da.readInt());
System.out.println("Name:"+da.readUTF());
System.out.println("Salary:"+da.readDouble());
da.close();
}
catch(IOException e)
{
}
catch(NumberFormatException e)
{
}
}
}