我完成了我的大部分项目,但我还有一个。我必须从用户那里获取数据并将其放入3个变量(profit2012
,profit2013
,profit2014
)中,然后将它们存储在以逗号分隔的文本文件中。之后我想把它们从文件中取出来在图形上绘制它们(Canvas)。我怎么能这样做?
private static int year() {
Scanner input = new Scanner(System.in);
int selectedYear;
System.out.println("Choose one of the following years: ");
System.out.println("[1] 2012 - - [2] 2013 - - [3] 2014");
selectedYear = input.nextInt();
if (selectedYear == 1) {
int Expenses2012;
System.out.println("Please enter Expenses for this year : ");
Expenses2012 = input.nextInt();
int Sales2012;
System.out.println("Please enter Sales for this year : ");
Sales2012 = input.nextInt();
int profit2012 = Sales2012 - Expenses2012;
}
if (selectedYear == 2) {
int Expenses2013;
System.out.println("Please enter Expenses for this year : ");
Expenses2013 = input.nextInt();
int Sales2013;
System.out.println("Please enter Sales for this year : ");
Sales2013 = input.nextInt();
int profit2013 = Sales2013 - Expenses2013;
}
if (selectedYear == 3) {
int Expenses2014;
System.out.println("Please enter Expenses for this year : ");
Expenses2014 = input.nextInt();
int Sales2014;
System.out.println("Please enter Sales for this year : ");
Sales2014 = input.nextInt();
int profit2014 = Sales2014 - Expenses2014;
}try {
main(null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
如你所见,我有变量,图已准备就绪,我只是不知道如何将它们放入文本文件然后读入数组以将它们用作3个矩形的高度(将它们分开放置逗号)。
答案 0 :(得分:0)
使用ObjectOutputStream
并将您的集合编写为文件对象。稍后您将通过ObjectInputStream