Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 12-15-2004, 10:35 AM   #1 (permalink)
freesoft_2000
Code Monkey
 
Join Date: Oct 2004
Posts: 51
freesoft_2000 is on a distinguished road
Question Files

Hi everyone,

I have a rather silly question but please bear with me for a while. If lets say i have a string which has the contents

Code:
    "C:\\p\\kol\\pki.txt"
The thing with the above string is that if i use as an argument in a
fileinputstream it will only work on windows platform.

Basically my question is how do i convert the above string to a platform independent form.

I hope someone can help me with this

Thank You

Yours Sincerely

Richard West
freesoft_2000 is offline   Reply With Quote
Old 12-15-2004, 02:14 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,166
Belisarius is on a distinguished road
To some extent, you can't. "C:" is a uniquely Windows concept. If you're working with relative files, however (where you aren't giving an absolute path), you can use "File.separator" to get the proper seperator character for the given operating system. So, if you're on a Unix machine, you would get "/", whereas on Windows you're get "\\".
__________________
GitS
Belisarius is offline   Reply With Quote
Old 12-20-2004, 04:55 AM   #3 (permalink)
freesoft_2000
Code Monkey
 
Join Date: Oct 2004
Posts: 51
freesoft_2000 is on a distinguished road
Hi everyone,

Belisarius can i do this to create a platform independent file

Code:
   
     String str1 = "C:\\abc\\efg.exe"
     File file1 = new File(str1);
     String str2 = file1.getCanonicalPath();
     //Now i will the instance of str2 for any file streams

OR THIS


Code:
    String str1 = "C:\\abc\\efg.exe"
     File file1 = new File(str1);
     String str2 = file1.getAbsolutePath();
     //Now i will the instance of str2 for any file streams
Will what i do create a platform independent file?

By the way

Merry Christmas and a Happy New Year to everyone

Thank You

Yours Sincerely

Richard West
freesoft_2000 is offline   Reply With Quote
Old 12-20-2004, 05:18 AM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,166
Belisarius is on a distinguished road
Like I said, in that form it can't be truely independent. In Unix, there is no "C:".

The main difference between absolute and canonical is that the latter resolves the "." and ".." characters as well as things like sim-links, whereas the former doesn't.

They won't switch around the slashes for you.

What exactly are you trying to accomplish? I might be able to help you make the whole thing system independant.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 12-20-2004, 12:19 PM   #5 (permalink)
freesoft_2000
Code Monkey
 
Join Date: Oct 2004
Posts: 51
freesoft_2000 is on a distinguished road
Hi everyone,

Belisarius what i am trying to do is to write a function in which i input
a string say "C:\\a\\b\\c.txt" and the function returns a string which has been converted to the its equivalent according to which os the program is running on

Do you know how this can be achieved

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
freesoft_2000 is offline   Reply With Quote
Old 12-20-2004, 12:50 PM   #6 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,166
Belisarius is on a distinguished road
Ok, but why? So they can load up a saved file or a config file? In that case, the user themselves will make the change to the correct OS type.

Say I store a document on my desktop in Windows, and want to load it in your program. You pop up a dialogue asking me where the file is. I say "C:\Documents and Settings\Belisarius\Desktop\myfile.txt". If we were to do the same thing in Linux, I'd tell the program it was save to my home directory at "/home/Belisarius/myfile.txt". It's not something you really need to worry about. It's also not really a problem you can solve it someone say the file is at "C:\Documents and Settings\Belisarius\Desktop\myfile.txt" while in Linux; there is no such place.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code For Displaying files in List Box shash MS Technologies ( ASP, VB, C#, .NET ) 1 08-23-2004 09:53 PM
Installing and using CMUgraphics library. Valmont Standard C, C++ 12 03-29-2003 08:39 AM
All Class Files mixingsoup PHP 12 12-04-2002 06:49 AM


All times are GMT -8. The time now is 01:26 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting