how to write a string in a file in java




Asked on June 24, 2015
how to write a string in a file in java


Replied on June 24, 2015
Using java io package you can create string to a file. To do this you need to import the import java.io.FileWriter; package in your code. See the example

import java.io.FileWriter;

public class WriteAFile {
public static void main(String[] args) {
try {
FileWriter writer  = new FileWriter("D:/Eclipse_Workspace/SerializationFile/WriteAFile.txt");  // path to save the file
writer.write("First file to write");  // string that you want to write in your file
writer.close();
System.out.println("Please check the file directory");
} catch (Exception ex) {
ex.printStackTrace();
}
}

}




Replied on June 24, 2015
To write a String
fileWriter.write("My first String to save");


Write Answer











©2024 concretepage.com | Privacy Policy | Contact Us