Use:
Paths.get(...).normalize().toString()
Another solution woul be:
Paths.get(...).toAbsolutePath().toString()
However, you get strange results: Paths.get("/tmp", "foo").toString() returns /tmp/foo here. What is your filesystem?
Use:
Paths.get(...).normalize().toString()
Another solution woul be:
Paths.get(...).toAbsolutePath().toString()
However, you get strange results: Paths.get("/tmp", "foo").toString() returns /tmp/foo here. What is your filesystem?
To complete the the fge's answer, I would add some info:
normalize()simply removes redundant pieces of strings in your path, like.or..; it does not operate at the OS level or gives you an absolute path from a relative pathtoAbsolutePath()on the contrary gives you what the name says, the absolute path of thePathobject. But...toRealPath()resolves also soft and hard links (yes they exists also on Windows, so win user, you're not immune). So it gives to you, as the name says, the real path.
So what's the best? It depends, but personally I use toRealPath() the 99% of the cases.
As pointed out by Roberto Bonvallet, since toRealPath() throws an exception if the file does not already exist because, for example, you want to create it. In this case I prefer toAbsolutePath().
Source: Official javadoc
Videos
Nearly every major programming language has a library to handle the directory separators for you. You should leverage them. This will simplify your code and prevent bugs.
In my experience, the usual reason for combining strings like this is that they come from different sources. Sometimes it's different pieces from a configuration file. Sometimes it's a constant combining with a function argument. In any and all cases, when they come from different sources, you have to consider several different possible cases regarding the separators on the ends to be combined:
- Both ends could have a separator:
"images/"and"/sounds" - Only one has a separator:
"images"and"/sounds"or"images/"and"sounds" - Neither has a separator:
"images"and"sounds"
The fact each part comes from a different source means each source might have its own ideas about what conventions to follow, if someone gave any thought to it at all! Whatever is calling your code should not have to worry about this. Your code should handle all cases because someone will violate your convention. This will result in wasted time investigating the cause of an error and making a fix. I have had several unpleasant occasions where a coworker made an assumption about how paths should be formatted in a configuration file, meaning I had to go hunt down the code and figure out what they were expecting (or fix the code).
Most major languages provide a method to do this for you that already handles many of the cases:
os.path.joinfor PythonFile.joinfor RubyPath.joinfor Node.jsPaths.getfor Java (7 and up)Path.Combinefor .NETfilesystem::path.operator+for C++17
There is a caveat with these. A number of these seem to assume that a leading directory separator in the second argument refers to a root path and that this means the first argument should be dropped entirely. I don't know why this is considered useful; for me, it just causes problems. I've never wanted to combine two path portions and end up with the first part being dropped. Read the documentation carefully for special cases, and if necessary, write a wrapper that does what you want with these instead of their special handling.
This additionally helps if you have any need for supporting different operating systems. These classes almost ubiquitously account for choosing the correct separator. The libraries usually have a way of normalizing paths to fit the OS conventions, as well.
In the event that your programming language does not have a readily available library, you should write a method that handles all these cases and use it liberally and across projects.
This falls into the category of "don't make assumptions" and "use tools that help you."
In Java, the answer would be "neither of the above". Best practice would be to assemble pathnames using the java.io.File class; e.g.
File assets = new File("images");
File sounds = new File(assets, "sounds");
The File class also takes care of platform-specific pathname separators.
There is a separate issue of whether your pathname should start with a slash or not. But that is more to do with correctness than best practice. A pathname that starts with a slash means something different to a pathname that doesn't!!
There isn't explicit support for pathname handling in the core (ECMA) Javascript library, but (at least) Node.js provides support via the Path module.
The main point in order to your code doesn't work is that you don't re-assign the return value of resolve() method to your path variable, since the method returns a new object.
In order to build your Paths, you can use something like this:
documentsPath = Paths.get(string.format("C:\\Users\\%s\\%s", username, "Documents");
If you want to reuse some code, you can use an array of folders and create them:
List<Path> paths = new ArrayList();
String[] defaultFolders = {"Documents", "Desktop", "Music"};
foreach (folder : defaultFolders) {
paths.add(Path.get(string.format("C:\\Users\\%s\\%s", username, folder)));
PS: Since you're developing that in java, you should consider to make the Path's UNIX or Windows Compatible, since UNIX environments doensn't recognize the "C:/Users" path.
The resolve methods returns a Path
public void copyDocumentsFolder() throws IOException
{
Path newDir = Paths.get("C:\\Users\\ryanb\\Documents\\TestCopy");
documentsPath = documentsPath.resolve(username + "\\Documents");
networkFolder = networkFolder.resolve(foldername + "\\Backup");
System.out.format("%s%n", documentsPath);
System.out.format("%s%n", networkFolder);
System.out.println(networkFolder.getFileName());
Files.move(documentsPath, networkFolder.resolve(documentsPath.getFileName()));
System.out.println(newDir.toString());
}
You need to escape the backslash in your windows path, like so:
Copyprivate static final String BasePath = "C:\\imgs";
You can also just use a forwardslash:
Copyprivate static final String BasePath = "C:/imgs";
You can use File.separator and then you don't have to worry about platform specific path separators. e.g. below
Copyprivate static final String BASE_PATH = "C:" + File.separator + "imgs";
More detail can be found here
Dear Driftwood HR,
Greetings! Thank you for posting in Microsoft Community. We are happy to help you.
Generally, we can use steps below to uninstall the Teams App on Windows computer:
Open Windows Settings > Apps > Installed apps> search for Teams App, then click the 3 dots next to it and choose Uninstall.
Then reinstall Teams by downloading package from this support page: Download Microsoft Teams Desktop and Mobile Apps | Microsoft Teams
If you meet any error message when trying to reinstall Teams, may I know if there is any error message? If yes, please share a screenshot of it. I will further help you.
Look forward to your reply. Please understand that our initial reply may not always immediately resolve the issue. However, with your help and more detailed information, we can work together to find a solution.
Thanks for your cooperation and understanding. I hope that you are keeping safe and well!
Sincerely,
Tina | Microsoft Community Moderator
Hi Tina Chen,
Good day..!!
I've uninstalled Teams from VDI server 2019 standard in our org. Now, i'm unable to re-install the teams using teamsbootstrapper.exe -p command with MSIX file. this command is not executing in command prompt(admin).
I have tried using powershell using Add Appx package Command. but deployment failed with HResult :0x80073CFF.
I'm unable to install teams in that server. Please advise few methods to install teams.
Thanks in Advance.