Java 当我添加一个播放器时,它会删除文本文件中的所有内容,我如何修复这个问题?

Java 当我添加一个播放器时,它会删除文本文件中的所有内容,我如何修复这个问题?,java,Java,滚动到底部查看添加播放器代码,它会删除文本文件中的所有内容,而不是添加。请给我一个人掌舵,因为我的高中项目明天就要交了。非常感谢 import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.*; import java.util.Scanner; import java.io.PrintWriter; /** * Soccer Database, can search

滚动到底部查看添加播放器代码,它会删除文本文件中的所有内容,而不是添加。请给我一个人掌舵,因为我的高中项目明天就要交了。非常感谢

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.*;
import java.util.Scanner;
import java.io.PrintWriter;
/**
 * Soccer Database, can search statistics of players with several different options. 
 * 
 * @author (Sachin Khargie) 
 * @version (10/1/2014)
 */
public class FinalProject3
{
    static Scanner sc= new Scanner(System.in);
    static Scanner sc2= new Scanner(System.in);    
    static String names[] = {"Angel Di Maria", "Neymar Da Silva Santos", "Leo Messi", "Chrstiano Ronaldo", "Eden Hazard", "Diego Costa", "Benzema", "David De Gea", "Wayne Rooney", "Radamel Falcao", "Sachin Khargie", "Shawn Heaton",};
    static double salary[] = {21000000, 20000000, 22000000, 22000000, 19000000, 18000000, 20000000, 15000000, 16000000, 17000000, 40000000, 40000000};
    static String players[] = {"Angel Di Maria", "Neymar Da Silva Santos", "Leo Messi", "Chrstiano Ronaldo", "Eden Hazard", "Diego Costa", "Benzema", "David De Gea", "Wayne Rooney", "Radamel Falcao", "Sachin Khargie", "Shawn Heaton",};
    static double ratings[] = {86, 86, 93, 92, 88, 85, 85, 85, 86, 88, 95, 95};
    public static void main (String args[]) throws IOException
    {

        BufferedReader input = new BufferedReader (new FileReader ("soccerdata.txt"));
        int menu;
        System.out.println("Welcome to the Soccer Player Database."); 
        while (true)
        {
            System.out.println("1. Search for the players salary by name (Press 1)"); 
            System.out.println("2. Search for the Players club (press 2)");
            System.out.println("3. Search for the Players postion (press 3)");
            System.out.println("4. Enter club to find out the players that play for that club (press 4)");
            System.out.println("5. This option will output the salaries of the players then sorts it from lowest to greatest(press 5)");
            System.out.println("6. Enter the players name to find out their rating (press 6)");
            System.out.println("7. This option will output the ratings of the players then sorts it from lowest to greatest (press 7)");
            System.out.println("8. Add a player to the database"); 
            menu = sc.nextInt();


            if (menu == 1)
                nametoSalary();
            else if (menu == 2)
                nametoClub();
            else if (menu == 3)
                nametoPostion();
            else if (menu == 4)
                clubtoPlayer();
            else if (menu == 5)
                sortbySalary();
            else if (menu == 6)
                nametoRating();
            else if (menu == 7)
                sortbyRating();
            else if (menu == 8)
                addPlayer();








    }

}
public static void nametoSalary () throws IOException
    {
        BufferedReader i = new BufferedReader (new FileReader ("soccerdata.txt"));
        String data=i.readLine();

        String player="";


        System.out.println("Enter the Name of the player:");
        player = sc2.nextLine();


        String salary="";
        boolean found=false;
        while (data!=null)
        {
            String database[]=data.split(",");
            data=i.readLine();
            if (data!=null)
            {
                for (int x=0;x<data.length(); ++x)
                {
                    if (database[0].equalsIgnoreCase(player)) 
                    {
                        salary=database[1];
                        found=true;
                        break;
                    }
                }
            }
        }

        if (found==true)
        {
            System.out.println();
            System.out.println("The salary of that player is " + "" + salary);
            System.out.println();
        }   

        else if (found==false)
        {
            System.out.println();
            System.out.println("That player does not exist in the database");
            System.out.println(); 
        }
    }
public static void nametoClub () throws IOException
    {
         BufferedReader i = new BufferedReader (new FileReader ("soccerdata.txt"));
         String data=i.readLine();

         String player="";

         System.out.println("Enter the Name of the player to find out what club they play for:");
         player = sc2.nextLine();
         String club="";
        boolean found=false;
        while (data!=null)
        {
            String database[]=data.split(",");
            data=i.readLine();
            if (data!=null)
            {
                for (int x=0;x<data.length(); ++x)
                {
                    if (database[0].equalsIgnoreCase(player)) 
                    {
                        club=database[2];
                        found=true;
                        break;
                    }
                }
            }
        }
         if (found==true)
        {
            System.out.println();
            System.out.println("The club of that player is " + "" + club);
            System.out.println();
        }   

        else if (found==false)
        {
            System.out.println();
            System.out.println("That player does not exist in the database");
            System.out.println(); 
        }
    }
public static void nametoPostion () throws IOException
    {
         BufferedReader i = new BufferedReader (new FileReader ("soccerdata.txt"));
         String data=i.readLine();

         String player="";

         System.out.println("Enter the Name of the player to find out what postion they play for:");
         player = sc2.nextLine();
         String postion="";
        boolean found=false;
        while (data!=null)
        {
            String database[]=data.split(",");
            data=i.readLine();
            if (data!=null)
            {
                for (int x=0;x<data.length(); ++x)
                {
                    if (database[0].equalsIgnoreCase(player)) 
                    {
                        postion=database[3];
                        found=true;
                        break;
                    }
                }
            }
        }
         if (found==true)
        {
            System.out.println();
            System.out.println("The postion of that player is " + "" + postion);
            System.out.println();
        }   

        else if (found==false)
        {
            System.out.println();
            System.out.println("That postion does not exist in the database");
            System.out.println(); 
        }
    }
public static void clubtoPlayer () throws IOException
    {
         BufferedReader i = new BufferedReader (new FileReader ("soccerdata.txt"));
         String data=i.readLine();

         String club="";

         System.out.println("Enter the Name of the club to find out what players are playing for that club in the current list");
         club= sc2.nextLine();
         String player="";
        boolean found=false;
        while (data!=null)
        {
            String database[]=data.split(",");
            data=i.readLine();
            if (data!=null)
            {
                for (int x=0;x<data.length(); ++x)
                {
                    if (database[0].equalsIgnoreCase(club)) 
                    {
                        player=database[1];
                        found=true;
                        break;
                    }
                }
            }
        }
         if (found==true)
        {
            System.out.println();
            System.out.println("The players that play for that club are " + "" + player);
            System.out.println();
        }   

        else if (found==false)
        {
            System.out.println();
            System.out.println("That club does not exist in the database");
            System.out.println(); 
        }
    }
public static void sortbySalary()
{
       System.out.println("-------------------------------");
       System.out.println("Original List");
       System.out.println("-------------------------------");
       printThese (names, salary);
       System.out.println("-------------------------------");
       System.out.println("Sorted List");
       sortThese(names,salary);
       System.out.println("-------------------------------");
       printThese (names,salary);
       System.out.println("-------------------------------");
}
public static void printThese(String n[], double m[])
        {
         for (int x=0;x<n.length;++x)
         {
           System.out.println(n[x] + "  " + m[x]);
           }

        }
public static void sortThese(String n[],double m[])
        {
            for (int x=0;x<n.length;++x)
            {
                for (int y=0;y<n.length-1;++y)
                {
                    if (m[y]> m[y+1])
                    {
                        String temp=n[y];
                        n[y]=n[y+1];
                        n[y+1]=temp;

                        double temp2=m[y];
                        m[y]=m[y+1];
                        m[y+1]=temp2;
                       }
                   }


           }


        }
public static void nametoRating () throws IOException
    {
         BufferedReader i = new BufferedReader (new FileReader ("soccerdata.txt"));
         String data=i.readLine();

         String player="";

         System.out.println("Enter the Name of the player to find out there rating");
         player= sc2.nextLine();
         String rate="";
        boolean found=false;
        while (data!=null)
        {
            String database[]=data.split(",");
            data=i.readLine();
            if (data!=null)
            {
                for (int x=0;x<data.length(); ++x)
                {
                    if (database[0].equalsIgnoreCase(player)) 
                    {
                        rate=database[4];
                        found=true;
                        break;
                    }
                }
            }
        }
         if (found==true)
        {
            System.out.println();
            System.out.println("The players rating is " + "" + rate);
            System.out.println();
        }   

        else if (found==false)
        {
            System.out.println();
            System.out.println("That player does not exist in the database or the rating has not been entered into the database");
            System.out.println(); 
        }
    }
public static void sortbyRating()
        {
       System.out.println("-------------------------------");
       System.out.println("Original list");
       System.out.println("-------------------------------");
       printThese (players, ratings);
       sortThese(players,ratings);
       System.out.println("-------------------------------");
       System.out.println("Sorted List");
       System.out.println("-------------------------------");
       printThese (players,ratings);
       System.out.println("-------------------------------");
        }
public static void outputThese(String n[], double m[])
        {
         for (int x=0;x<n.length;++x)
         {
           System.out.println(n[x] + "  " + m[x]);
           }

        }
public static void sortThem(String n[],double m[])
        {
            for (int x=0;x<n.length;++x)
            {
                for (int y=0;y<n.length-1;++y)
                {
                    if (m[y]> m[y+1])
                    {
                        String temp=n[y];
                        n[y]=n[y+1];
                        n[y+1]=temp;

                        double temp2=m[y];
                        m[y]=m[y+1];
                        m[y+1]=temp2;
                       }
                   }


           }


        }
public static void addPlayer() throws IOException
    {
        BufferedReader input = new BufferedReader (new FileReader ("soccerdata.txt"));
        String data=input.readLine();

        PrintWriter newPlayer= new PrintWriter(new FileWriter("soccerdata.txt"));

        String add="";

        String i[]=new String [5];

        System.out.println("Enter Players Name");
        add = sc.next();
        newPlayer.println(add + ",");

        System.out.println("Enter Players Salary");
        add = sc.next();
        newPlayer.println(add + ",");

        System.out.println("Enter Players Club ");
        add = sc.next();
        newPlayer.println(add + ",");

        System.out.println("Enter Players Postion");
        add = sc.next();
        newPlayer.println(add + ",");

        System.out.println("Enter Players Rating");
        add = sc.next();
        newPlayer.println(add + ",");

        System.out.println(" ");
        System.out.println("Player has been added to the database.");
        System.out.println(" ");
}
}
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileWriter;
导入java.io.*;
导入java.util.Scanner;
导入java.io.PrintWriter;
/**
*足球数据库,可以搜索具有多个不同选项的球员的统计信息。
* 
*@作者(Sachin Khargie)
*@版本(10/1/2014)
*/
公共类最终项目3
{
静态扫描仪sc=新扫描仪(System.in);
静态扫描仪sc2=新扫描仪(System.in);
静态字符串名称[]={“安吉尔·迪·玛丽亚”、“内马尔·达席尔瓦·桑托斯”、“利奥·梅西”、“克里斯蒂亚诺·罗纳尔多”、“伊登·哈扎德”、“迭戈·科斯塔”、“本泽马”、“大卫·德赫亚”、“韦恩·鲁尼”、“拉达梅尔·法尔考”、“萨钦·哈吉”、“肖恩·希顿”};
静态双薪[]={21000000、20000000、22000000、22000000、19000000、18000000、20000000、15000000、16000000、17000000、40000000、40000000};
静态弦乐演奏者[]={“安吉尔·迪玛丽亚”、“内马尔·达席尔瓦·桑托斯”、“利奥·梅西”、“克里斯蒂亚诺·罗纳尔多”、“伊登·哈扎德”、“迭戈·科斯塔”、“本泽马”、“大卫·德赫亚”、“韦恩·鲁尼”、“拉达梅尔·法尔考”、“萨钦·哈吉”、“肖恩·希顿”};
静态双重评级[]={86,86,93,92,88,85,85,85,86,88,95,95};
公共静态void main(字符串args[])引发IOException
{
BufferedReader输入=新的BufferedReader(新文件读取器(“soccerdata.txt”);
int菜单;
System.out.println(“欢迎来到足球运动员数据库”);
while(true)
{
System.out.println(“1.按姓名搜索球员工资(按1)”;
System.out.println(“2.搜索球员俱乐部(按2)”;
System.out.println(“3.搜索玩家位置(按3)”;
System.out.println(“4.进入俱乐部查找为该俱乐部踢球的球员(按4)”;
System.out.println(“5.此选项将输出球员的工资,然后将其从最低到最高排序(按5)”;
System.out.println(“6.输入玩家姓名以了解他们的等级(按6)”;
System.out.println(“7.此选项将输出玩家的评级,然后将其从最低到最高排序(按7)”;
System.out.println(“8.向数据库添加播放器”);
menu=sc.nextInt();
如果(菜单==1)
nametoSalary();
否则如果(菜单==2)
nametoClub();
否则如果(菜单==3)
nametoPostion();
else if(菜单==4)
clubtoPlayer();
否则如果(菜单==5)
sortbySalary();
否则如果(菜单==6)
命名();
否则如果(菜单==7)
sortbyRating();
否则如果(菜单==8)
addPlayer();
}
}
公共静态void nametoSalary()引发IOException
{
BufferedReader i=新的BufferedReader(新文件读取器(“soccerdata.txt”);
字符串数据=i.readLine();
弦乐演奏家=”;
System.out.println(“输入玩家名称:”);
player=sc2.nextLine();
字符串salary=“”;
布尔值=false;
while(数据!=null)
{
字符串数据库[]=data.split(“,”);
数据=i.readLine();
如果(数据!=null)
{

对于(int x=0;x,您的FileWriter将覆盖文件而不是添加到其中

若要追加,请在构造函数中指定so

即:

此外,直到您刷新磁盘,才真正写入磁盘,因此在PrintWriter上调用flush(),或者在构建它时,指定希望它自动刷新,如下所示:

new PrintWriter(fileWriter,true);

首先,在
addPlayer
方法中,您在覆盖模式下创建了
FileWriter
,默认情况下会发生这种情况。其次,您不会关闭
PrintWriter

用于打开附加模式,并关闭
PrintWriter

您还应该关闭方法末尾的
BufferedReader


如果您使用的是Java 7或更高版本,您可以使用在完成后自动关闭
BufferedReader
PrintWriter

您能给我们一个文本文件的示例吗?例如,文件soccerdata.txt的结构如何?请只发布与您的问题相关的代码。如果问题在一种方法中od,那么请只发布该方法的代码。给我发电子邮件,我会把文本文件发给你
new PrintWriter(fileWriter,true);