**我为一台简单的ATM机编写了一个程序。代码正在生成 Exception :: java.lang.UnsupportedOperationException:尚不支持。 请让我摆脱这个问题。我们也不能使用exists()函数和FileOutputStream吗?
import java.io.*;
import java.util.Scanner;
class Customer1 implements Serializable
{
private static final int serialVersionUID=100;
int i;
transient Scanner s=new Scanner(System.in);
int acc[]=new int[5];
int pin[]=new int[5];
double balance[]=new double[5];
void getdata(int i)
{
System.out.println("Enter account number: ");
acc[i]=s.nextInt();
System.out.println("Enter pin: ");
pin[i]=s.nextInt();
System.out.println("Enter balance: ");
balance[i]=s.nextDouble();
}
void showdata(int i)
{
System.out.println("Account number is: "+acc[i]);
System.out.println("Pin is: "+pin[i]);
System.out.println("Balance is: "+balance[i]);
}
}
class WriteFile1 implements Serializable
{
public static final int serialVersionUID=100;
void filewrite()
{
try
{
Customer1 c=new Customer1();
boolean f;
File fo=new File("ATM.txt");
FileOutputStream fout=new FileOutputStream(fo);
if(fo.exists())
{
System.out.println(" ATM MACHINE");
System.out.println("Enter your ATM pin:");
String pass=s.nextLine();
for(int i=0;i<2;i++)
{
if(pass.equals(c.pin))
{
try
{
FileInputStream fin=new FileInputStream("ATM.txt");
ObjectInputStream objin=new ObjectInputStream(fin);
Customer1 c1=(Customer1)objin.readObject();
c1.showdata(i);
fin.close();
}
catch(Exception e)
{
System.out.println("Exception:: "+e.getMessage());
}
}
}
}
else
{
f=fo.createNewFile();
}
ObjectOutputStream objout=new ObjectOutputStream(fout);
for(int i=0;i<2;i++)
{
c.getdata(i);
}
objout.writeObject(c);
fout.close();
objout.close();
}
catch(Exception e)
{
System.out.println("Exception :: "+e);
}
}
}
public class project
{
void mainmenu()
{
System.out.println("1. Withdraw Cash.");
System.out.println("2. Deposit Cash.");
System.out.println("3. Generate Receipt.");
System.out.println("4. Exit.");
}
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
project a=new project();
WriteFile1 wr=new WriteFile1();
wr.filewrite();
}
}
答案 0 :(得分:0)
在WriteFile1类中还添加一个Scanner变量
class WriteFile1 implements Serializable {
transient Scanner s = new Scanner(System.in);
//your other code
}
关于FileOutputStream的问题。如果文件不存在并且无法创建它,它将抛出异常。
来自javadoc: FileNotFoundException - 如果文件存在但是是目录而不是常规文件,则不存在但无法创建,或者由于任何其他原因无法打开