Share on Google+Share on Google+

How to make a file in java

Ads
This tutorial demonstrate how to make a file and write text in java. Here a text file is made using the File Class.

Description:

This example demonstrate how to make a file and write string in it. To create a file in java we use the File Class. The instance of the File class is immutable that means the pathname represented by the File object will never change.

 Code:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FilePermission;
import java.util.PropertyPermission;

public class WriteToFile {

  public static void main(String[] args) {
    try {
      File makefile = new File("output.txt");
      FileWriter fwrite = new FileWriter(makefile);
      fwrite.write("This is ");
      fwrite.write("a example");
      fwrite.write("of FileWriter");
      fwrite.flush();
      fwrite.close();
    catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Output:

It will create a file name ouput.txt at the current directory. The output.txt file have "This is a example of FileWriter" content written inside. 

How to check a file exists or not in java.
This tutorial demonstrate how to check for a file exists or not in java.
 
How to check a file is hidden or not in java
This tutorial demonstrate how to check a file is hidden or not in java.
 
How to check a file is read-able or not in java
This tutorial demonstrate that how to rectify a file whether it have read permission or not.
 
How to check a file is write-able or not in java
This tutorial demonstrate that how to rectify a file tha have write permission or not.
 
How to make directory in java
This tutorial demonstrate how to make directory at specified location. This example will make a directory.
 

Advertisements

Ads

Posted on: April 12, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Ads

Advertisement