Find out really (!) the path / getParent() not working

L

Lars Willich

I pass a filename to a java program through the command line. E.g.

java myprogram myfile.dat

myfile.dat is in the same directory as myprogram. Hence myprogram has no difficulties to
find the file.

When I now want to find out the path of the file then the following does NOT work:


File myFile = null;
String myfilename = args[0]; // contains "myfile.dat"
myFile = new File(myfilename);
String filepath = myFile.getParent();
System.out.println("Path=" + filepath);



yields "null"

So how do I get the path otherwise ?

Alternatively: How do a program gets his own path (only path - not filename)?

Lars
 
T

Thomas Weidenfeller

Lars said:
File myFile = null;
String myfilename = args[0]; // contains "myfile.dat"
myFile = new File(myfilename);
String filepath = myFile.getParent();
System.out.println("Path=" + filepath);



yields "null"

So how do I get the path otherwise ?

First, there is no need to post such a trivial question to three
newsgroups, including clj.gui where it is off topic.

Second, the answer is in the API documentation, which you maybe want to
read before posting. Look at getAbsoluteFile().getParent().


/Thomas
 
M

Mark Thomas

Lars said:
I pass a filename to a java program through the command line. E.g.

java myprogram myfile.dat

myfile.dat is in the same directory as myprogram. Hence myprogram has no difficulties to
find the file.

When I now want to find out the path of the file then the following does NOT work:
"Returns the pathname string of this abstract pathname's parent, or null
if this pathname does not name a parent directory."
You don't name a parent directory when you say "myfile.dat".
File myFile = null;
String myfilename = args[0]; // contains "myfile.dat"
myFile = new File(myfilename);
String filepath = myFile.getParent();
System.out.println("Path=" + filepath);



yields "null" As the API says it will

So how do I get the path otherwise ?

Alternatively: How do a program gets his own path (only path - not filename)?

Lars
Try
String filepath = myFile.getAbsoluteFile().getParent();

Mark
 
T

Thomas Schodt

Thomas said:
Lars said:
File myFile = null;
String myfilename = args[0]; // contains "myfile.dat"
myFile = new File(myfilename);
String filepath = myFile.getParent();
System.out.println("Path=" + filepath);

yields "null"

First, there is no need to post such a trivial question to three
newsgroups, including clj.gui where it is off topic.

Also, clj.help would probably have been a better choice of FoT,
actually the post should never have made it outside clj.help.
Second, the answer is in the API documentation, which you maybe want to
read before posting. Look at getAbsoluteFile().getParent().

or getCanonicalFile().getParent()
 
P

Patricia Shanahan

Lars said:
I pass a filename to a java program through the command line. E.g.

java myprogram myfile.dat

myfile.dat is in the same directory as myprogram. Hence myprogram has no difficulties to
find the file.

When I now want to find out the path of the file then the following does NOT work:


File myFile = null;
String myfilename = args[0]; // contains "myfile.dat"
myFile = new File(myfilename);
String filepath = myFile.getParent();
System.out.println("Path=" + filepath);



yields "null"

So how do I get the path otherwise ?

Alternatively: How do a program gets his own path (only path - not filename)?

Lars

Look at the File methods, especially those for Canonical or Absolute
path/file.

Patricia
 
O

Oliver Wong

Lars Willich said:
I pass a filename to a java program through the command line. E.g.

java myprogram myfile.dat

myfile.dat is in the same directory as myprogram. Hence myprogram has no
difficulties to
find the file.

When I now want to find out the path of the file then the following does
NOT work:


File myFile = null;
String myfilename = args[0]; // contains "myfile.dat"
myFile = new File(myfilename);
String filepath = myFile.getParent();
System.out.println("Path=" + filepath);



yields "null"

So how do I get the path otherwise ?
http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#getAbsolutePath()


Alternatively: How do a program gets his own path (only path - not
filename)?

It's difficult. What do you plan to do with this information?

- Oliver
 
S

steve

I pass a filename to a java program through the command line. E.g.

java myprogram myfile.dat

myfile.dat is in the same directory as myprogram. Hence myprogram has no
difficulties to
find the file.

When I now want to find out the path of the file then the following does NOT
work:


File myFile = null;
String myfilename = args[0]; // contains "myfile.dat"
myFile = new File(myfilename);
String filepath = myFile.getParent();
System.out.println("Path=" + filepath);



yields "null"

So how do I get the path otherwise ?

Alternatively: How do a program gets his own path (only path - not filename)?

Lars

