所以基本上我需要制作一个存储 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
如果有更多帮助,我还尝试使用数组列表进行一些尝试
任何输入将不胜感激。