• Uncategorized

About java : java-syntax-file-difference-between-windows-and-linux

Question Detail

I am trying to access a file in linux. I have previously read the file from Windows. Where I did something like this:

import java.io.File;

public static File readFile = new File("D:\\test.w3");

This is probably a very silly question but how do I access that same file in the home directory or a sub folder?

public static File readFile = new File("//home//test.w3");

I’m getting the old 404 from Apache Tomcat 9.0.6
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

I might have located the main problem.
But i do not know how to resolve it.
In eclipse when selecting the right server as I seem to have run the wrong one I get the following error:

Tomcat server launch problem in eclipse

Any ideas where to begin the journey?
It is like if Eclipse reserves those ports and then refuses to disclose the rest. I have tried to change the port names but I get the same error with the new ports.

Question Answer

Good news, the File constructor has a form that can help. And there’s a System.property that maps to the HOME (that works across platforms)!

public static File readFile = new File(System.getProperty("user.home"), "test.w3");

It’s also worth noting that you can use / as a path separator on both *nix and Windows systems. There’s also File.separator if you want to manually build paths.

You can use

String path = java.nio.file.FileSystems.getDefault().getPath("").toAbsolutePath().toString(); // If the path is similar with project location, else replace with your path.
String fileName = "test.w3";

public static File readFile = new File(path + "/" + fileName);

On Linux you should use / instead of \\ from Windows.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.