Write String to a File:
How can I save the contents of the "text" variable to a text file?
String File Example 1:
import java.i o.*;
public class WriteFile{
public static void main(String[]args){
File file = new File (”/home/Parvez/MyJavaBook” , ”MyText.txt”);
try{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new Buf feredReader(isr);
PrintWriter out = new PrintWriter(new FileWriter(file));
System.out.println (”Write String: ”);
String str = in.readLine();
out.println(str);
in.close();
out.close();
}catch(IOException e){
e.printStackTrac e ();
}
}
}
String File Example 2:
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class WriteToFileExample
{
public static void main(String[] args)
{
try
{
// Here we'll write our data into a file called
// sample.txt, this is the output.
File file = new File("sample.txt");
// We'll write the string below into the file
String data = "Learning Java Programming";
// To write a file called the writeStringToFile
// method which require you to pass the file and
// the data to be written.
FileUtils.writeStringToFile(file, data);
} catch (IOException e)
{
e.printStackTrace();
}
}
}
String File Example 3:
It's a simple example to write String to text file using Java code. Please note that you need the right to write in the target file.
package javafile;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @web http://java-buddy.blogspot.com/
*/
public class WriteStringToFile {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
FileWriter fileWriter = null;
try {
String content = "Hello! Java-Buddy :)";
File newTextFile = new File("C:/test/test.txt");
fileWriter = new FileWriter(newTextFile);
fileWriter.write(content);
fileWriter.close();
} catch (IOException ex) {
Logger.getLogger(WriteStringToFile.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fileWriter.close();
} catch (IOException ex) {
Logger.getLogger(WriteStringToFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
I am a beginner Java programmer attempting to make a simple text editor. I have the text from the text field in a String variable called "text".
How can I save the contents of the "text" variable to a text file?
String File Example 1:
import java.i o.*;
public class WriteFile{
public static void main(String[]args){
File file = new File (”/home/Parvez/MyJavaBook” , ”MyText.txt”);
try{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new Buf feredReader(isr);
PrintWriter out = new PrintWriter(new FileWriter(file));
System.out.println (”Write String: ”);
String str = in.readLine();
out.println(str);
in.close();
out.close();
}catch(IOException e){
e.printStackTrac e ();
}
}
}
String File Example 2:
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class WriteToFileExample
{
public static void main(String[] args)
{
try
{
// Here we'll write our data into a file called
// sample.txt, this is the output.
File file = new File("sample.txt");
// We'll write the string below into the file
String data = "Learning Java Programming";
// To write a file called the writeStringToFile
// method which require you to pass the file and
// the data to be written.
FileUtils.writeStringToFile(file, data);
} catch (IOException e)
{
e.printStackTrace();
}
}
}
String File Example 3:
It's a simple example to write String to text file using Java code. Please note that you need the right to write in the target file.
package javafile;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @web http://java-buddy.blogspot.com/
*/
public class WriteStringToFile {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
FileWriter fileWriter = null;
try {
String content = "Hello! Java-Buddy :)";
File newTextFile = new File("C:/test/test.txt");
fileWriter = new FileWriter(newTextFile);
fileWriter.write(content);
fileWriter.close();
} catch (IOException ex) {
Logger.getLogger(WriteStringToFile.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fileWriter.close();
} catch (IOException ex) {
Logger.getLogger(WriteStringToFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Pretty good post. I just came across your site and wanted to say that I’ve really enjoyed reading your posts. In any case I’ll be subscribing to your feed and I hope you will keep a good work!Cheer!
ReplyDeletesap online training
software online training
sap sd online training
hadoop online training
sap-crm-online-training