Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make sure that your files will be read on both linux/windows

Tags:

java

I'm currently using this to read my files as inputStream, it works under windows, will this work in LINUX?

File file = new File(currentDirectory.getCanonicalPath().toString() + "\\" + "myfile.txt");

If not what is the right way to read files, this "\\" looks kind of "fishy"

ADD-ON

I forgot this :

File currentDirectory = new File(".");
like image 668
London Avatar asked Jan 26 '26 11:01

London


1 Answers

Instead of hard-coding the filename separator, use File.separator there.

Better yet, use:

File file = new File(currentDirectory, "myfile.txt");
like image 65
Pointy Avatar answered Jan 28 '26 01:01

Pointy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!