Get file full path in java - java

When I pass File file to a method I'm trying to get its full path like file.getAbsolutePath(); I always get the same result no matter which one I use either absolute or canonical path PATH_TO_MY_WORKSPACE/projectName/filename and it is not there, how can I get exact location of the file?
Thank you
DETAILS:
Here is some code and this solutions(its bad but its working):
private static void doSomethingToDirectory(File factDir) throws IOException {
File[] dirContents = factDir.listFiles();
if(factDir.isDirectory() && dirContents.length > 0){
for (int i = 0; i < dirContents.length; i++) {
for (String str : dirContents[i].list()) {
if(str.equals(TEMP_COMPARE_FILE)){
process(new File(dirContents[i].getAbsolutePath() + "\\" + str));
}
}
}
}
}
I'm looping trough directories where factDir is src/main, I'm seeking toBeProcessed.txt files only that is TEMP_COMPARE_FILE value and I'm sending them to process method which reads the file and does processing of it.
If someone could better solution I'd be greatful

This quote from the Javadoc might be helpful:
A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.
I interpret this so that if you create your File object with new File("filename") where filename is a relative path, that path will not be converted into an absolute path even by a call to file.getAbsolutePath().
Update: now that you posted code, I can think of some ways to improve it:
you could use a FilenameFilter to find the desired files,
note that list and listFiles return null for non-directory objects, so we need an extra check for that,
you could also use listFiles() again in the inner loop, thus avoiding the need to create new File objects with hand-assembled paths. (Btw note that appending \\ manually to the path is not portable; the proper way would be to use File.separator).
The end result is
private static void doSomethingToDirectory(File factDir) throws IOException {
if (factDir.isDirectory()) {
for (File file : factDir.listFiles()) {
if (file.isDirectory()) {
for (File child : file.listFiles(new MyFilter())) {
process(child);
}
}
}
}
}
class MyFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return name.equals(TEMP_COMPARE_FILE);
}
}
Note that this code mimics the behaviour of your original piece of code as much as I understood it; most notably, it finds the files with the proper name only in the direct subdirectories of factDir, nonrecursively.

I think there is a way it may help you if and only if the file is in the program directory.
first you get the program directory by :
new File(".").getCanonicalPath()
then :
if file is inside a specific directory like folder\\filename
the full path will be
(new File(".").getCanonicalPath() + "\\folder\\filename")
or if file is directly inside the program directory:
the full path will be
(new File(".").getCanonicalPath() + "\\filename")
i wish this answer help you :)

Related

How to read the files inside a directory when Only Relative path of directory is known

