INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Code to delete file on Windows and Unix safely

Code to delete file on Windows and Unix safely

Code to delete file on Windows and Unix safely

(OP)
Hi
I have a JAVA SE Code which delete a file on the file system
under windows.
I need to get the path and file name separately. Therefore do something like this and I get them:
String path = C:\\a\\b\\file.xml
String newPath= path.substring(lastIndexOf("\\"));
String fileName=path.substring(lastIndexOf("\\"),path.length() );

Question:
How should I change the code , that works on Unix systems too
As You know on linux is the separator not "\"  and which issues I should consider to delete safely on file system??
Thanks for your help  

RE: Code to delete file on Windows and Unix safely

A few rules:
- Do not use drive letters, as Linux doesn't have that
- Use / instead of \, as Windows fully supports that from all NT based versions and up (NT, 2000, XP, Vista, W7, Server 2003 and Server 2008)
- Getting the filename from a path should be done using like this (found in the web):

CODE

String path = "C:/Programfiles/hello/abc.txt";
String fileName = new File(path).getName();

RE: Code to delete file on Windows and Unix safely

You can use the pathSeparator field of String class.

What do you mean with safely?

Cheers,
Dian

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close