private static String getLocalURLDirName() {
//Get the current execution directory
String localDirName;
final java.net.URL myURL =
bootLoader.class.getResource(getClassName()); //Open a URL to the our .class
file



localDirName = (myURL.getPath()).replaceAll("%20", " "); //change %20
chars to spaces
if (localDirName.lastIndexOf(".jar")>0){
localDirName = localDirName.substring(0, localDirName.lastIndexOf("/"));
//clean off the file name

localDirName = localDirName.substring(0,
localDirName.lastIndexOf("/")); //clean off the file name
}
localDirName = localDirName.substring(0,
localDirName.lastIndexOf("/")); //clean off the file name


return localDirName;
}

where 'bootLoader' is the name of the .Jar you are currently in. (and should
be what you launched)
 
R

Roland de Ruiter

Lars said:
I pass a filename to a java program through the command line. E.g.

java myprogram myfile.dat

myfile.dat is in the same directory as myprogram. Hence myprogram has no difficulties to
find the file.

When I now want to find out the path of the file then the following does NOT work:


File myFile = null;
String myfilename = args[0]; // contains "myfile.dat"
myFile = new File(myfilename);
String filepath = myFile.getParent();
System.out.println("Path=" + filepath);



yields "null"

So how do I get the path otherwise ?

Alternatively: How do a program gets his own path (only path - not filename)?

Lars
Use
myFile.getAbsoluteFile().getParent()
or
myFile.getCanonicalFile().getParent()
See
<http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#getAbsoluteFile()>
<http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#getCanonicalFile()>
 
M

mail2alankar

Hi Lars,

Ok.. See your code is perfectly all right.
What the problem is in the argument passed i.e. you have to understand
the difference between absolute path and relative path.

for eg:
File fileName = new File("abc.java")
String name = fileName.getParent(); // name = null
as the file created has no parent, its relative

on the other hand
File fileName = new File("D:\java\abc.java")
String name = fileName.getParent(); // name = D:\java
as the file now has absolute path.

If you want to get the absolute path from the relative path then you
can do this

let args[0] = myFile.dat

File file = new File(args[0]);
file = file.getAbsolutePath(); // file=d:\java\myFile.dat

file = new File(file);
String path = file.getParent(); // path = d:\java

I hope this will solve the problem

-Alan
 
M

Mahesh

getParent() will work here.

file = new File("D:\\FolderX\\MyFile.java");
parentPath = file.getParent();

so u may need to get them in args[]
 
P

Paul Cager

Lars said:
I pass a filename to a java program through the command line. E.g.

java myprogram myfile.dat

myfile.dat is in the same directory as myprogram. Hence myprogram has no difficulties to
find the file.

When I now want to find out the path of the file then the following does NOT work:


File myFile = null;
String myfilename = args[0]; // contains "myfile.dat"
myFile = new File(myfilename);
String filepath = myFile.getParent();
System.out.println("Path=" + filepath);



yields "null"

So how do I get the path otherwise ?

Alternatively: How do a program gets his own path (only path - not filename)?

Lars

Have a look at the getCanonicalFile method:

http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html#getCanonicalFile()
 
M

mail2alankar

Hi Lars

The reason you are getting because you are passing up the relative path
for example let us take the following code

File myFile = new File("abc.java");
String path = myFile.getParent() // path=null as it is a relative path.

if we do it like this
File myFile = new File("D:/java/abc.java");
String path = myFile.getParent() // path=D:/java as it is a absolute
path.

Now if you want to find the parent path from the relative path, this is
what you can do.

File myFile = new File("abc.java");
myFile = new File(myFile.getAbsolutePath());
String path = myFile.getParent(); //this will have the path.
System.out.println("Path=" + path);

I hope this solves your problem, if there is more please let me know.

-Alan
 
D

Dieter Schicker

So how do I get the path otherwise ?

File f = new File(args[0]);
String abspath = f.getAbsolutePath();
String path = abspath.substring(0, abspath.indexOf(args[0]));
System.out.println(path);

Didi
 
M

mail2alankar

Hi Lars

What you are doing is correct the only thing is that you are getting
the relative path. for ex.

File fileName = new File("abc.java");
String path = fileName.getParent(); //path = null

File fileName = new File("D:/test/abc.java");
String path = fileName.getParent(); //path = D:/test

Now if you want to get absolute path from relative path you can do
this.

File fileName = new File(args[0]);
fileName = fileName.getAbsolutePath();
fileName = new File(fileName);
String path = fileName.getParent();

I hope this will solve your problem.
If there is anything more, let me know.

-Alan
fileName = new File()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top