2017-02-19 6 views
0

私がやっていた学校のプロジェクトについて2,3日前に投稿しました。それにもかかわらず、何か他のものが生じました。私がやっているこのプログラムでは、私は馬のための異なる「訓練方法」を入力することを意図しています。これは、ユーザがプログラムで入力したもので、そのトレーニング方法をテキストファイルに書き込むためのものです。これはうまくいきますが、私は別の馬のために別の訓練方法を入力したいとしましょう。これは起こりますが、最初の馬からの入力は完全に削除され、テキストファイルに書き込まれた最後のものだけが残ります。なぜ誰がこのことができるのか、それを修正するために何ができるのかを知っていますか?ありがとうございました! (申し訳ありませんが、私はちょうど合法的にそれはそれは私のプログラムをめちゃくちゃにすることができているどの部分かわからないロングコードのために)あなたのjButton1ActionPerformed(ActionEvent)方法でテキストファイルのすべてを上書きすることを避けることはできません

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.PrintWriter; 
import java.util.StringTokenizer; 
import javax.swing.JOptionPane; 


public AddNutrition() { 
    initComponents(); 




    NutritionCB.removeAllItems(); 
    NutritionCB.addItem("Choose Horse..."); 

    try{ 
     FileReader horseReader = new FileReader("HorseFile.txt"); 
     BufferedReader horseBuffer = new BufferedReader(horseReader); 
     String record = ""; 
     while((record = horseBuffer.readLine())!=null) 
     { 
      StringTokenizer tokenizerHorse = new StringTokenizer(record,","); 
      String vHorseID = tokenizerHorse.nextToken(); 
      String vHorseName = tokenizerHorse.nextToken(); 
      String vHorseBreed = tokenizerHorse.nextToken(); 
      String vHorseDOB = tokenizerHorse.nextToken(); 
      String vHorseHeight = tokenizerHorse.nextToken(); 
      String vHorseHandSize = tokenizerHorse.nextToken(); 

      NutritionCB.addItem(vHorseBreed + "_" + vHorseName); 
     } 
     horseBuffer.close(); 
    } 
    catch (Exception Error) { 
     System.out.println("Error in reading file."); 
    } 
} 


