链接列表到文件

时间:2012-07-16 15:46:43

标签: java

所以基本上我需要制作一个存储cd信息的程序,我需要把它写入file.I需要能够:

添加, 编辑或 删除信息

所以我使用了链接列表,但是当写入文件时,内容有点粗制滥造。

另外,每当我想添加更多信息时,它都会覆盖信息。

这就是我到目前为止所做的。

   public class Project1
{
    static String userIn;   //Determines what the user wants to do in the application

    static String[] info = new String[] {};     //Stores album info

    static LinkedList <String> list = new LinkedList<String> ();        //Array that stores the info to file

    static Scanner scan = new Scanner(System.in);                       //Takes input from keyboard

    static String album;                //Takes in the album name

    static String artist;               //Takes in the artist name

    static String date;                 //Takes in the release date of the album

    static  FileWriter fstream;         //Used to write to file

    static BufferedWriter out;          //Used to write to file

    static String file_name ;           //The name of the file is stored 

    static String search ;              //Takes input 

    public void welcome()           //Main menu of the program where user makes selection

            {//Method will be used as a main menu asking the user what he/she would like to do

                System.out.println("*****MAIN MENU*****");
                System.out.println("");
                System.out.println("To add your album to the system please enter the word add");
                System.out.println("");
                System.out.println("To edit a album in your collection please insert the word edit");
                System.out.println("");
                System.out.println("If you would like to delete a album please insert the word delete");
                System.out.println("");
                System.out.println("To search for a particular album by the CD name please insert the word cd");
                System.out.println("");
                System.out.println("To search for your album by artist name insert the word artist");

                userIn = scan.next();       //Takes user input to decide what action to take

            }//Close of welcome method (Serving as main menu)

        public void adding()                //Used to add info to the linked list
            {//Start of adding method
                if(userIn.equals("add"))    //Starts the add method
                {//Start of if statement

                    System.out.println("*****ADD MENU*****");
                    System.out.println("Please enter the name of the artist");
                    artist = scan.next();   //Stores the artist name
                    System.out.println("Please enter the album name");
                    album = scan.next();    //Stores the album name
                    System.out.println("Please enter the release date");
                    date = scan.next();     //Stores the release date

                    list.add(artist);       //Adds the artist variable to file
                    list.add(album);        //Adds album variable to file
                    list.add(date);         //Adds the date variable to file

                    System.out.println("Album name,Artist Name,Date of release " + list);   //Displays the linked list

                    System.out.println();   //Prints Blank Line

                    file_name = "output.txt";       //File where info is stored to

                     try 
                    {//Start of try and catch method to catch fileNotFound Exception

                         fstream = new FileWriter(file_name);       //Creates an instance of the fileWriter class

                         out = new BufferedWriter(fstream);         //Creates instance of the BufferedWriter class

                         out.write(artist);                 //Adds artist variable to file

                         out.write(album);                  //Adds album variable to file

                         out.write(date);                   //Adds date variable to file

                         out.close();                       //Closes the input stream
                    } 
                     catch (IOException e)                  //Catches FileNotFoundException
                    {
                        e.printStackTrace();

                    }//End of try/catch

                }//End of if statement              

            }//End of adding method

                public void edit()
                    {//Starts the edit method
                        if(userIn.equals("edit"))
                        {//Start of if statement which searches for the album through a matching word

                            System.out.println("*****EDIT MENU*****");

                            System.out.println("Please enter the name you would like to edit");

                            search = scan.next();       //Takes input to match files that need to be edited

                            if(search.equals(album))    //Matches the name the user wants edited to the name in the list
                            {//Start the if statement that 

                                System.out.println(list);       //Displays the list containing the info

                                list.removeAll(list);           //Removes all the input to allow user to edit

                                System.out.println("Please enter new name of the artist ");     
                                artist = scan.next();   //Allows user to add a new artist
                                System.out.println("Please enter new album name ");
                                album = scan.next();    //Allows user to add a new album
                                System.out.println("Please enter new release date");
                                date = scan.next();     //Allows user to add a new release date


                                list.add("," + artist);     //Adds the artist variable to the linked list
                                list.add("," + album);      //Adds the album variable to the linked list
                                list.add( date);            //Adds the date to the variable to the linked list

                                System.out.println("Album name,Artist Name,Release Date" + list);       //Displays the Linked List

                                  try 
                                    {//Start of try and catch method to catch fileNotFound Exception

                                         fstream = new FileWriter(file_name);       //Creates an instance of the fileWriter class

                                         out = new BufferedWriter(fstream);         //Creates instance of the BufferedWriter class

                                         out.write(artist);                 //Adds artist variable to file

                                         out.write(album);                  //Adds album variable to file

                                         out.write(date);                   //Adds date variable to file

                                         out.close();                       //Closes the input stream
                                    } 
                                     catch (IOException e)                  //Catches FileNotFoundException
                                    {
                                        e.printStackTrace();


                                    try 
                                    {//Start
                                        out.close();        //Closes the stream 
                                    }//End 
                                    catch (IOException z) 
                                    {//Start of catch statement
                                    z.printStackTrace();
                                    }//End of try catch

                                    }

                                    }//End of if statement
                                    else
                                    {//Start of 

                                    System.out.println("Album not found");      //Displays if the user input could not be linked to info in the linked list

                                }//End of if else statement

                        }//End of if statement     

                }//End of method

