#Using a FileWriter within an object oriented project

1 messages · Page 1 of 1 (latest)

sweet bear
#

I have read and watched several videos on how to write and read from files in java.
i have dont some exercises but i am really struggling with what my steps should be in my assignment. Id really appreciate any tips in the right direction.
(i am using eclipse)

echo umbraBOT
#

<@&987246399047479336> please have a look, thanks.

echo umbraBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

sweet bear
#

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.

left ocean
#

just use the utility class Files

#

it got several method for writing/reading of an File etc

sweet bear
sweet bear
echo umbraBOT
#

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
twin gorge
sweet bear
#

my textbook didnt really show how to create a file using java

echo umbraBOT
#

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 👍