好吧,我正在尝试制作输入/输出文件,所以在我的应用程序开始时它会读取文件并将信息放在正确的位置,当我在应用程序中时,我可以添加信息到这个文件。
问题在于它读写的内容非常奇怪,例如“1”。这是我的阅读功能:
private Vector<GEPlayer> ReadPlayers() {
Vector<GEPlayer> play_aux = new Vector<>();
String path = System.getProperty("user.dir");
try {
File fread = new File(path + "/bin/Players.txt");
System.out.println(fread.length());
if (fread.length() > 3) {
BufferedReader bread = new BufferedReader(new InputStreamReader(new FileInputStream(fread), "ISO-8859-1"));
String linea;
int i = 0;
while ((linea = bread.readLine()) != null) {
GEPlayer p_aux = new GEPlayer(-1);
if (linea != null) {
switch (i) {
case 0:
currentidplayer = Integer.parseInt(linea);
p_aux.setId_player(currentidplayer);
System.out.println("GEModel -- ReadPlayers -- Readed Player ID: " + currentidplayer);
i++;
break;
case 1:
currentidteam = Integer.parseInt(linea);
p_aux.setId_team(currentidteam);
System.out.println("GEModel -- ReadPlayers -- Readed Player Team ID: " + currentidplayer);
i++;
break;
case 2:
p_aux.setName(linea);
System.out.println("GEModel -- ReadPlayers -- Readed Player Name: " + linea);
i++;
break;
case 3:
p_aux.setSurname(linea);
System.out.println("GEModel -- ReadPlayers -- Readed Player Surname: " + linea);
i++;
break;
case 4:
p_aux.setNacionality(linea);
System.out.println("GEModel -- ReadPlayers -- Readed Player Nacionality: " + linea);
i++;
break;
case 5:
p_aux.setActualTeam(linea);
System.out.println("GEModel -- ReadPlayers -- Readed Player Actual Team: " + linea);
i++;
break;
case 6:
p_aux.setBornDate(linea);
System.out.println("GEModel -- ReadPlayers -- Readed Player Born Date: " + linea);
i++;
break;
case 7:
p_aux.setNumber(Integer.parseInt(linea));
System.out.println("GEModel -- ReadPlayers -- Readed Player Number: " + linea);
play_aux.add(p_aux);
i = 0;
break;
}
}
}
if (i != 0) {
currentidplayer = 0;
play_aux.removeAllElements();
}
}
} catch (FileNotFoundException | UnsupportedEncodingException ex) {
Logger.getLogger(GEModel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(GEModel.class.getName()).log(Level.SEVERE, null, ex);
}
return play_aux;
}
这是我的写作功能:
private void writePlayer(GEPlayer aux) {
File fw;
BufferedWriter bw = null;
try {
String path = System.getProperty("user.dir");
fw = new File(path + "/bin/Players.txt"); ///< The true will append the new data
FileOutputStream fos = new FileOutputStream(fw);
bw = new BufferedWriter(new OutputStreamWriter(fos));
bw.write(aux.getId_player()); ///< Appends the string to the file
System.out.println("GEModel -- writePlayer -- Printed: " + aux.getId_player());
bw.newLine();
bw.write(aux.getId_team()); ///< Appends the string to the file
System.out.println("GEModel -- writePlayer -- Printed: " + aux.getId_team());
bw.newLine();
bw.write(aux.getName()); ///< Appends the string to the file
System.out.println("GEModel -- writePlayer -- Printed: " + aux.getName());
bw.newLine();
bw.write(aux.getSurname()); ///< Appends the string to the file
System.out.println("GEModel -- writePlayer -- Printed: " + aux.getSurname());
bw.newLine();
bw.write(aux.getNacionality()); ///< Appends the string to the file
System.out.println("GEModel -- writePlayer -- Printed: " + aux.getNacionality());
bw.newLine();
bw.write(aux.getActualTeam()); ///< Appends the string to the file
System.out.println("GEModel -- writePlayer -- Printed: " + aux.getActualTeam());
bw.newLine();
bw.write(aux.getBornDate()); ///< Appends the string to the file
System.out.println("GEModel -- writePlayer -- Printed: " + aux.getBornDate());
bw.newLine();
bw.write(aux.getNumber()); ///< Appends the string to the file
System.out.println("GEModel -- writePlayer -- Printed: " + aux.getNumber());
bw.newLine();
bw.close();
} catch (IOException ex) {
Logger.getLogger(GEModel.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
bw.close();
} catch (IOException ex) {
Logger.getLogger(GEModel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
当我执行没有txt信息的程序时,我可以完整地阅读所有内容,它会显示正确的信息,并告诉我信息是正确的。但是当我重新启动应用程序时,它会抛出这样的错误:
Exception in thread "main" java.lang.NumberFormatException: For input string: "1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at GEModel.GEModel.ReadPlayers(GEModel.java:55) ///< This line is this: "currentidplayer = Integer.parseInt(linea);"
at GEModel.GEModel.<init>(GEModel.java:34)
at GEMain.GEMainClass.main(GEMainClass.java:22)
Java结果:1
我该怎么做才能解决这个问题?
答案 0 :(得分:0)
这是因为当你读到时你设置了显式编码。但是当写不到时,使用默认值 - 通常是UTF-8。
在写作和阅读时使用相同的文本编码(UTF-8是最佳选择)。
尝试简单的事情:
File f=new File("/tmp/test.txt");
try (FileOutputStream fos=new FileOutputStream(f)){
int id=1;
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
bw.write(String.valueOf(id)); //write player id
bw.newLine();
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
try (FileInputStream fis=new FileInputStream(f)){
BufferedReader br = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
String idstr=br.readLine();
System.out.printf("readstr: '%s'\n",idstr);
int id=Integer.parseInt(idstr); // parse player id
System.out.printf("parse id: '%d'\n",id);
br.close();
} catch (Exception e) {
e.printStackTrace();
}