2017-09-27 12 views
0

自分のコードでプレーヤーを編集して、作成したCSVファイルの値を更新しようとしていますが、新しい値を書き込む方法がわかりませんファイルそのもの。私は何をしたいのか分かっていますが、それをどうやってやるのかは分かりません。私は支払った金額を編集していて、プレーヤーのファイルで更新しようとしています。ここでCSVファイルをJavaの新しい値で上書きする方法

私のクラスは次のとおりです。ここ メインクラス

package squashapplication; 

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.ArrayList; 

public class SquashMain { 

public static String MENU = "Options:\nA) Add player\nS) Show players\n G) Update Amount Paid\nX) Exit"; 
public static String FILE_NAME = "c:\\cis2232\\players.csv"; 

public static void main(String[] args) throws IOException { 

    Files.createDirectories(Paths.get("/cis2232")); 

    ArrayList<SquashPlayer> theList = new ArrayList(); 
    loadPlayers(theList); 
    String choice = ""; 
    do{ 
     System.out.println(MENU); 
     choice = FileUtility.getInput().nextLine().toUpperCase(); 

     switch(choice){ 
      case "A": 
       SquashPlayer player = new SquashPlayer(true); 
       theList.add(player); 


       BufferedWriter bw = null; 
       FileWriter fw = null; 

       try { 
        fw = new FileWriter(FILE_NAME, true); 
        bw = new BufferedWriter(fw); 
        bw.write(player.getCSV(true)); 
        System.out.println("Done"); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } finally { 
        try { 
         if (bw != null) { 
          bw.close(); 
         } 

         if (fw != null) { 
          fw.close(); 
         } 

        } catch (IOException ex) { 

         ex.printStackTrace(); 

        } 

       } 

       break; 
      case "S": 
       System.out.println("Here are the players"); 
       for (SquashPlayer SquashPlayer : theList) { 
        System.out.println(SquashPlayer); 
       } 
       break; 
      case "G": 
       System.out.println("Enter ID:"); 
       int input = FileUtility.getInput().nextInt(); 
       FileUtility.getInput().nextLine(); 
       for(SquashPlayer id:theList){ 
        if(id.getId() == input){ 
         System.out.println("Enter in new amount paid:"); 
         int newAmount = FileUtility.getInput().nextInt(); 
         FileUtility.getInput().nextLine(); 
         id.setAmountPaid(newAmount); 
        } 
       } 
      case "X": 
       System.out.println("Goodbye"); 
       break; 
      default: 
       System.out.println("Invalid option"); 
       break;  
     } 


    }while (!choice.equalsIgnoreCase("x")); 

} 
public static void loadPlayers(ArrayList squash){ 
    System.out.println("Loading players from the list!"); 
    int counter = 0; 

    try{ 
     ArrayList<String> tester = (ArrayList<String>) Files.readAllLines(Paths.get(FILE_NAME)); 
     for(String current:tester){ 
      System.out.println("Loading: "+current); 
      SquashPlayer temp = new SquashPlayer(current); 
      squash.add(temp); 
      counter++; 

     } 
    }catch(IOException ex){ 
     System.out.println("Error loading players from file."); 
     System.out.println(ex.getMessage()); 
    } 
    System.out.println("Loaded players from file: "+ counter + " players"); 
} 

}

ここSquashPlayerクラス

package squashapplication; 

import java.util.Scanner; 

/** 
* 

*/ 
public class SquashPlayer { 

private static int maxRegistrationId; 
private int id; 
private String name; 
private String parentName; 
private String phoneNumber; 
private String email; 
private int amountPaid; 


public SquashPlayer() { 

} 


public SquashPlayer(boolean getFromUser){ 


    System.out.println("Enter Full Name:"); 
    this.name = FileUtility.getInput().nextLine(); 

    System.out.println("Enter Parents name:"); 
    this.parentName = FileUtility.getInput().nextLine(); 

    System.out.println("Enter phone number:"); 
    this.phoneNumber = FileUtility.getInput().nextLine(); 

    System.out.println("Enter e-mail:"); 
    this.email = FileUtility.getInput().nextLine(); 

    System.out.println("Enter amount paid:"); 
    this.amountPaid = FileUtility.getInput().nextInt(); 

    FileUtility.getInput().nextLine(); 

    this.id = ++ maxRegistrationId; 

} 
public SquashPlayer(int id, String name, int amountPaid , String phoneNumber, String parentName , String email ) { 
    this.id = id; 
    this.amountPaid = amountPaid; 
    this.name = name; 
    this.parentName = parentName; 
    this.email = email; 
    this.phoneNumber = phoneNumber; 
} 
public SquashPlayer(String[] parts) { 
    this(Integer.parseInt(parts[0]), parts[1], Integer.parseInt(parts[2]), parts[3],parts[4], parts[5]); 

    if (Integer.parseInt(parts[0]) > maxRegistrationId) { 
     maxRegistrationId = Integer.parseInt(parts[0]); 
    } 
} 
public SquashPlayer(String csvValues) { 
    this(csvValues.split(",")); 
} 

public String getCSV() { 
    return id + "," + name + "," + amountPaid + "," + phoneNumber + "," + email + "," + parentName; 
} 
public String getCSV(boolean withLineFeed){ 
    if(withLineFeed){ 
     return getCSV()+System.lineSeparator(); 
    }else{ 
     return getCSV(); 
    } 
} 


public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getParentName() { 
    return parentName; 
} 

public void setParentName(String parentName) { 
    this.parentName = parentName; 
} 

public String getPhoneNumber() { 
    return phoneNumber; 
} 

public void setPhoneNumber(String phoneNumber) { 
    this.phoneNumber = phoneNumber; 
} 

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

public int getAmountPaid() { 
    return amountPaid; 
} 

public void setAmountPaid(int amountPaid) { 
    this.amountPaid = amountPaid; 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 



@Override 
public String toString() { 
    return "ID=" +id+ ", Name=" + name + ", email=" + email + ", Phone Number=" + phoneNumber + ", Amount Paid=" + amountPaid + ", Parent's Name: "+parentName; 
} 

}

答えて

0

これは、助け 代わりのfw.write、その可能性があります私はthを信じるあなたが使っているものでは、末尾に改行文字"\n"を追加してfw.appendを使用してください。

+0

しかし、すでにファイルにあるエントリを上書きしたいとします。プレイヤーの情報を編集したいのと同じように、switch文で "G"を押すと、ファイル内の情報が上書きされます。 – Shuckyducky

関連する問題