Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Create File Path in Java

Using file.separator method in java.io.File package, you can create a file path. See the example below. 

 

package com.seleniummaster.examplefile;

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

public class CreateFilePath {

    public static void main(String[] args)
    {
        CreateAFilePath("myFile.txt");
    }

    public static void CreateAFilePath(String fileName)
    {
        try {
            //project directory
            String workingDirectory = System.getProperty("user.dir");
             String  filePath = workingDirectory + File.separator + fileName;

            System.out.println("Final file path : " + filePath);

            //create a file in the filepath

            File file = new File(filePath);

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

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 

Run the code and see the result below

Final file path : C:\seleniummaster\myFile.txt
File is created!

Process finished with exit code 0