Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code to delete file on Windows and Unix safely

Status
Not open for further replies.

Jefhandle

Technical User
Mar 9, 2005
69
DE
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
 
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();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top