Construct file path

  1. /*
  2.   Construct file path
  3.   This Java example shows how to construct a file path in Java using File class.
  4. */
  5.  
  6. import java.io.*;
  7.  
  8. public class ConstructFilePath {
  9.  
  10. public static void main(String[] args) {
  11.  
  12. /*
  13.   * To create a file path use File.separator constant defined in
  14.   * File class.
  15.   *
  16.   * Windows based machines has path like \dir1\dir2 while UNIX based machines
  17.   * has path like /dir1/dir2
  18.   */
  19.  
  20. String filePath = File.separator + "JavaExamples" + File.separator + "IO";
  21.  
  22. //create new File object
  23. File file = new File(filePath);
  24.  
  25. /*
  26.   * Please note that creating file object DOES NOT actually create a file.
  27.   * It DOES NOT have any effects on the filesystem.
  28.   */
  29.  
  30. //display file path using getPath() method of File class.
  31. System.out.println("File path is : " + file.getPath());
  32.  
  33. }
  34. }
  35.  
  36. /*
  37. Output in Windows machine would be,
  38. File path is : \JavaExamples\IO
  39.  
  40. Output in UNIX machine would be,
  41. File path is : /JavaExamples/IO
  42. */

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!