File file = new File("C\\Examples\\file1.txt");
Path covertedPath = file.toPath();
System.out.println("The converted path is " + covertedPath);
File parent = new File("C:\\Examples");
//so here we created instance of parent that is use to invoke the resolve method
// and we pass it as argument to new File and child part of file as other parameter
File resolvedFile = new File(parent, "dir\\file.txt");
// and we use resolvedFile method to get the path
System.out.println("The resolved path is " + resolvedFile.toPath());
Another way of doing it is like below. Here we are passing both parent and child as String and we get same results
resolvedFile = new File("C:\\Examples", "dir\\file.txt");
System.out.println("The resolved path is " + resolvedFile.toPath());
Both methods used above are also equivalent to doing something like below
Path parentPath = Paths.get("C:\\Examples");
Path childRelativePAth = Paths.get("dir\\file.txt");
System.out.println("The resolved path is " + parentPath.resolve(childRelativePAth));
I/o methods to rename and delete files are File.renameTo() and File.delete()
File workingDirectory = new File("").getAbsoluteFile();
System.out.println("The current working directory is "+workingDirectory.getAbsolutePath());
File dir2File = new File(workingDirectory,"\\FileTree1\\Dir2");
String[] dir2Contents = dir2File.list();
for(int i = 0;i<dir2Contents.length;i++){
System.out.println("i="+i+" : "+dir2Contents[i]);
}
Now lets do the same using listFiles method. This method will also only print one level deep that is it will print Dir3 but not anything inside Dir3.
File[] dir2Files = dir2File.listFiles();
for(int i = 0;i<dir2Files.length;i++){
//only major difference in list and listFiles method is here we are using getName
//method to print and it returns the last segment of filePath which in case in the file name
System.out.println("i="+i+" : "+dir2Files[i].getName());
}
We should use java.nio methods when working with FileSystems ,but when it is reading and writing file contents sometimes java.io is better choice