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 trialRazvan Cirstea
2,493 PointsHow 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
46,160 PointsI 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
Balazs Peak
46,160 Pointstry {
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");
}
Razvan Cirstea
2,493 PointsRazvan Cirstea
2,493 PointsFirst, 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";
}
'''
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) {
}
prompter.displayOutcome();
write.writeToFile();
} } '''
The program is running, however I don't see any file in E:\Java. Could you, please, help me ?