滚动到底部查看添加播放器代码,它会删除文本文件中的所有内容而不是添加内容。请告诉我某人,因为明天我的高中项目将会到期。非常感谢
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(" ");
}
}
答案 0 :(得分:2)
首先,在addPlayer
方法中,您在覆盖模式下创建了FileWriter
,默认情况下会发生这种情况。其次,您没有关闭PrintWriter
。
使用the FileWriter
constructor that takes a boolean
打开追加模式,然后关闭PrintWriter
。
您还应关闭方法末尾的BufferedReader
。
如果您使用的是Java 7或更高版本,则可以使用"try-with-resources"让BufferedReader
和PrintWriter
在完成后自动关闭。
答案 1 :(得分:0)
您的FileWriter会覆盖该文件而不是添加它。
要追加,请在构造函数
中指定即:
new FileWriter("filename.dat",true);
此外,在刷新之前,实际上并未对磁盘进行写入,因此要么在PrintWriter上调用flush(),要么在构造它时,指定您希望它自动刷新,如下所示:
new PrintWriter(fileWriter,true);