Syntax error File Path with double slash in Java

File Path with double slash in Java



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);
Updated on: 2019-07-30T22:30:24+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements