This post will expain you the two classes - FileWriter and BufferedWriter and how to write java program to write to a file.
Class FileWriter - The FileWriter is a class used for writing character files.
Class BufferedWriter - The BufferWriter class is used to write text to a character-output stream, buffering characters. It will help us to provide for the efficient writing of single characters, arrays, and strings.
Find the code below of java program to write text to a file:
import java.io.*;
class FileWrite
{
public static void main(String args[])
{
try{
// Create file
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
Saturday, June 12, 2010
Thursday, November 26, 2009
Example to Determine if a File or Directory Exists
import java.io.*;
public class filedirexists{
public static void main(String args[])
{
File filedir=new File("File or Directory exists or not!!")
boolean exists = filedir.exists()
if (!exists)
{
System.out.println("The File/Directory you are looking for is not found")
}
else
{
System.out.println("The File/Directory you are looking for available")
Subscribe to:
Posts (Atom)