File Path with double slash in Java

Java 8Object Oriented ProgrammingProgramming

The double slash in the file path is required as to create the ’\’ character , another ‘\’ needs to be added to escape it. A program that demonstrates this is given as follows −

Example

 Live Demo

import java.io.File;
public class Demo {
   public static void main(String[] args) {
      File file = new File("C:\\jdk11.0.2\\demo1.txt");
      System.out.println(file);
   }
}

The output of the above program is as follows −

Output

C:\jdk11.0.2\demo1.txt

Now let us understand the above program.

The file path of the file is provided with double slash and then it is printed. A code snippet that demonstrates this is given as follows −

File file = new File("C:\\jdk11.0.2\\demo1.txt");
System.out.println(file);
raja
Published on 25-Feb-2019 16:20:45
Advertisements