@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    jLabel1 = new javax.swing.JLabel(); 
    NutritionCB = new javax.swing.JComboBox<>(); 
    jLabel2 = new javax.swing.JLabel(); 
    jLabel3 = new javax.swing.JLabel(); 
    jLabel4 = new javax.swing.JLabel(); 
    jLabel5 = new javax.swing.JLabel(); 
    jLabel6 = new javax.swing.JLabel(); 
    jLabel7 = new javax.swing.JLabel(); 
    jLabel8 = new javax.swing.JLabel(); 
    HorseID = new javax.swing.JTextField(); 
    HorseName = new javax.swing.JTextField(); 
    HorseBreed = new javax.swing.JTextField(); 
    HorseDOB = new javax.swing.JTextField(); 
    HorseHeight = new javax.swing.JTextField(); 
    HorseHandSize = new javax.swing.JTextField(); 
    jLabel9 = new javax.swing.JLabel(); 
    jLabel10 = new javax.swing.JLabel(); 
    jLabel11 = new javax.swing.JLabel(); 
    FoodName = new javax.swing.JTextField(); 
    FoodQuantity = new javax.swing.JTextField(); 
    FoodTime = new javax.swing.JTextField(); 
    LoadButton = new javax.swing.JToggleButton(); 
    jButton1 = new javax.swing.JButton(); 

    jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N 
    jLabel1.setText("Add Horse Nutrition"); 

    jLabel2.setText("Select Horse:"); 

    jLabel3.setText("Horse ID"); 

    jLabel4.setText("Horse Name"); 

    jLabel5.setText("Horse Breed"); 

    jLabel6.setText("Horse DOB"); 

    jLabel7.setText("Horse Height"); 

    jLabel8.setText("Horse Handsize"); 

    HorseID.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      HorseIDActionPerformed(evt); 
     } 
    }); 

    jLabel9.setText("Food Name:"); 

    jLabel10.setText("Food Quantity:"); 

    jLabel11.setText("Food Time:"); 

    FoodName.setToolTipText(""); 
    FoodName.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      FoodNameActionPerformed(evt); 
     } 
    }); 

    LoadButton.setText("Load Horse"); 
    LoadButton.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      LoadButtonActionPerformed(evt); 
     } 
    }); 

    jButton1.setText("Save"); 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton1ActionPerformed(evt); 
     } 
    }); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
    this.setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
      .addComponent(jLabel1) 
      .addGap(213, 213, 213)) 
     .addGroup(layout.createSequentialGroup() 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(37, 37, 37) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
         .addGroup(layout.createSequentialGroup() 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
           .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
             .addComponent(jLabel3) 
             .addComponent(jLabel7) 
             .addComponent(jLabel6)) 
            .addGap(28, 28, 28)) 
           .addGroup(layout.createSequentialGroup() 
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
             .addComponent(jLabel4) 
             .addComponent(jLabel5)) 
            .addGap(46, 46, 46)) 
           .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
            .addComponent(jLabel8) 
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
           .addComponent(HorseBreed, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE) 
           .addComponent(HorseDOB) 
           .addComponent(HorseHandSize) 
           .addComponent(HorseHeight) 
           .addComponent(HorseName) 
           .addComponent(HorseID)) 
          .addGap(87, 87, 87) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
           .addComponent(jLabel11) 
           .addComponent(jLabel10) 
           .addComponent(jLabel9)) 
          .addGap(42, 42, 42) 
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
           .addComponent(FoodQuantity, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE) 
           .addComponent(FoodTime) 
           .addComponent(FoodName))) 
         .addGroup(layout.createSequentialGroup() 
          .addComponent(jLabel2) 
          .addGap(64, 64, 64) 
          .addComponent(NutritionCB, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE) 
          .addGap(18, 18, 18) 
          .addComponent(LoadButton)))) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(278, 278, 278) 
        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))) 
      .addContainerGap(128, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(18, 18, 18) 
      .addComponent(jLabel1) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addGap(79, 79, 79) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(jLabel9) 
         .addComponent(FoodName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addGap(21, 21, 21) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
         .addComponent(jLabel10) 
         .addGroup(layout.createSequentialGroup() 
          .addGap(1, 1, 1) 
          .addComponent(FoodQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(jLabel11) 
         .addComponent(FoodTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) 
       .addGroup(layout.createSequentialGroup() 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(NutritionCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addComponent(jLabel2) 
         .addComponent(LoadButton)) 
        .addGap(49, 49, 49) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(jLabel3) 
         .addComponent(HorseID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(jLabel4) 
         .addComponent(HorseName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(jLabel5) 
         .addComponent(HorseBreed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(HorseDOB, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addComponent(jLabel6)) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(HorseHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addComponent(jLabel7)) 
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
         .addComponent(HorseHandSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addComponent(jLabel8)))) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE) 
      .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addGap(21, 21, 21)) 
    ); 
}// </editor-fold>       

private void HorseIDActionPerformed(java.awt.event.ActionEvent evt) {           
    // TODO add your handling code here: 
}          

private void FoodNameActionPerformed(java.awt.event.ActionEvent evt) {           
    // TODO add your handling code here: 
}           

private void LoadButtonActionPerformed(java.awt.event.ActionEvent evt) {           
String CBHorseLoading = NutritionCB.getSelectedItem()+""; 
StringTokenizer stCBHorseLoad = new StringTokenizer(CBHorseLoading,"_"); 
String HCBBreed = stCBHorseLoad.nextToken().trim(); 
String HCBHorse = stCBHorseLoad.nextToken().trim(); 

try { 
    FileReader horseReader = new FileReader("HorseFile.txt"); 
    BufferedReader horseBuffer = new BufferedReader(horseReader); 
    String record = ""; 
    while((record = horseBuffer.readLine())!=null) 
    { 
     StringTokenizer TokenizerHorse = new StringTokenizer(record,","); 
     String vHorseID = TokenizerHorse.nextToken(); 
     String vHorseName = TokenizerHorse.nextToken(); 
     String vHorseBreed = TokenizerHorse.nextToken(); 
     String vHorseDOB = TokenizerHorse.nextToken(); 
     String vHorseHeight = TokenizerHorse.nextToken(); 
     String vHorseHandSize = TokenizerHorse.nextToken(); 
     if (HCBHorse.equals(vHorseName)){ 
      System.out.println(); 

      HorseID.setText(vHorseID); 
      HorseName.setText(vHorseName); 
      HorseBreed.setText(vHorseBreed); 
      HorseDOB.setText(vHorseDOB); 
      HorseHeight.setText(vHorseHeight); 
      HorseHandSize.setText(vHorseHandSize); 


     } 

    } 
    horseBuffer.close(); 
} 
catch(Exception Error){ 
    System.out.println("Error in loading horse."); 
} 


    // TODO add your handling code here: 
}           

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
boolean entryError = false; 
if(FoodName.getText().equals("")) 
{ 
JOptionPane.showMessageDialog(null, "Enter the Horse Food   Name.","Error.",JOptionPane.ERROR_MESSAGE); 
entryError = true; 

} 
if(FoodQuantity.getText().equals("")&& entryError==false) 
{ 
JOptionPane.showMessageDialog(null, "Enter the Horse Food  Quantity.","Error.",JOptionPane.ERROR_MESSAGE); 
entryError = true; 

} 
if(FoodTime.getText().equals("")&& entryError == false) 
{ 
JOptionPane.showMessageDialog(null, "Enter the Horse Food  Times.","Error.",JOptionPane.ERROR_MESSAGE); 
entryError = true; 

} 

if(entryError == false) 
{ 


try { 
    FileWriter tWriter = new FileWriter("HorseNutrition.txt"); 
    PrintWriter tPrinter = new PrintWriter(tWriter); 

    tPrinter.println(HorseID.getText() + "," + FoodName.getText() + "," + FoodQuantity.getText() + "," + FoodTime.getText()); 
    tPrinter.close(); 

    JOptionPane.showMessageDialog(null, "Nutritional data has been saved.","Info",JOptionPane.INFORMATION_MESSAGE); 


} 
catch(Exception error) { 
    System.out.println("Error in saving nutritional data"); 
} 
    } 


}           


// Variables declaration - do not modify      
private javax.swing.JTextField FoodName; 
private javax.swing.JTextField FoodQuantity; 
private javax.swing.JTextField FoodTime; 
private javax.swing.JTextField HorseBreed; 
private javax.swing.JTextField HorseDOB; 
private javax.swing.JTextField HorseHandSize; 
private javax.swing.JTextField HorseHeight; 
private javax.swing.JTextField HorseID; 
private javax.swing.JTextField HorseName; 
private javax.swing.JToggleButton LoadButton; 
private javax.swing.JComboBox<String> NutritionCB; 
private javax.swing.JButton jButton1; 
private javax.swing.JLabel jLabel1; 
private javax.swing.JLabel jLabel10; 
private javax.swing.JLabel jLabel11; 
private javax.swing.JLabel jLabel2; 
private javax.swing.JLabel jLabel3; 
private javax.swing.JLabel jLabel4; 
private javax.swing.JLabel jLabel5; 
private javax.swing.JLabel jLabel6; 
private javax.swing.JLabel jLabel7; 
private javax.swing.JLabel jLabel8; 
private javax.swing.JLabel jLabel9; 
// End of variables declaration     
} 
+0

