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

Sacred Thought

5 May 2024 Hari Om Verse 50-51, chapter two:  In this chapter two Shree krishna explains a simple way of living. Free from desires and void ...