Skip to main content
edited title
Link

Can I write only in certain lines in a file with BufferdWriter without overwrite others and only certain lines as well eith BufferdReader?

added 24 characters in body; edited tags
Source Link

I want to save a score and a namehighscore in a filedatabase, but there are differntif an sql-exeption is thrown I want to temporary save the data in a file. The game has diffent modes that have differnt valuesand for each there is a highscore. I would like to have 2 lines for each mode, in which the name and the score are saved without overwritig other linesa savefile like this:

2 Bob 4 Luca 6 Celina 9 Bob 

I tried using "newLine()" to skipLine 1 is the lines I'm already using, but thenscore for gamemode 1 and in line 2 there is the data from those lines gets deletedname of the player. In addition,Line 3 and 4 for gamemode 2 and so on a restart differnt lines are used than before.

Writing partI have this Java Code:

Reading Part:The problem is, that the data of line 1 and 2 for example gets deleted if I want to save the data for line 3 and 4.

I tried using "newLine()" to skip the lines I'm already using, but then the data from those lines gets deleted. For example if I want to save the score 4 with the name Luca (line 3 and 4) the score of the gamemode 1 (2 and Bob, line 1 and 2) gets deletetd.

try{ highscoreData = new HighscoreData(); BufferedReader br = new BufferedReader(new FileReader("....SaveFile.txt")); highscoreData.setScore(Integer.parseInt(br.readLine())); <blank> highscoreData.setName(br.readLine());  <blank> br.close();   }catch (Exception e2) {4  }Luca 

I want to save a score and a name in a file, but there are differnt modes that have differnt values. I would like to have 2 lines for each mode, in which the name and the score are saved without overwritig other lines

I tried using "newLine()" to skip the lines I'm already using, but then the data from those lines gets deleted. In addition, on a restart differnt lines are used than before.

Writing part:

Reading Part:

try{ highscoreData = new HighscoreData(); BufferedReader br = new BufferedReader(new FileReader("....SaveFile.txt")); highscoreData.setScore(Integer.parseInt(br.readLine()));  highscoreData.setName(br.readLine());   br.close();   }catch (Exception e2) {  } 

I want to save a highscore in a database, but if an sql-exeption is thrown I want to temporary save the data in a file. The game has diffent modes and for each there is a highscore. I would like to have a savefile like this:

2 Bob 4 Luca 6 Celina 9 Bob 

Line 1 is the score for gamemode 1 and in line 2 there is the name of the player. Line 3 and 4 for gamemode 2 and so on.

I have this Java Code:

The problem is, that the data of line 1 and 2 for example gets deleted if I want to save the data for line 3 and 4.

I tried using "newLine()" to skip the lines I'm already using, but then the data from those lines gets deleted. For example if I want to save the score 4 with the name Luca (line 3 and 4) the score of the gamemode 1 (2 and Bob, line 1 and 2) gets deletetd.

<blank> <blank> 4 Luca 
Source Link

Can I write only in certain lines in a file with BufferdWriter without overwrite others and only certain lines as well eith BufferdReader?

I want to save a score and a name in a file, but there are differnt modes that have differnt values. I would like to have 2 lines for each mode, in which the name and the score are saved without overwritig other lines

I tried using "newLine()" to skip the lines I'm already using, but then the data from those lines gets deleted. In addition, on a restart differnt lines are used than before.

Writing part:

public static void saveHighscoreToDatabase(int spielModus, String nameHighscoretraeger, int score){ try { Connection connection = DriverManager.getConnection(url, user, password); String sql = null; if (spielModus == 1){ sql = "INSERT INTO HighscoreLeicht (`Name`, `Score`) VALUES (?,?)"; }else if (spielModus == 2){ sql = "INSERT INTO HighscoreMittel (`Name`, `Score`) VALUES (?,?)"; }else if (spielModus == 3){ sql = "INSERT INTO HighscoreSchwer (`Name`, `Score`) VALUES (?,?)"; }else if (spielModus == 4){ sql = "INSERT INTO HighscoreModus (`Name`, `Score`) VALUES (?,?)"; } PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1,nameHighscoretraeger); statement.setInt(2, score); statement.execute(); connection.close(); } catch (SQLException e) { try { BufferedWriter bw = new BufferedWriter(new FileWriter("....SaveFile.txt",true)); if (spielModus == 1){ bw.write(String.valueOf(score)); bw.newLine(); bw.write(nameHighscoretraeger); bw.close(); }else if (spielModus == 2){ bw.newLine(); bw.newLine(); bw.write(String.valueOf(score)); bw.newLine(); bw.write(nameHighscoretraeger); bw.close(); }else if (spielModus == 3){ }else if (spielModus == 4){ } }catch (Exception e2){ System.out.println("Error"); } } 

Reading Part:

try{ highscoreData = new HighscoreData(); BufferedReader br = new BufferedReader(new FileReader("....SaveFile.txt")); highscoreData.setScore(Integer.parseInt(br.readLine())); highscoreData.setName(br.readLine()); br.close(); }catch (Exception e2) { }