ルックになります。また、あまりにも多くのコードを投稿して回答を得ることはあまりありません。おそらく、ファイルに書き込むコードだけを投稿してください。 –

答えて

1

まず、変更

FileWriter tWriter = new FileWriter("HorseNutrition.txt"); 

OutputStreamWriter tWriter = new OutputStreamWriter(new FileOutputStream("HorseNutrition.txt")); 

は、略語として機能するように実装されましたここでは論理的な違いはありません。実際には、FileWriter(String)コンストラクタは、以下に述べることをかなり行っています。

次に、FileOutputStream(File, boolean)コンストラクタを使用します。ここで、渡されたbooleanは、ファイルに既に含まれているデータが残っているかどうか、したがって指定された入力が追加されるかどうかを示します。

これは、「追加」またはファイルの末尾に追加するに

OutputStreamWriter tWriter = new OutputStreamWriter(new FileOutputStream("HorseNutrition.txt", true)); 
+0

これは、あなたが馬を読んだり、追加したりしたい場合にのみ有効です。データを変更したい場合、これは役に立ちません。 – Kayaman

+0

@Kayamanあなたは完全に正しいです。しかし、OPは「私は別の馬のための別の訓練方法を入力したい」と述べたので、それは問題ないと思います。 – Izruo

+0

馬の編集機能が表示される可能性があるので、警告として追加しました。 – Kayaman

関連する問題