-1

基本的に、CD情報を保存するプログラムを作成する必要があり、それをファイルに書き込む必要があります。次のことができる必要があります。

情報の追加、編集、または削除

そのため、Linked List を使用しましたが、ファイルに書き込むと、内容が少し粗雑になります。

さらに、情報を追加するたびに情報が上書きされます。

これは私がこれまでに持っているものです。

   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

編集または削除する場合は、すべての内容を上書きする必要があります。エントリを 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

すでに述べたファイルに加えて、いくつかの問題があります。これらの文字列は、参照によってリストに追加されます。文字列を変更すると、リストでも変更されます。リストに追加するときは、文字列の新しいインスタンスである必要があります。

2 つ目は、アルバムのデータをカプセル化するクラスを作成することです。どの文字列も静的であってはなりません。次に、Album クラスの (NEW) インスタンスをリストに追加します。次に、Album クラス内の ToString() メソッドをオーバーライドして、必要な方法で情報を出力します。

また、常にリストに追加する場合は、ここでリンクされたリストを使用する本当の理由はありません。単純な配列ベースのリストで問題ありません (特に各要素にすばやくアクセスする必要がある場合)。検索は、リンクされたリストでは big-Oh(n) になり、配列ベースのリストでは一定です。リンクされたリストが必要になるのは、リストの末尾以外の場所に効率的な挿入が必要な場合と、サイズを頻繁に再割り当てする場合だけです。最新のコンピューターや小さなアプリケーションでは、そのすべてが実際に重要というわけではありません。

于 2012-07-16T16:03:22.703 に答える