Difference between revisions of "Get an absolute file path from a relative file path"

From CodeCodex

(Ruby)
(Erlang)
 
Line 7: Line 7:
 
</pre>
 
</pre>
  
===Java===
+
===Erlang===
 
<pre>
 
<pre>
 +
% current working directory : /temp
 +
filename:absname("test.dat").  % "/temp/test.dat"
 +
 +
% or
 +
filename:absname("test.dat", "/usr/local").    % "/usr/local/test.dat"
 +
</pre>
 +
 +
===Java===
 +
<pre class="java">
 
     File file = new File("filename.txt");
 
     File file = new File("filename.txt");
 
     file = file.getAbsoluteFile();  // c:\temp\filename.txt
 
     file = file.getAbsoluteFile();  // c:\temp\filename.txt
Line 23: Line 32:
  
 
===Ruby===
 
===Ruby===
<pre>
+
<pre class="ruby">
 
Dir.chdir("/work/backup") do
 
Dir.chdir("/work/backup") do
 
   fname = "tmp.txt"
 
   fname = "tmp.txt"
Line 31: Line 40:
  
 
[[Category:Common Lisp]]
 
[[Category:Common Lisp]]
 +
[[Category:Erlang]]
 
[[Category:Java]]
 
[[Category:Java]]
 
[[Category:Ruby]]
 
[[Category:Ruby]]
 
[[Category:I/O]]
 
[[Category:I/O]]

Latest revision as of 17:33, 13 January 2011

Implementations[edit]

Common Lisp[edit]

> (directory "filename.txt")

(#P"/home/Username/filename.txt")

Erlang[edit]

% current working directory : /temp
filename:absname("test.dat").   % "/temp/test.dat"

% or
filename:absname("test.dat", "/usr/local").     % "/usr/local/test.dat"

Java[edit]

    File file = new File("filename.txt");
    file = file.getAbsoluteFile();  // c:\temp\filename.txt
    
    file = new File("dir"+File.separatorChar+"filename.txt");
    file = file.getAbsoluteFile();  // c:\temp\dir\filename.txt
    
    file = new File(".."+File.separatorChar+"filename.txt");
    file = file.getAbsoluteFile();  // c:\temp\..\filename.txt
    
    // Note that filename.txt does not need to exist

Ruby[edit]

Dir.chdir("/work/backup") do
  fname = "tmp.txt"
  p File.expand_path(fname)    #=> "C:/work/backup/tmp.txt"
end