Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Extracting a path from a complete filename?

Reply
Thread Tools

Extracting a path from a complete filename?

 
 
Gunter Hansen
Guest
Posts: n/a
 
      11-18-2005
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

 
Reply With Quote
 
 
 
 
Igor Planinc
Guest
Posts: n/a
 
      11-18-2005
Gunter Hansen wrote:
> 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();

> which yields
>
> Path=D:\aaa\bbb\ddd\
>
> Gunter
>

 
Reply With Quote
 
 
 
 
Seamus
Guest
Posts: n/a
 
      11-18-2005

Gunter Hansen wrote:
> 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));

 
Reply With Quote
 
mrandywarner@gmail.com
Guest
Posts: n/a
 
      11-18-2005
File file = new File(fn);
String parentPath = file.getParent();

 
Reply With Quote
 
Thomas Fritsch
Guest
Posts: n/a
 
      11-18-2005
Gunter Hansen schrieb:
> 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>

--
"Thomas:Fritsch$ops:de".replace(':','.').replace(' $','@')

 
Reply With Quote
 
nmarkon@gmail.com
Guest
Posts: n/a
 
      11-18-2005
how about something like this:

tmp.substring(0, tmp.lastIndexOf("\\"))

 
Reply With Quote
 
Steve W. Jackson
Guest
Posts: n/a
 
      11-18-2005
In article <437e007b$0$21950$(E-Mail Removed)-online.net>,
http://www.velocityreviews.com/forums/(E-Mail Removed) (Gunter Hansen) wrote:

> 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


See <http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html>.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
 
Reply With Quote
 
Andrey Kuznetsov
Guest
Posts: n/a
 
      11-18-2005
> 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();

--
Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-18-2005
On 18 Nov 2005 16:25:31 GMT, (E-Mail Removed) (Gunter Hansen)
wrote, quoted or indirectly quoted someone who said :

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


oops. \ must be doubled in strings.

See http://mindprod.com/jgloss/literals.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-18-2005
On 18 Nov 2005 16:25:31 GMT, (E-Mail Removed) (Gunter Hansen)
wrote, quoted or indirectly quoted someone who said :

>
>Is there a function which helps me to extract the path part of it similar to
>
>System.out.println("Path=" + fn.getPathPart());


see http://mindprod.com/jgloss/file.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
New Releases: Mr. & Mrs. Smith, Complete Buffy & Enterprise: Updated complete R1 DVD DB & info lists Doug MacLean DVD Video 0 08-09-2005 05:07 AM
Re: Request: A+ Complete 3rd edition (PDF) [1/1] - A+ Complete 3rd edition.txt (1/1) Spammy Sammy A+ Certification 0 03-04-2005 12:55 PM
New Releases: Criterion additions, WB Gangsters & Coupling: Complete: Updated complete downloadable R1 DVD DB& info lists Doug MacLean DVD Video 4 11-07-2004 05:20 PM
How to extract complete file path from traceback info Kenneth McDonald Python 2 06-21-2004 02:27 PM
Mr. Bean Complete Bean NOT complete!! Waterperson77 DVD Video 5 12-10-2003 05:22 PM



Advertisments