Sunday, 11 June 2017

Java Tutorial : Java IO (Java File - How to use File separator) ~ foundjava


Click here to watch in Youtube :

Click the below Image to Enlarge
Java Tutorial : Java IO (Java File - How to use File separator) 
FileDemo1.java
import java.io.File;
import java.io.IOException;
/*
 * Using File.separator construct the file path.
 */
public class FileDemo1
{

    public static void main(String[] args) throws IOException
    {
        String filename = "myfile.txt";
        String workingDirectory = System.getProperty("user.dir");

        String filePath = workingDirectory + File.separator + filename;

        System.out.println("filePath : " + filePath);

        File file = new File(filePath);

        if (file.createNewFile())
        {
            System.out.println("File is created!");
        }
        else
        {
            System.out.println("File is already existed!");
        }

    }

}
Output
filePath : D:\eclipse\workspace\JavaIODemo\myfile.txt
File is created!
FileDemo2.java
import java.io.File;
import java.io.IOException;

/*
 * Like below construct the file path.
 * File file = new File(workingDir, filename); 
 */
public class FileDemo2
{

    public static void main(String[] args) throws IOException
    {
        String filename = "myfile.txt";
        String workingDirectory = System.getProperty("user.dir");

        File file = new File(workingDirectory, filename);

        System.out.println("filepath : " + file.getAbsolutePath());
        
        if (file.createNewFile())
        {
            System.out.println("File is created!");
        }
        else
        {
            System.out.println("File is already existed!");
        }

    }

}
Output
filepath : D:\eclipse\workspace\JavaIODemo\myfile.txt
File is already existed!
FileDemo3.java
import java.io.File;
import java.io.IOException;

/*
 * Check the system OS and create file path manually, not recommend. 
 */
public class FileDemo3
{

    public static void main(String[] args) throws IOException
    {
        String filename = "myfile.txt";
        String workingDir = System.getProperty("user.dir");

        String absoluteFilePath = "";

        String os = System.getProperty("os.name").toLowerCase();

        if (os.indexOf("win") >= 0)
        {

            // if windows
            absoluteFilePath = workingDir + "\\" + filename;

        }
        else if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0
                || os.indexOf("mac") >= 0)
        {

            // if unix or mac
            absoluteFilePath = workingDir + "/" + filename;

        }
        else
        {

            // Unknown OS
            absoluteFilePath = workingDir + "/" + filename;

        }

        System.out.println("filepath = " + absoluteFilePath);

        File file = new File(absoluteFilePath);

        if (file.createNewFile())
        {
            System.out.println("File is created!");
        }
        else
        {
            System.out.println("File already exists!");
        }

    }

}
Output
filepath = D:\eclipse\workspace\JavaIODemo\myfile.txt
File already exists!
Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment