-1

所以基本上我需要制作一个存储 cd 信息的程序,我需要将它写入文件。我需要能够:

添加、编辑或删除信息

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

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

这就是我迄今为止所拥有的。

   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

4 回答 4

1

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

于 2012-07-16T15:49:58.667 回答
1

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

于 2012-07-16T15:53:23.013 回答
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");
于 2012-07-16T16:00:40.277 回答
0

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

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

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

于 2012-07-16T16:03:22.703 回答