Java的如何一个ArrayList保存到一个文件 [英] Java how to save An arrayList to a file
问题描述
您好,我有这个code和它要求用户输入的视频和客户ID,并将其添加到一个数组列表,称为员工如何将我保存的ArrayList到一个文件中,当完成并退出该程序?
进口的java.util.ArrayList;
进口java.util.Scanner中;
公共类驱动器{
公共静态最终诠释LISTCUSTOMERS = 1;
公共静态最终诠释LISTVIDEOS = 2;
公共静态最终诠释LISTHIRES = 3;
公共静态最终诠释HIRESTATISTICS = 4;
公共静态最终诠释HIREVIDEO = 5;
公共静态最终诠释RETURNVIDEO = 6;
公共静态最终诠释QUIT = 7;
公共静态最终整数默认= 0;
私有String [] menuText;
私有静态最终诠释MAXVIDEOS = 9;
私有静态最终诠释MAXCUSTOMERS = 5;
静态扫描仪SC =新的扫描仪(System.in);
私人诠释的选择;
公众诠释MAXLENGTH = 50;
公共静态的ArrayList<&租用GT; HireList =新的ArrayList<&租用GT;();
公共静态视频[] =视频新视频[MAXVIDEOS]
公共静态客户[] =客户新客户[MAXCUSTOMERS]
公共静态的String []选项= {,客户名单,列表影片,列表聘用,服务统计,服务视频,返回视频,退出};
公共静态驱动MYMENU =新的驱动程序(选项,SC);
公共静态布尔多=真;
公共静态INT选项= 0;
静态客户的客户; 公共静态无效的主要(字串[] args){
initilisation();
runHireVideo();
}
公共驱动程序(的String [] menuText,扫描仪SC){
this.menuText = menuText;
}
公众诠释getMenuSelection(){
this.displayMenu();
this.selection = sc.nextInt();
如果(this.selection<默认|| this.menuText.length< this.selection){
的System.out.println(您的选择是无效的 - 使用默认);
this.selection =默认值;
}
返回this.selection;
}
公共无效displayMenu(){
System.out.print(\\ nMicroVideo菜单:\\ n);
对于(INT指数= 1;指数< menuText.length;指数++)
的System.out.println(索引+ + menuText [索引]。);
System.out.print(输入选项:);
}
公共静态无效runHireVideo(){ 而(更多){
选项= myMenu.getMenuSelection();
开关(选配件){
案例LISTCUSTOMERS:
ListCustomers();
打破;
案例LISTVIDEOS:
ListVideos();
打破;
案例LISTHIRES:
ListHires();
打破;
案例HIRESTATISTICS:
HireStatistics();
打破;
案例HIREVIDEO:
HireVideo();
打破;
案例RETURNVIDEO:
ReturnVideo();
打破;
默认:
更多= FALSE;
的System.out.println(程序已退出);
打破;
}
}
}
公共静态无效initilisation(){
视频[0] =新的视频(140,黑客帝国,7.50,3);
视频[1] =新的视频(141,终结者2,5.00,3);
视频[2] =新的视频(142,怪物史莱克,5.00,10);
视频[3] =新的视频(143,城堡,5.00,1);
视频[4] =新的视频(146,音乐之声,1.00,23);
视频[5] =新的视频(147,行星人猿,5.00,0);
视频[6] =新的视频(148,碟中谍,1.00,15);
视频[7] =新的视频(150,巴格达之夜,6.00,5);
视频[8] =新的视频(151,指环王1的主,5.00,0); 客户[0] =新客户(9902JI,英尼斯,0,43484001);
客户[1] =新客户(8906RH,赫伯特,0,43484000);
客户[2] =新客户(9012GT,特纳,0,43480009);
客户[3] =新的客户(9012GS,斯帕克,0,43480007);
客户[4] =新的客户(9012MV,瓦兰斯,0,43480008);
}
公共静态无效ListCustomers(){
System.out.printf(% - 6S%-5S%6S \\ n,客户ID,客户名称,客户电话号码);
的for(int i = 0; I< customers.length;我++){
的System.out.println(客户[I]);
}
}
公共静态无效ListVideos(){
System.out.printf(% - 8S%-17s%6S%6S \\ n,视频ID,VideoName,视频价格率,库存);
的for(int i = 0; I< videos.length;我++){
的System.out.println(视频[I]);
}
}
公共静态无效ListHires(){
System.out.printf(%s%s%S%s%S \\ n,HireID,卡斯特名,视频名称,天聘用,服务成本);
对于(租用出租:HireList){
串客户=客户[hire.getID(hire.getVideoID())] getCustomerName();
字符串VIDEOID =视频[hire.getID(hire.getVideoID())]的getTitle()。
双DailyRate =影片[hire.getID(hire.getVideoID())]的getRate()* hire.getDaysHired();
System.out.printf(%D%s%S%D%1.2F \\ n,hire.getHireID(),客户,VIDEOID,hire.getDaysHired(),DailyRate);
}
}
公共静态无效HireStatistics(){
System.out.printf(%s%s%S \\ n,视频ID,视频名称,天聘请);
对于(租用出租:HireList){
字符串VIDEOID =视频[hire.getID(hire.getVideoID())]的getTitle()。
System.out.printf(%s%S%d个\\ N,hire.getVideoID(),VIDEOID,hire.getDaysHired());
}
}
公共静态无效HireVideo(){
诠释计数= 0;
而(计数== 0){
INT hireID = 1000 + HireList.size()+ 1;
的System.out.println(输入客户ID);
字符串的customerID =9902JI;
的System.out.println(输入视频ID);
字符串VIDEOID =140;
的System.out.println(输入天聘用);
INT daysHired = 4;
的System.out.println(输入另一个租用[Y / N]?);
String是= sc.next();
如果(Yes.equals(Y)){
计数= 0;
}
否则如果(Yes.equals(N)){
数= 1;
}
HireList.add(新员工(hireID,的customerID,VIDEOID,daysHired,0));
} }
公共静态无效ReturnVideo(){
的System.out.println(输入客户ID);
字符串的customerID =9902JI;
的System.out.println(输入视频ID);
字符串VIDEOID =140; 的for(int i = 0; I< HireList.size();我++){
如果(HireList.get(ⅰ).getCustomerID()。等于(的customerID)){
HireList.remove(ⅰ);
}
}
为(中间体二= 0; II蛋白酶HireList.size();ⅱ++){
如果(HireList.get(II).getVideoID()。等于(VIDEOID)){
HireList.remove(二);
}
}
的System.out.println(已删除+的customerID);
} }
最简单的方法,如前面提到的,那就是只要给每个成员保存在一个文本文件,并与扫描仪的对象(的 http://docs.oracle.com/javase/7/docs/api /java/util/Scanner.html )。
如果你是关心如何写入一个文本文件,我建议阅读这篇文章:的 http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/
这也解释了如何使用FileWriter对象。
另一种选择是让驱动程序实现serialisable接口,这样可以让你存储整个驱动程序对象在一系列字节,但这是稍微复杂一些。系列化上一个体面的教程可以在这里找到:
http://www.tutorialspoint.com/java/java_serialization.htm
hello i have this code and it asks the user to enter a video and customer id and adds it to an array list called hires how would i save that arraylist to a file when the are finished and quit this program?
import java.util.ArrayList;
import java.util.Scanner;
public class Driver {
public static final int LISTCUSTOMERS = 1;
public static final int LISTVIDEOS = 2;
public static final int LISTHIRES = 3;
public static final int HIRESTATISTICS = 4;
public static final int HIREVIDEO = 5;
public static final int RETURNVIDEO = 6;
public static final int QUIT = 7;
public final static int DEFAULT = 0;
private String[] menuText;
private static final int MAXVIDEOS = 9;
private static final int MAXCUSTOMERS = 5;
static Scanner sc = new Scanner(System.in);
private int selection;
public int MAXLENGTH = 50;
public static ArrayList<Hire>HireList = new ArrayList<Hire>();
public static Video[]videos= new Video[MAXVIDEOS];
public static Customer [] customers = new Customer[MAXCUSTOMERS];
public static String [] options = {"","List Customers", "List Videos", "List Hires", "Hire Statistics", "Hire Video", "Return Video", "Quit"};
public static Driver myMenu = new Driver (options, sc);
public static boolean more = true;
public static int option = 0;
static Customer customer;
public static void main (String [] args){
initilisation();
runHireVideo();
}
public Driver (String[] menuText, Scanner sc){
this.menuText = menuText;
}
public int getMenuSelection(){
this.displayMenu();
this.selection = sc.nextInt();
if(this.selection < DEFAULT || this.menuText.length<this.selection){
System.out.println("Your selection was invalid - using default");
this.selection = DEFAULT;
}
return this.selection;
}
public void displayMenu(){
System.out.print("\nMicroVideo Menu: \n");
for (int index = 1; index < menuText.length; index++)
System.out.println(index + ". "+ menuText [index]);
System.out.print("Enter option: ");
}
public static void runHireVideo(){
while (more){
option = myMenu.getMenuSelection();
switch(option){
case LISTCUSTOMERS:
ListCustomers();
break;
case LISTVIDEOS:
ListVideos();
break;
case LISTHIRES:
ListHires();
break;
case HIRESTATISTICS:
HireStatistics();
break;
case HIREVIDEO:
HireVideo();
break;
case RETURNVIDEO:
ReturnVideo();
break;
default:
more = false;
System.out.println("Program Exited");
break;
}
}
}
public static void initilisation(){
videos[0]=new Video("140","The Matrix ", 7.50, 3);
videos[1]=new Video("141", "Terminator 2 ", 5.00, 3);
videos[2]=new Video("142", "Shrek ", 5.00, 10);
videos[3]=new Video("143", "The Castle ", 5.00, 1);
videos[4]=new Video("146", "Sound Of Music ", 1.00, 23);
videos[5]=new Video("147", "Planet Of The Apes", 5.00, 0);
videos[6]=new Video("148", "Mission Impossible", 1.00, 15);
videos[7]=new Video("150", "Bagdad by Night", 6.00, 5);
videos[8]=new Video("151", "Lord of the Rings 1", 5.00, 0);
customers [0]= new Customer("9902JI", "Innes ", 0,43484001);
customers [1]= new Customer("8906RH", "Herbert", 0,43484000);
customers [2]= new Customer("9012GT", "Turner ", 0,43480009);
customers [3]= new Customer("9012GS", "Sparke ", 0,43480007);
customers [4]= new Customer("9012MV", "Vallance", 0,43480008);
}
public static void ListCustomers(){
System.out.printf("%-6s %-5s %6s\n", "Customer ID", "Customer Name", " Customer Phone Number");
for (int i=0;i<customers.length;i++){
System.out.println(customers[i]);
}
}
public static void ListVideos(){
System.out.printf("%-8s %-17s %6s %6s\n", "Video ID", "VideoName", " Video Price Rate", " In Stock");
for (int i=0;i<videos.length;i++){
System.out.println(videos[i]);
}
}
public static void ListHires(){
System.out.printf("%s %s %s %s %s \n","HireID", "Cust Name", "Video Name", "Days Hired", "Hire Cost");
for (Hire hire: HireList){
String Customer = customers[hire.getID(hire.getVideoID())].getCustomerName();
String videoID = videos[hire.getID(hire.getVideoID())].getTitle();
double DailyRate = videos[hire.getID(hire.getVideoID())].getRate()*hire.getDaysHired();
System.out.printf("%d %s %s %d %1.2f \n",hire.getHireID(), Customer, videoID, hire.getDaysHired(), DailyRate);
}
}
public static void HireStatistics(){
System.out.printf("%s %s %s\n", "Video ID", "Video Name", "Days Hired");
for (Hire hire: HireList){
String videoID = videos[hire.getID(hire.getVideoID())].getTitle();
System.out.printf("%s %s %d \n",hire.getVideoID(), videoID, hire.getDaysHired());
}
}
public static void HireVideo(){
int count =0;
while (count == 0){
int hireID = 1000+ HireList.size()+1;
System.out.println("Enter Customer ID");
String customerID = "9902JI";
System.out.println("Enter Video ID");
String videoID = "140";
System.out.println("Enter Days Hired");
int daysHired = 4;
System.out.println("Enter Another Hire [Y/N]?");
String Yes = sc.next();
if(Yes.equals("y")){
count = 0;
}
else if (Yes.equals("n")){
count = 1;
}
HireList.add(new Hire(hireID,customerID, videoID, daysHired, 0));
}
}
public static void ReturnVideo(){
System.out.println("Enter Customer ID");
String customerID = "9902JI";
System.out.println("Enter Video ID");
String videoID = "140";
for (int i = 0; i < HireList.size();i++){
if (HireList.get(i).getCustomerID().equals(customerID)){
HireList.remove(i);
}
}
for (int ii = 0; ii < HireList.size();ii++){
if (HireList.get(ii).getVideoID().equals(videoID)){
HireList.remove(ii);
}
}
System.out.println("Removed " + customerID );
}
}
The simplest approach, as mentioned before, would be simply to save each member in a text file and read them back in with a Scanner object (http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html).
If you are concerned with how to write to a text file, I would suggest reading this article: http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/ which explains how to use the FileWriter Object.
An alternative option would be to have Driver implement the serialisable interface which would allow you store the entire Driver object in a series of bytes, but this is slightly more complicated. A decent tutorial on serialisation can be found here: http://www.tutorialspoint.com/java/java_serialization.htm
这篇关于Java的如何一个ArrayList保存到一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!