                public void delete()        //Used to delete info from the linked list
                {//Start of delete method
                    if(userIn.equals("delete"))
                    {//Matches user input to verify if correct method

                        System.out.println("*****DELETE MENU*****");

                        System.out.println("Please enter the name to delete");

                        search = scan.next();   //Takes in the name to match with linked list   

                        System.out.println("To continue insert 1");

                        int uSure = scan.nextInt(); //Takes input to verify if deletion is desired

                        if(uSure==1)
                        {//Starts 

                            System.out.println("Please enter the name of the album you would like to delete");  //Displays the file contents to the user
                            list.removeAll(list);       //Deletes the contents of the list

                        }//End of second if statement

                        if(search.equals(album))
                        {//Searches for info in linked list according to user input.
                            System.out.println(list);
                            list.remove();  //Removes info from the linked lists
                            System.out.println("Album deleted");

                          try 
                            {//Start of try and catch method to catch fileNotFound Exception

                                 fstream = new FileWriter(file_name);       //Creates an instance of the fileWriter class

                                 out = new BufferedWriter(fstream);         //Creates instance of the BufferedWriter class

                                 out.write(artist);                 //Adds artist variable to file

                                 out.write(album);                  //Adds album variable to file

                                 out.write(date);                   //Adds date variable to file

                                 out.close();                       //Closes the input stream
                            } //end of try 
                             catch (IOException e)                  //Catches FileNotFoundException
                            {
                                e.printStackTrace();


                            }//End of try/catch
                    }
                    else
                    {
                        System.out.println("Album not found");
                    }//End of if statement

                }//End of first if statement

            }//End of method    


            public void search()
            {//Start of search method

                if(userIn.equals("search"))                     
                {//Matches user input to verify if correct method
                    System.out.println("*****SEARCH MENU*****");

                    System.out.println();               //Prints out a blank line

                    System.out.println("Please insert the name of the album");

                    search = scan.next();               //Takes user input

                    if(album.equals(search))            //Matches user input to info in linked list
                    {
                    System.out.println(list);       //Matches user search to info in linked list
                    }
                    else
                    {//Ends the if statement

                    System.out.println("Album name could not be found");  //Displayed if user input could not be matched with info in the linked list

                }//End of second if statement

              }//End of first if statement

         }//End of search method

            public static void main(String [] args) throws IOException 
            {//Starts the main method which runs the application

            Project1 cd = new Project1();       //Making an instance of the class

            cd.welcome();   //Runs the welcome method
            cd.adding();    //Runs the adding method
            cd.edit();      //Runs the edit method
            cd.delete();    //Runs the edit method
            cd.search();    //Runs the search method

        }//End Of Main method

}//End Of Class

如果有更多的帮助,我还尝试使用数组列表

任何意见都会受到赞赏。

4 个答案:

答案 0 :(得分:1)

如果文件不需要人工编辑或有性能限制,您可以使用Java's serialization将数组写入文件。

答案 1 :(得分:1)

如果要编辑或删除,则必须覆盖所有内容。您只添加一个条目。添加,编辑或删除条目后,只需一个方法将LinkedList的所有内容重写到文件中。

答案 2 :(得分:0)

1。如果相同的java程序写入和读取的信息,则使用序列化

2. 否则请使用文件。

第3        您需要附加数据而不是将其覆盖在文件中。

  File f = new File("d:\\viv.txt");
  FileWriter fw = new FileWriter(f, true);  // true is to enable appending
  BufferedWriter bw = new BufferedWriter(fw);

  bw.append("hello");

答案 3 :(得分:0)

除了已经提到的文件之外,你还有几个问题。这些字符串通过引用添加到列表中。更改字符串时,您也会在列表中更改它。将它们添加到列表中时,它们需要是字符串的新实例。

第二,我会创建一个类来封装专辑的数据。这些字符串都不应该是静态的。然后将Album类的(NEW)实例添加到列表中。然后,覆盖Album类中的ToString()方法,以您需要的方式打印信息。

此外,如果您总是附加到列表,则没有真正的理由在此处使用链接列表,基于普通数组的列表将是正常的(特别是如果您需要快速访问每个元素)。搜索在链表上将是大哦(n),它们在基于数组的列表上是常量。您需要链接列表的唯一时间是您需要在列表末尾以外的位置进行高效插入,并且您将经常重新分配大小。并非所有这些在现代计算机和小型应用程序中都非常重要。