In my java application,
1. I have relative path of the directory (Directory and files in it are part of the build).
2.The directory contains multiple files.
3. I want to read the file names in the parent directory.
4. Files can change later and are many in number, So I do not know the names of the files and Do not want my code to change if more files are added or removed or renamed
Now as I do not know the names of the files before hand as they may change later (there are multiple files which can vary according to environment). I only know about the relative path of the parent directory of the files.
How do I read the files ?
You can get list of all files of that directory by file.getlistFiles() method of file class.
It returns an array of files.
Even you can define filter for your files, so it returns exactly files that you want.
try {
File f = new File("D:/Programming");
FilenameFilter filter = new FilenameFilter() {
#Override
public boolean accept(File f, String name) {
// We want to find only .c files
return name.endsWith(".c");
}
};
// Note that this time we are using a File class as an array,
// instead of String
File[] files = f.listFiles(filter);
look at this example.
If you want to use relative path, You can use
System.getProperty("user.dir");
String relative Path =System.getProperty("user.dir");
it returns the folder that you put your app in it.
If your folder has some subfolders, you can simply use file.list();
it returns names of all files and folders of your directory .
String [] listOfMyFilesAndFolders =file.list();
you can add these names to your path to access another folders.
You can check your path is a file or is a folder by using
file.isDirectory();
for ( String path: listOfFilesAndFolders ) {
File file = new File(basePath+path);
if ( file.isDirectory() {
// it is a folder and you can use another for loop or recursion to navigate sub directories
} else {
// it is a file and you can do everyThing you want}}
I think that you can use recursion to walk in your sub directories use recursion to read more
I hope helps.
the question is badly asked and the text is misleading.
"I have relative path of the directory (Directory and files in it are part of the build)" means little and nothing, you have to clarify what you mean.
Assuming you get a relative directory (for example "/folder1/folder2") via command line parameter, you basically have 3 choices:
1) start the program with the directory in which the jar is located as the "current directory", so that it can be used as a working directory. This approach requires that when you launch your application via the java -jar myapp.jar command first you prepend a "cd" command to place yourself directly in the directory. The code will look like the #hamidreza75 solution but obviously you will not have the variable "D: / Programming" but directly the relative path of the directory in which to read the files.
launch script
#echo off
cd {jar-folder}
java -jar myapp.jar
java code:
package com.sample.stack;
import java.io.File;
import java.io.FilenameFilter;
public class FileRenamenter {
public static void main(String[] args) {
final String relativePath = "folder1/folder2";
File directory = new File(relativePath);
String[] list = directory.list(new FilenameFilter(){
public boolean accept(File dir, String name) {
// filter condition
return true;
}
});
// echo file list
for (String filePath : list) {
System.out.println(filePath);
}
}
}
2) pass the folder to be controlled via command line parameter, like this:
launch script
#echo off
java -jar {jar-folder}/myapp.jar {jar-folder}
java code (only the "directory" variable changes)
File directory = new File(args[0], relativePath); // note args[0]
3) programmatically find the folder in which the jar is running [very discouraged practice] and then concatenate the relative path:
java code (only the "directory" variable changes):
String jarFolder = ClassLoader.getSystemClassLoader().getResource(".").getPath();
File directory = new File(jarFolder, relativePath); // note jarFolder

Find Canonical Path like C:\Agent17-01\_work\50\s\Test Files

can someone help me to find Canonical Path like "C:\Agent17-01\_work\50\s\Test Files".
I have created below method to find Canonical path but it gave the path
"C:\Agent17-01\_work\50\Test Files".
\s\ is missing in my method. below is my method code
public static String getRelativePath(String FileName)
{
String path =
null;
File f = new File("..\\..\\Test Files\\" + FileName);
try
{
path = f.getCanonicalPath();
System.out.println(path);
}
catch (Exception e)
{
}
return path;
}
any help is appreciated
Thanks in advance.
Java accepts also "/" chars as directory dividers. Why is it important? - OS independence. My recomendation is to put the needed resource files into the src/main/resources folder, and then:
getClass().getClassLoader().getResource(fileName).getFile();
this code would return you File instance, and you would be sure that it won't relies on absolute path. If you need just File path, and you are not gonna perform IO operations, use the method you've used (getCannonicalPath).
BTW: Please use Java code style while writing in Java: (brackets, parameters naming). I would recomend also to read about Try with Resources construction.

renameTo() not working

I need to rename a file by replacing - with _ in the file name.
Suppose if a file name is ab-9.xml, it should be ab_9.xml.
renameTo() is not working for me. Is there any other way to do that? Here is my code:
File replaceCheracter(File file) {
File oldPath = new File(file.getPath())
String filePath = file.getPath()
if(filePath.contains("-")){
String newFilePath = filePath.replace("-", "_")
if(oldPath.renameTo(newFilePath)) {
System.out.println("renamed");
} else {
System.out.println("Error");
}
}
return oldPath
}
You should consider using the java.nio.file package:
final Path file = Paths.get("path\\to\\your-file.txt");
Files.move(file, file.resolveSibling(file.getFileName().toString().replace("-", "_")));
Used the very handy function Path#resolveSibling() as second argument for Files#move().
Explanation:
Path#resolveSibling() takes the directory path of the Path object it is called on, and swaps the last part (the actual file name) for the supplied argument (the new, modified file name in this case).
Using this behavior as second argument for Files#move() will result in a move where the source directory and the target directory are the same, thus it only renames the file.
See The Java Tutorials - File I/O for further information on this.
renameTo method accepts File as parameter not String
Change oldPath.renameTo(newFilePath) with
oldPath.renameTo(new File(newFilePath))

.java file path to class name

What's the most efficient way of getting the class(es) created on a .java file? I have the .java file path and I need to get the class full name.
I can only remember:
Compile it with JavaCompiler
Using the file text to parse it with Eclipse ASTParser (or another)
Infer the class name through part of the file path, but I don't know
if this works for every cases
Somehow, use a tool like javap (dind't really thought about this one)
EDIT
I have this file, located at C:\myfolder\MyClass.java (let's ignore package and folder association conventions):
package mypackage.mysubpackage;
public class MyClass
{
// my class implementation here
public class MyInnerClass
{
// my inner class implementation here
}
}
The full name of the classes declared in this file are:
mypackage.mysubpackage.MyClass
mypackage.mysubpackage.MyClass.MyInnerClass (I don't know if this
one it's correct, but let's pretend it is)
How can I get those class when I only have the .java file path (C:\myfolder\MyClass.java) ?
The only way to reliably obtain the names of the classes (mind that it may also define interfaces) files a .java file declares would be to really parse the java language contained in that file.
And even then you will need to know which compiler will be/has been used to compile the .java file, as a java compiler could use any naming convention it likes for anonymous classes (the Oracle compiler uses $1, $2..., but there is no strict need to mimic that behavior).
Considering these obstacles I believe its very hard to do from the .java files contents and simply impossible with the .java files path alone.
The most effective way is Class.forName().getName()
I have the .java file path and I need to get the class full name.
Which means, you know the path of .java file and you want the class name of each class file.
class Filter {
public static void main(String[] a) {
Filter f = new Filter();
String dirName = "D:\\Yourfolder\\"; // assuming your java file are located in D:\Yourfolder\
f.finder(dirName); // call the method for listing all the class file
}
public File[] finder(String dirName) {
File dir = new File(dirName);
return dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String filename) {
if(filename.endsWith(".class"))
{
System.out.println(filename);
}
return filename.endsWith(".class");
}
});
}
}
Replace dirName with your .java directory path.
One approach is to scan the directory tree where your Java source files are located, and for each file ending in ".java", you take its full folder path as a String and convert each dir separator to a '.' character. This will give you the fully qualified class name (FQCN). For example, if the path is: com\foo\fee\Foo.java, that becomes com.foo.fee.Foo.
Of course, this does not give you inner or nested classes and other advanced things, but these are created when you compile.
I have seen this kind of directory scanning in many frameworks, even Spring.
I am working on this in Groovy, so far I have:
File file = new File(rootSourcePath)
file.eachFileRecurse(FILES){
def path =it.getAbsolutePath()
println path
if(path.endsWith(".java")){
// to do the conversion here
}
}
Hope this interpreted your question correctly.
To get the name of the class file Try this
void printClassName(String classname)
{
System.out.println("The class name " + classname +" is " + classname.getClass().getName());
}

