java 写文件覆盖问题
publicvoidrun(){Filecordrecord=newFile("d://cordrecord.txt");try{br=newBufferedReader...
public void run() {
File cordrecord = new File("d://cordrecord.txt");
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(
comrecord)));
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, "从文件获取输入流失败!", "操作失败",
JOptionPane.INFORMATION_MESSAGE);
e.printStackTrace();
br = null;
}
try {
if (!cordrecord.exists()) {
try {
cordrecord.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
ps = new PrintStream(new FileOutputStream(cordrecord), true);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(this, "从文件获取输出流失败!", "操作失败",
JOptionPane.INFORMATION_MESSAGE);
ps = null;
}
com = competition.toString();
comp = com.split(" ");
try {
while ((tem = br.readLine()) != null) {
temp = tem.split(" ");
//判断赛程文件中是否有要输入比分的赛程
if ((temp[0].equals(comp[0]) && (temp[1].equals(comp[1])))||((temp[0].equals(comp[1]) && (temp[1].equals(comp[0]))))) {
comp[3] = temp[3];
ps.print(comp[0] + " " + comp[1] + " " + comp[2] + " "
+ comp[3] + "\n");
ps.flush();
flag = true;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "读取数据失败!", "操作失败",
JOptionPane.INFORMATION_MESSAGE);
}
if (flag == true) {
JOptionPane.showMessageDialog(this, "录入比分成功!", "操作成功",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "录入比分失败!", "操作失败",
JOptionPane.INFORMATION_MESSAGE);
}
}
每次对d://cordrecord.txt的写字符串都会删除原先的记录,怎样让写的字符串添加到文件的末尾?书上说在new ps的时候加一个参数true,加了,没用。
用ps.append()方法也没有效果。。 展开
File cordrecord = new File("d://cordrecord.txt");
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(
comrecord)));
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(this, "从文件获取输入流失败!", "操作失败",
JOptionPane.INFORMATION_MESSAGE);
e.printStackTrace();
br = null;
}
try {
if (!cordrecord.exists()) {
try {
cordrecord.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
ps = new PrintStream(new FileOutputStream(cordrecord), true);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(this, "从文件获取输出流失败!", "操作失败",
JOptionPane.INFORMATION_MESSAGE);
ps = null;
}
com = competition.toString();
comp = com.split(" ");
try {
while ((tem = br.readLine()) != null) {
temp = tem.split(" ");
//判断赛程文件中是否有要输入比分的赛程
if ((temp[0].equals(comp[0]) && (temp[1].equals(comp[1])))||((temp[0].equals(comp[1]) && (temp[1].equals(comp[0]))))) {
comp[3] = temp[3];
ps.print(comp[0] + " " + comp[1] + " " + comp[2] + " "
+ comp[3] + "\n");
ps.flush();
flag = true;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "读取数据失败!", "操作失败",
JOptionPane.INFORMATION_MESSAGE);
}
if (flag == true) {
JOptionPane.showMessageDialog(this, "录入比分成功!", "操作成功",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "录入比分失败!", "操作失败",
JOptionPane.INFORMATION_MESSAGE);
}
}
每次对d://cordrecord.txt的写字符串都会删除原先的记录,怎样让写的字符串添加到文件的末尾?书上说在new ps的时候加一个参数true,加了,没用。
用ps.append()方法也没有效果。。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询