#Using a FileWriter within an object oriented project
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
package games.utilities;
import java.io.*;
public class FileManager {
public synchronized static void writeToFile(String saveState, String fileName) throws IOException {
//TODO write a string to a new file synchronously
try (//create a template for the objects to create files in ){
// FileWriter fr = new FileWriter(fileName);
// fr.write(saveState); creating a filewriter, witing my tostring for each object
// fr.close(); closing file
// handle exceptions with a try-with-recources block (I don't really understand this too well from what I've seen eclipse shows where exceptions occur and you can catch them that way. IOException is the only one I'm getting currently, I just added it at the top for right now)
}
}
public static void writeToFileAsync(String saveState, String fileName ) {
//TODO write a string to a new
file asynchronously
//this part is provided for me as a guide
new Thread() {
public void run() {
try {
writeToFile (saveState,fileName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}
here is my code for my class which objects should use i included my thoughts on what i should do as notes
this is my code to set the objects in the files
FileManager.writeToFileAsync(TicTacToe.toString(), "ttt.txt");
FileManager.writeToFileAsync(ConnectFour.toString(), "c4.txt");
FileManager.writeToFileAsync(MasterMind.toString(), "mm.txt")
i can provide more information if needed
Just looking to see if my thoughts are correct or if I'm thinking about it wrong.
just use the utility class Files
it got several method for writing/reading of an File etc
Do you think my thoughts so far are right? Or am I missing something?
I'll look into that thank you
File IO in Java should be done preferably with NIO (Java 7+), revolving around the classes Files and Path; and not with the old interface File, BufferedReader, FileReader and similar.
NIO is simple to use. The path to a file is represented using the Path class:
Path path = Path.of("myFile.txt");
All file operations can be found in the Files class:
// Reading
List<String> allLines = Files.readAllLines(path);
// or as a single string
String content = Files.readString(path);
// or with a stream
try (Stream<String> stream = Files.lines(path)) {
stream.forEach(System.out::println);
}
// Writing
Files.write(path, lines);
// or as a single string
Files.writeString(path, "hello world");
// or with extra options
Files.writeString(path, "hello world",
StandardOpenOption.WRITE,
StandardOpenOption.CREATE,
StandardOpenOption.APPEND);
If you need more control over the process, you can fallback to the old interface, but prefer using the bridge methods from NIO (Files.newBufferedReader, Files.newInputStream, path.toFile() and similar) to benefit from advantages such as correct encoding and better error detection.
Here is a simple example of how to read a file line-wise with the old interface
try (BufferedReader br = Files.newBufferedReader(path)) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
it is way more verbose than NIO but it gives more control.
You must not forget to close file handles, even in all exceptional cases. Closing a handle manually is very hard, which is why you should always use try-with-resources for this to let Java automatically close the handle for you:
try (SomeResource resource = ...) {
...
} // Automatically closed here, even in exceptional cases
The try with resources is to ensure that the writer is correctly closed, and so calling close() is useless since the try with resources already handles this
And as Squid and Quinteger showed, you should use Files class instead
ill look into the files class, i dont know if im allowed to use that in my project but ill give it a shot. thanks for clearing up the try with recources .
my textbook didnt really show how to create a file using java
Closed the thread due to inactivity.
If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