所以,我想更改文件Utilizadores.txt
中的值
默认值为zero
,但是当我单击按钮时,值必须更改为10,每次我执行新值时,值将获得+10
问题是当e尝试这样做时应该出现的空间(10/20/30 ......)显示为空白
PS:它只会更改具有特定ID(我的用户ID)的文件行
private void ConfirmarActionPerformed(java.awt.event.ActionEvent evt) {
String filePath = "Reservas.txt";
File file = new File(filePath);
try {
FileWriter fw1 = new FileWriter(file, true);
BufferedWriter bw1 = new BufferedWriter(fw1);
bw1.write(this.id+ "-" + cbxrestaurante.getSelectedItem().toString() + "-" + Dia.getText() + "-" + Hora.getText() + "-" + Lugares.getText());
bw1.newLine();
bw1.close();
fw1.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(ClienteForm.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ClienteForm.class.getName()).log(Level.SEVERE, null, ex);
}
String filePath1 = "Utilizadores.txt";
File file1 = new File(filePath1);
try {
FileReader fr = new FileReader(file1);
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(file1, true);
PrintWriter bw = new PrintWriter(fw);
/*BufferedReader br = new BufferedReader(new FileReader(file1));
FileWriter fw = new FileWriter(file1, true);
PrintWriter bw = new PrintWriter((file1), "UTF-8");*/
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file1, true)));
Object[] lines = br.lines().toArray();
Integer[] getPontos = new Integer[lines.length];
String[] identificador = new String[lines.length];
String[] cartao = new String[lines.length];
String[] Pass = new String[lines.length];
String[] Nome = new String[lines.length];
String[] NIF = new String[lines.length];
String[] Tele = new String[lines.length];
String Zero = "zero";
String[] Morada = new String[lines.length];
String[] Localidade = new String[lines.length];
String[] dono = new String[lines.length];
for (int i = 0; i < lines.length; i++) {
String[] row = lines[i].toString().split("-");
if (Objects.equals(Zero, row[10])||Integer.parseInt(row[10])==0) {
getPontos[i] = 0;
} else {
getPontos[i] = Integer.parseInt(row[10]);
}
identificador[i] = row[1];
cartao[i] = row[2];
Pass[i] = row[3];
Nome[i] = row[4];
NIF[i] = row[5];
Tele[i] = row[6];
Morada[i] = row[7];
Localidade[i] = row[8];
dono[i] = row[9];
if (Integer.parseInt(identificador[i]) == this.id) {
if (getPontos[i] == 0) {
getPontos[i] = 10;
} else {
getPontos[i] = getPontos[i] + 10;
}
}
}
fr.close();
br.close();
File temp = new File("Utilizadores.txt");
if (temp.exists()) {
RandomAccessFile raf = new RandomAccessFile(temp, "rw");
raf.setLength(0);
}
for (int i = 0; i < lines.length; i++) {
bw.write("UserId-");
bw.write(identificador[i] + "-");
bw.write(cartao[i] + "-");
bw.write(Pass[i] + "-");
bw.write(Nome[i] + "-");
bw.write(NIF[i] + "-");
bw.write(Tele[i] + "-");
bw.write(Morada[i] + "-");
bw.write(Localidade[i] + "-");
bw.write(dono[i] + "-");
bw.write(getPontos[i]);
bw.write("\r\n");
}
bw.close();
fw.close();
this.dispose();
} catch (FileNotFoundException ex) {
System.out.println("Import nao funciona");
} catch (IOException ex) {
Logger.getLogger(ClienteForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
答案 0 :(得分:0)
尝试将整数转换为字符串:
bw.write( String.valueOf(getPontos[i]));
代替
bw.write(getPontos[i]);