Some times we need to create a File,using java.Here is the code
CreateFile.java
----------------------------------------------------------------------------------------------------
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class CreateFile
{
public static void main(String[] args) throws IOException
{
boolean msg=false;
File f = new File("c:/myfile.txt");
//check if file is not present
if (!f.exists()) msg=f.createNewFile();//returns boolean
System.out.println("File Created ?"+msg);
FileWriter fstream = new FileWriter("c:/myfile.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello World2");
out.close();
System.out.println("File Written Successfully");
}
}
-----------------------------------------------------------------------------------------------
CreateFile.java
----------------------------------------------------------------------------------------------------
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class CreateFile
{
public static void main(String[] args) throws IOException
{
boolean msg=false;
File f = new File("c:/myfile.txt");
//check if file is not present
if (!f.exists()) msg=f.createNewFile();//returns boolean
System.out.println("File Created ?"+msg);
FileWriter fstream = new FileWriter("c:/myfile.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello World2");
out.close();
System.out.println("File Written Successfully");
}
}
-----------------------------------------------------------------------------------------------
No comments:
Post a Comment