FileUtilities.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* ========================================================================
  2. * JCommon : a free general purpose class library for the Java(tm) platform
  3. * ========================================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jcommon/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25. * in the United States and other countries.]
  26. *
  27. * ------------------
  28. * FileUtilities.java
  29. * ------------------
  30. * (C) Copyright 2000-2004, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * $Id: FileUtilities.java,v 1.5 2005/10/18 13:16:02 mungady Exp $
  36. *
  37. * Changes (from 5-Nov-2001)
  38. * -------------------------
  39. * 05-Nov-2001 : Changed package to com.jrefinery.io.* (DG);
  40. * 04-Mar-2002 : Renamed Files.java --> FileUtilities.java (DG);
  41. * 10-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  42. *
  43. */
  44. package org.jfree.io;
  45. import java.io.File;
  46. import java.util.StringTokenizer;
  47. /**
  48. * A class containing useful utility methods relating to files.
  49. *
  50. * @author David Gilbert
  51. */
  52. public class FileUtilities {
  53. /**
  54. * To prevent unnecessary instantiation.
  55. */
  56. private FileUtilities() {
  57. }
  58. /**
  59. * Returns a reference to a file with the specified name that is located
  60. * somewhere on the classpath. The code for this method is an adaptation
  61. * of code supplied by Dave Postill.
  62. *
  63. * @param name the filename.
  64. *
  65. * @return a reference to a file or <code>null</code> if no file could be found.
  66. */
  67. public static File findFileOnClassPath(final String name) {
  68. final String classpath = System.getProperty("java.class.path");
  69. final String pathSeparator = System.getProperty("path.separator");
  70. final StringTokenizer tokenizer = new StringTokenizer(classpath, pathSeparator);
  71. while (tokenizer.hasMoreTokens()) {
  72. final String pathElement = tokenizer.nextToken();
  73. final File directoryOrJar = new File(pathElement);
  74. final File absoluteDirectoryOrJar = directoryOrJar.getAbsoluteFile();
  75. if (absoluteDirectoryOrJar.isFile()) {
  76. final File target = new File(absoluteDirectoryOrJar.getParent(), name);
  77. if (target.exists()) {
  78. return target;
  79. }
  80. }
  81. else {
  82. final File target = new File(directoryOrJar, name);
  83. if (target.exists()) {
  84. return target;
  85. }
  86. }
  87. }
  88. return null;
  89. }
  90. }