Extracting a path from a complete filename?

G

Gunter Hansen

Assume I read (from a text file) into a string a file name which includes a prepended path
like

String fn = new String("D:\aaa\bbb\ddd\myfile.txt");

Is there a function which helps me to extract the path part of it similar to

System.out.println("Path=" + fn.getPathPart());

which yields

Path=D:\aaa\bbb\ddd\

Gunter
 
I

Igor Planinc

Gunter said:
Assume I read (from a text file) into a string a file name which includes a prepended path
like

String fn = new String("D:\aaa\bbb\ddd\myfile.txt");

You should escape the backslashes. The new String(...) is also not needed.
String fn = "D:\\aaa\\bbb\\ddd\\myfile.txt";
That is not too nice, though. Backslash is a file separator on Windows only.
Class File has four constants you can use to retrieve separators in a
platform-independent way:
static String pathSeparator
static char pathSeparatorChar
static String separator
static char separatorChar
Is there a function which helps me to extract the path part of it similar to

System.out.println("Path=" + fn.getPathPart());

File f = new File(fn);
String path = f.getPath();
 
S

Seamus

Gunter said:
Assume I read (from a text file) into a string a file name which includes a prepended path
like

String fn = new String("D:\aaa\bbb\ddd\myfile.txt");

Is there a function which helps me to extract the path part of it similar to

System.out.println("Path=" + fn.getPathPart());

which yields

Path=D:\aaa\bbb\ddd\

Gunter

fn.substring(0, fn.lastIndexOf(java.io.FIle.pathSeparatorChar));
 
T

Thomas Fritsch

Gunter said:
Assume I read (from a text file) into a string a file name which includes a prepended path
like

String fn = new String("D:\aaa\bbb\ddd\myfile.txt");
By the way:
(1) You have to write "D:\\aaa\\bbb\\ddd\\myfile.txt" because of the
special meaning of \ in strings.
(2) You can shorten new String("aaaa") to simply "aaaa".
Is there a function which helps me to extract the path part of it similar to

System.out.println("Path=" + fn.getPathPart());

which yields

Path=D:\aaa\bbb\ddd\
I am not sure if I understood you correctly. It seems you want
String fn = "D:\\aaa\\bbb\\ddd\\myfile.txt";
File file = new File(fn);
System.out.println("Path=" + file.getParent());
See <http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html>
 
A

Andrey Kuznetsov

String fn = new String("D:\aaa\bbb\ddd\myfile.txt");
Is there a function which helps me to extract the path part of it similar
to

System.out.println("Path=" + fn.getPathPart());

which yields

Path=D:\aaa\bbb\ddd\

File#getPath();
 
R

Rodrigo Zechin

import java.io.File;
....
File f = new File(fn);
String directory = f.isDirectory() ? f.getPath() : f.getParent();

RZR
 
M

Monique Y. Mudama

Assume I read (from a text file) into a string a file name which
includes a prepended path like

String fn = new String("D:\aaa\bbb\ddd\myfile.txt");

Is there a function which helps me to extract the path part of it
similar to

System.out.println("Path=" + fn.getPathPart());

which yields

Path=D:\aaa\bbb\ddd\

Gunter

Take a look at the javadocs for the class File, especially
getPath().
 
J

jonck

Is there a function which helps me to extract the path part

Not that I know of, but it would be very simple to write your own
though. If you need help writing such a function post back here and
I'll give you a hand.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top