Hey there,

I'm new to java and I need to write a string that i generated in my code to an output file. I'm currently writing the string to the standard output like so :

System.out.println("K_"+Receiver+",t"+msgnum+"(R("
+Receiver+",t"+msgnum+"("+out1+")))");

All the types e.g. 'Receiver' or 'out1' are of type string. Could anyone show me how to open a file for writing and then write this information to the file? Is there some sort of file writer equivalent for system.out.println()? Thanks very much

Dani AI

Generated

The simplest, safest way to write your generated string to a file is to use the java.nio.file API and try-with-resources so the stream is always closed for you. was printing to System.out; pointed at FileWriter (missing the close() call in the snippet) and suggested PrintStream — both approaches work, but the examples in the thread either omitted error handling or used awkward construction. Below are two modern, reliable patterns you can apply immediately.

A compact Java 11+ option that appends a line (creates the file if needed):

Path out = Path.of("output.txt");
Files.writeString(out, message + System.lineSeparator(),
                  StandardOpenOption.CREATE, StandardOpenOption.APPEND);

A Java 7+ style using a BufferedWriter (gives control over encoding and write/line APIs):

Path out = Paths.get("output.txt");
try (BufferedWriter bw = Files.newBufferedWriter(out, StandardCharsets.UTF_8,
                 StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
    bw.write(message);
    bw.newLine();
}

Notes and troubleshooting: use an absolute path if the working directory is unclear; prefer StandardCharsets.UTF_8 to avoid platform-dependent encoding; handle or declare IOException; APPEND vs TRUNCATE_EXISTING controls whether you add or overwrite; try-with-resources avoids forgetting close() (the bug in 's snippet). For atomic replace of existing files, write to a temp file then Files.move(...) with ATOMIC_MOVE. For more API details see the Java IO tutorial (https://docs.oracle.com/javase/tutorial/essential/io/) and the Files class docs (https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html).

Recommended Answers

All 2 Replies

Hey there,

I'm new to java and I need to write a string that i generated in my code to an output file. I'm currently writing the string to the standard output like so :

System.out.println("K_"+Receiver+",t"+msgnum+"(R("
+Receiver+",t"+msgnum+"("+out1+")))");

All the types e.g. 'Receiver' or 'out1' are of type string. Could anyone show me how to open a file for writing and then write this information to the file? Is there some sort of file writer equivalent for system.out.println()? Thanks very much

You can read about File streams of java :
Anywayz I give you some code ... You have to write exceptions yourself:

String s ="this is the string you want to write to the file.";
File name = new File("sani.txt");
FileWriter fw = new FileWriter(name);
fw.write(s);
fw.close;

Hey there,

I'm new to java and I need to write a string that i generated in my code to an output file. I'm currently writing the string to the standard output like so :

System.out.println("K_"+Receiver+",t"+msgnum+"(R("
+Receiver+",t"+msgnum+"("+out1+")))");

All the types e.g. 'Receiver' or 'out1' are of type string. Could anyone show me how to open a file for writing and then write this information to the file? Is there some sort of file writer equivalent for system.out.println()? Thanks very much

u can use PrintStream out = new PrintStream(new OutputStream(new FileOutputStream(new File("Name of ur file"))));
now u can use same line for wrting into file
like this
out.println("K_"+Receiver+",t"+msgnum+"(R("
+Receiver+",t"+msgnum+"("+out1+")))");

it will write to the file don't forgot to import import java.io.*;
if any doubts please revert

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.