Wednesday, April 1, 2020

Writing a String to a txt file using Java

//Put this at the top of the file:
import java.io.*;
import java.util.*;
BufferedWriter out = new BufferedWriter(new FileWriter("test.txt"));
//Add this to write a string to a file
//
try {

    out
.write("aString\nthis is a\nttest");  //Replace with the string
                                            
//you are trying to write
}
catch (IOException e)
{
   
System.out.println("IO Exception occurred: "+ e);
}
finally
{
    out
.close();
} source: stackoverflow https://stackoverflow.com/questions/10390254/how-do-you-write-a-string-to-a-text-file

No comments:

Post a Comment

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows