So I'm trying my hand at some basic file i/o following a few online tutorials and such. I think I have the right idea but there is a multitude of ways to accomplish this, and varying examples are starting to make my head spin. My code definitely could use some help...
Ironically, the reason I cannot compile currently actually due to newLine() not being recognizable. I ran into this problem using \n in a drawString before, but this is not as easy to work around.
Here is my code for the I/O. If it would be more helpful to post the entire class I will do so, but it is lengthy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public static int [] loadScores() throws IOException { //load preiviously saved scores from file BufferedReader inputStream = null ; int scoreArray[] = new int [ 5 ]; inputStream = new BufferedReader( new FileReader( "highscores.txt" )); int num = 0 ; //line count String t = "" ; int start = Integer.parseInt(inputStream.readLine()); //first number in file while ((t = inputStream.readLine()) != null ) // only if there is something to read { scoreArray[num] = Integer.parseInt(t); num++; } inputStream.close(); return scoreArray; } |
1 2 3 4 5 6 7 8 9 10 11 | //save new array of highscores to file FileWriter outputStream = null ; outputStream = new FileWriter( "highscores.txt" , false ); for ( int k= 0 ; k < allArray.length; k++) { outputStream.write(allArray[k]); outputStream.newLine(); } outputStream.close(); } |