Monday, May 7, 2012

Java code snap - Create a File

package com.example.javaIO

import java.io.File;
import java.io.IOException;

public class CreateFile
{
public static void main( String args[] )
{
try {

File file = new File("/home/user/readme_IO.txt");

if (file.createNewFile()){
    System.out.println("JAVA IO - Created a File - /home/user/readme_IO.txt");
}else{
    System.out.println("JAVA IO - Created a File faild - /home/user/readme_IO.txt");
}

} catch (IOException e) {
System.out.println("JAVA IO - Catch Exception");
e.printStackTrace();
}
}
}

Simple program java program to create a file on server.

This example creates a file which is called readme_IO.txt file on server location("/home/user/readme_IO.txt"). It use createNewFile method to create a file, it return true if the file is created otherwise returns false.

References

http://docs.oracle.com/javase/7/docs/api/java/io/File.html
http://docs.oracle.com/javase/7/docs/api/java/io/File.html#createNewFile()

No comments:

Post a Comment