SuperStar
SuperStar

Reputation: 495

Take a file path from commandline and use it in a "file" constructor?

I want my code to take a file path - C:\games\mario.txt (windows) or C:/games/mario.txt (linux). I want to use this path inside a "file" constructor like FileInputStream("");

If the path is windows style, then it needs to be modified by adding \ after every \, ie FileInputStream("C:\\games\\mario.txt");

Is there any API to validate such paths and then prepare them for use in a "file" constructor ?

Upvotes: 0

Views: 82

Answers (2)

sanbhat
sanbhat

Reputation: 17622

You can use System.getProperty("file.separator") to know whether the underneath OS uses / or \ to separate paths.

Upvotes: 0

AllTooSir
AllTooSir

Reputation: 49432

You can also use FileNameUtils#separatorsToSystem(). It is in Apache Commons API.

In Java 7 , you can make use of the Paths class.

String path = Paths.get("C:/games/mario.txt").toString();

Upvotes: 1

Related Questions