- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
几天前,我发布了有关我正在为学校做的一个项目的信息,我得到了一些非常有用的帮助,使我能够进一步推进我的项目。尽管如此,还是出现了其他情况。在我正在做的这个程序中,我的目的是为马输入不同的“训练方法”。这是由用户在程序中输入的,旨在将该训练方法写入文本文件。这工作得很好,但是假设我想为另一匹马输入另一种训练方法。这种情况会发生,但第一匹马的输入将被完全删除,只有最后写入文本文件的内容将保留。有谁知道为什么会这样以及我可以采取哪些步骤来解决它?谢谢你! (抱歉,代码很长,我只是不知道哪一部分可能会弄乱我的程序)
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
}
最佳答案
在您的jButton1ActionPerformed(ActionEvent)
方法中:
首先,改变
FileWriter tWriter = new FileWriter("HorseNutrition.txt");
到
OutputStreamWriter tWriter = new OutputStreamWriter(new FileOutputStream("HorseNutrition.txt"));
FileWriter
只是实现作为缩写,因此这里没有逻辑差异。事实上,FileWriter(String)
构造函数几乎执行了下面所述的操作。
然后使用 FileOutputStream(File, boolean)
构造函数。这里,传递的 boolean 值表示是否保留文件中已包含的数据,因此是否应附加给定的输入。
这会导致
OutputStreamWriter tWriter = new OutputStreamWriter(new FileOutputStream("HorseNutrition.txt", true));
关于java - 无法避免覆盖文本文件上的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42328898/
我有一个简单的 pyparsing 构造,用于提取日志消息的部分内容。看起来像这样 log_line = 时间戳 + task_info + Suppress(LineEnd()) 此结构可以很好地解
我想定义一个函数 scaryDict(),它接受一个参数(textfile)并返回 textfile 中的单词按字母顺序排列,基本上生成字典但不打印任何一个或两个字母的单词。 这是我目前所拥有的……不
我正在尝试弄清楚如何包含对外部数据文件(文本形式)的引用,我希望通过 Web Start (JNLP) 与我的应用程序一起分发该文件。筛选 JNLP 结构的文档,我发现您可以包含对 JAR、nativ
我尝试将 Java 程序从 Eclipse 导出到 .jar 文件,但遇到了问题。它运行良好,但由于某种原因它没有找到它应该从中获取数据的文本文件。如果有人能帮忙解决这个问题,我将非常感激。 最佳答案
在过去的 20 个小时里,我试图解决以下问题,所以在开始考虑跳出窗外之前我想,我最好在这里寻求帮助: I have a text file with following content: ID 1 T
今天我试图删除一个简单文本文件中的重复行,例如: input (list.txt): hello hello try output (list.txt): try 我尝试使用 Notepad++ 删除
我将一个文本文件添加到我的项目中,如下路径所示: Myproject/WebPages/stopwords.txt 图片: http://s7.postimg.org/w65vc3lx7/Untitl
所以我在我的程序上工作,现在我无法找到解决方案。我需要在 fext 文件中替换更多的符号,目前程序只将“TIT”替换为代码“*245$a”,如果我想用同样的方式替换其他字母,程序不会改变。有人知道如何
这是一个非常简单的问题,但无论我看哪里,我都会得到不同的答案(这是因为它在 c++0x 中已经改变还是将要改变?): 在 C++ 中,我如何从一个文本文件中读取两个数字并将它们输出到另一个文本文件中?
我有一个 C++ 项目应该添加 到每一行的开头和到每一行的末尾。这适用于普通英文文本,但我有一个中文文本文件,我想这样做,但它不起作用。我通常使用 .txt 文件,但为此我必须使用 .rtf 来保存中
所以我的驱动看起来像这样: #include "problem2.h" #include "problem1.h" #include "problem3.h" #include #include
我有一个包含字符串标识符的 ascii 数字文本文件(>50k 行),可以将其视为数据 vector 的集合。根据用户输入,应用程序在运行时只需要这些数据 vector 之一。 据我所知,我有 3 个
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Any decent text diff/merge engine for .NET ? 我有两个文本文件,
我正在尝试将对话选择器中的唤醒时间和 sleep 时间记录到这样的文本文件中,但是对方法 commitToFile2 的调用不会 append 文本文件“savedData.txt”。 我知道这段代码
我开发了一个 android webview 并尝试在单击 webview 中的链接时下载生成的数据:文本文件。 webView.setDownloadListener(new Downloa
我在一个文本文件中有 250 张图像/天 4000*3000 像素。 file '/home/user/camdata/nonseqdata.jpg' file '/home/user/camdata
我曾多次尝试将此配置文件转换为多维数组,这意味着我必须读取 config.txt 文件,然后必须将其转换为多维数组。我需要帮助或一些建议。 配置文件: id=www session.timeout=1
我正在尝试使用 sublime text 3 打开文件,我想用光标在具体行号处打开它。 我一直在查subl --help但我找不到混凝土线的选择。因此我只是使用:subl filename 有没有办法
我想在我的应用程序中快速显示一个大文本文件的内容,而不是将整个文件加载到内存中。 其他人是怎么做的? Total Commander是一个很棒的工具,它有一个很棒的内部查看器可以做到这一点。无论文件有
我是一名优秀的程序员,十分优秀!