#Need help with creating a save and load method
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
Sure, I can help you with creating a save and load method in Java.
To save data to a file, you can use the FileWriter or BufferedWriter class. Here's an example of how you can implement a save method:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class DataSaver {
public void saveData(String data, String filename) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
writer.write(data);
} catch (IOException e) {
e.printStackTrace();
}
}
}
In this example, the saveData method takes two parameters: data (the data you want to save) and filename (the name of the file to save the data to). It uses a BufferedWriter wrapped around a FileWriter to write the data to the file.
To load data from a file, you can use the FileReader or BufferedReader class. Here's an example of how you can implement a load method:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class DataLoader {
public String loadData(String filename) {
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append(System.lineSeparator());
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
In this example, the loadData method takes one parameter: filename (the name of the file to load the data from). It uses a BufferedReader wrapped around a FileReader to read each line of the file and append it to a StringBuilder. Finally, it returns the accumulated data as a string.
You can then use these methods in your program like this:
public class Main {
public static void main(String[] args) {
DataSaver saver = new DataSaver();
DataLoader loader = new DataLoader();
String data = "Hello, world!";
String filename = "data.txt";
// Save data to file
saver.saveData(data, filename);
// Load data from file
String loadedData = loader.loadData(filename);
System.out.println("Loaded data: " + loadedData);
}
}
This example saves the string "Hello, world!" to a file named "data.txt" using the saveData method, and then loads the data from the same file using the loadData method. Finally, it prints the loaded data to the console.
whats the problem you're having? so far all you've posted was basically an assignment, "i need this done"
my code isnt working
like i got it to save
but it saves this
and when i try to load it doesnt load anything
don't use object streams
you're reading/writing objects. you should be using writeUTF and readUTF to read/write strings
actually, don't use those methods
their naming is confusing, they are not for writing utf/unicode strings
they are for writing in a very specific custom utf encoding that is not what u would expect
it does write utf, just in a modified way than standard, with appending the length to the end. using writeChars requires the dev to manually handle the length
using writeUTF with readUTF will produce the proper string output
yes
but opening it in a text editor and expecting readable text, not so much. just saying
so how shud i do it?
the writing and loading
you could use Files.write(Path, Iterable) and Files.readAllLines(Path), which would be the modern way of reading & writing
hmmmm
why does it need to be readable text?
you are using object streams
ok no it doesnt need to be readable
if you need to process each line, you could use Files.lines(Path), just remember to close it after
then writeUTF is perfectly fine
even when needing to read the file, writeUTF is fine for most situations
but its recommended to use the more modern Files API
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
ill try it out and see how it goes
i got a feeling imma fuck it up lmao
uhh it aint working
im dumb
i want it to be readable when i click the file

are you getting an error?
?
"it aint working" doesnt give us much to work with, when it comes to helping
writeUTF is sstill usable even if i want the txt file to be readable?
try it
whats the problem with using Files?