Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Delivering the MVP Wrapping Up

Razvan Cirstea
Razvan Cirstea
2,493 Points

How to store the highest score in a file ?

What are the steps I should use in order to store the highest score in a file and read it on every replay of the program? Should I use the method

static Path createFile(Path path, FileAttribute<?>... attrs) (Creates a new and empty file, failing if the file already exists.)

?

if so , how ?

Many thanks.

2 Answers

Balazs Peak
Balazs Peak
46,160 Points

I didn't quite find the problem you are refering to, but according to your question, you'll probably need OutputStream for the writing, and InputStream, for the reading. If this is not enough, please copy your code and what you want to do with it, and I'll go into details :D

Razvan Cirstea
Razvan Cirstea
2,493 Points

First, I'm trying to create a method that writes a string into a file, at a path of my choosing. I've created a separate class for writing into the file . I found some code searching on google and I modified it and created a method out of it, which I called through my Hangman.java file.

First of all, the code from the class that writes to file:

'''java

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

public class WriteToFileExample1 {

private static String FILENAME = "E:\Java\filename.txt";

public WriteToFileExample1(String FILENAME){

  this.FILENAME=FILENAME;
}
    BufferedWriter bw = null;

    FileWriter fw = null;

public void writeToFile (){

try {  

String content = "This is the content to write into file\n";

        fw = new FileWriter(FILENAME);

        bw = new BufferedWriter(fw);

        bw.write(content);

        System.out.println("Done");

} catch (IOException e) {

        e.printStackTrace();

    } finally {

        try {

            if (bw != null)
                bw.close();

            if (fw != null)
                fw.close();

        } catch (IOException ex) {

            ex.printStackTrace();

        }
    }

    }

}

'''

The Hangman class code is presented below:

'''java

import java.io.OutputStream;

import java.io.FileOutputStream;

public class Hangman {

public static void main(String[] args) {

// Your incredible code goes here...

if (args.length == 0) {

  System.out.println("Usage: java Hangman <answer>");

  System.err.println("answer is required");

  System.exit(1);

}

Game game = new Game(args[0]);

Prompter prompter = new Prompter (game);

WriteToFileExample1 write = new WriteToFileExample1 ("E:\\Java\\blablabla.txt");

while (game.getRemainingTries()>0 && !game.isWon()) {

  prompter.displayProgress();

  prompter.promptForGuess();

}

prompter.displayOutcome();

write.writeToFile();

} } '''

The program is running, however I don't see any file in E:\Java. Could you, please, help me ?

Balazs Peak
Balazs Peak
46,160 Points

try {

        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("test.txt"))));
        w.write("POTATO!!!");
        w.close();

    } catch (IOException e) {
        System.err.println("Problem writing");
    }