Java can't get the path of a file that exists in the current directory

If a file exists in the same directory where a Java application is running and I create a File object for that file the Java File methods for the path of the file include the filename as well. Code and output are below.
If this was a bug in the JDK version I'm using someone would surely have seen it by now.
Why do File.getAbsolutePath() and File.getCanonicalPath() include the file name? The Javadocs indicate that the directory name should be returned.
import java.io.File;
import java.io.IOException;
public class DirectoryFromFile {
private void getDirectoryOfFile(String fileName) throws IOException{
File f = new File(fileName );
System.out.println("exists(): " + f.exists());
System.out.println("getPath(): " + f.getPath());
System.out.println("getAbsolutePath(): " + f.getAbsolutePath());
System.out.println("getParent(): " + f.getParent() );
System.out.println("getCanonicalPath(): " + f.getCanonicalPath() );
System.out.println("getAbsoluteFile().getCanonicalPath(): " + f.getAbsoluteFile().getCanonicalPath() );
String dirname = f.getCanonicalPath();
System.out.println("dirname: " + dirname);
File dir = new File(dirname);
System.out.println("dir: " + dir.getAbsolutePath());
if (dirname.endsWith(fileName))
dirname = dirname.substring(0, dirname.length() - fileName.length());
System.out.println("dirname: " + dirname);
File dir2 = new File(dirname);
System.out.println("dir2: " + dir2.getAbsolutePath());
}
public static void main(String[] args) {
DirectoryFromFile dff = new DirectoryFromFile();
try {
dff.getDirectoryOfFile("test.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Here' the output:
exists(): true
getPath(): test.txt
getAbsolutePath(): C:\dean\src\java\directorytest\directory.from.file\test.txt
getParent(): null
getCanonicalPath(): C:\dean\src\java\directorytest\directory.from.file\test.txt
getAbsoluteFile().getCanonicalPath(): C:\dean\src\java\directorytest\directory.from.file\test.txt
dirname: C:\dean\src\java\directorytest\directory.from.file\test.txt
dir: C:\dean\src\java\directorytest\directory.from.file\test.txt
dirname: C:\dean\src\java\directorytest\directory.from.file\
dir2: C:\dean\src\java\directorytest\directory.from.file
So far the only way I've found to get the directory in this case is to manually parse off the file name.
Does the File class have a way to get the directory name in this case (where a File that exists in the current directory is created without specifying a directory)?
Why do File.getAbsolutePath() and File.getCanonicalPath() include the
file name? The Javadocs indicate that the directory name should be
returned.
No, they don't. If you'd care to point out why you think they do, someone can probably identify the mistake in your reasoning. Also, if you specify exactly what you'd like to see for output given some particular input, we can help you out there, too. Your question title seems strange, too, since your problem seems to be that it is returning the full path to a file.
Edit: I think I understand the source of your confusion. A File represents a file system path in a platform-agnostic way. It can be a path to a file or to a directory. It also always represents the same path, though not necessarily the same absolute path. This is a very fine distinction but a very important one. A File object representing a relative path is always relative. Given a File representing a relative path, you can get the current corresponding absolute path using getAbsolutePath(). This doesn't, however, alter the fact that the File represents a relative path. Further invocations of getAbsolutePath() on the same File object may return different values. Consider, for example:
// A relative file
File foo = new File("foo.txt");
// Resolve relative file against CWD
System.out.println(foo.getAbsolutePath());
// Output: D:\dev\projects\testbed\foo.txt
System.setProperty("user.dir", "C:\\somewhere");
// Resolve relative file against new CWD
System.out.println(foo.getAbsolutePath());
// Output: C:\somewhere\foo.txt
// Get an absolute file
File absoluteFoo = foo.getAbsoluteFile();
// Show absolute path
System.out.println(absoluteFoo.getAbsolutePath());
// Output: C:\somewhere\foo.txt
System.setProperty("user.dir", "D:\\somewhere-else");
// An absolute path doesn't change when the CWD changes
System.out.println(absoluteFoo.getAbsolutePath());
// Output: C:\somewhere\foo.txt
It should be clear now that the path a File represents is only that: a path. Further, a path can be composed of zero or more parts, and calling getParent() on any File gives back the path of that File with the last path element removed unless there isn't a "last path element" to remove. Thus the expected result of new File("foo").getParent() is null since the relative path "foo" has no parent.
From the example and explanation above, you should be able to see that the way to get the containing directory when you've created relative-path File object is with
String absoluteParentDirPath = someRelativeFile.getAbsoluteFile().getParent();
with the caveat that the "absolute path" depends on your environment at the time.
Additional note: Since File is Serializable, you could write a relative-path file to disk or send it across a network. That File, when deserialized in another JVM, will still represent a relative path and will be resolved against whatever the current working directory of that JVM happens to be.
The behaviour is expected. The documentation does not mention that the filename is not included.
Perhaps you are confused by the difference between getAbsolutePath() and getAbsoluteFile(). It's that the latter returns a File instance.
I'm not sure why you think the Javadoc says that it returns the directory name.
Here is the Javadoc --
An abstract representation of file and directory pathnames.
User interfaces and operating systems use system-dependent pathname strings to name files and directories. This class presents an abstract, system-independent view of hierarchical pathnames. An abstract pathname has two components:
An optional system-dependent prefix string, such as a disk-drive specifier, "/" for the UNIX root directory, or "\\" for a Microsoft Windows UNC pathname, and
A sequence of zero or more string names.
The first name in an abstract pathname may be a directory name or, in the case of Microsoft Windows UNC pathnames, a hostname. Each subsequent name in an abstract pathname denotes a directory; the last name may denote either a directory or a file. The empty abstract pathname has no prefix and an empty name sequence.
http://download.oracle.com/javase/6/docs/api/java/io/File.html#getAbsolutePath%28%29
Returns the absolute pathname string of this abstract pathname.
In addition to the existing answers with regards to getAbsolutePath and getCanonicalPath, please also note, that File.getParent() does not mean "parent directory" it merely refers to the parent file object that was used to create the file.
For example, if the file object can be created as such:
File dir = new File("/path/to/a/directory");
File f1 = new File(dir, "x.txt");
File f2 = new File(dir, "../another/y.txt");
File f3 = new File("z.txt");
f1 would refer to /path/to/a/directory/x.txt, it's parent is dir (/path/to/a/directory)
f2 would refer to /path/to/a/directory/../another/y.txt, it's canonical path would be /path/to/a/another/y.txt, but it's parent is still the reference to dir (/path/to/a/directory)
f3 would refer to z.txt in the current directory. It does not have a parent file object, so f3.getParent() or f3.getParentFile() would return null.
path is the full path
if you only want the directory you need to call file.getParent()

Categories