You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileUtilities.java 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant;
  19. import org.apache.tools.ant.util.FileUtils;
  20. import java.io.File;
  21. import java.io.FileReader;
  22. import java.io.IOException;
  23. import static org.junit.Assume.assumeTrue;
  24. public class FileUtilities {
  25. /**
  26. * Reads the contents of a file into a String.
  27. * @param project the project containing the base directory the file is in.
  28. * @param fileName the path to the file from the base directory.
  29. * @return the contents of the given file as a string.
  30. * @throws IOException on error reading the file (not existing, not readable etc)
  31. */
  32. public static String getFileContents(Project project, String fileName) throws IOException {
  33. return getFileContents(new File(project.getBaseDir(), fileName));
  34. }
  35. /**
  36. * Reads the contents of a file into a String.
  37. * @param file the file to read.
  38. * @return the contents of the given file as a string.
  39. * @throws IOException on error reading the file (not existing, not readable etc)
  40. */
  41. public static String getFileContents(File file) throws IOException {
  42. FileReader rdr = null;
  43. try {
  44. rdr = new FileReader(file);
  45. return FileUtils.readFully(rdr);
  46. }
  47. finally {
  48. if (rdr != null) {
  49. rdr.close();
  50. }
  51. }
  52. }
  53. /**
  54. * Modified the timestamp on a file so it's <tt>seconds</tt> earlier than it was before. Where <tt>file</tt>
  55. * is a directory, this function recurses into all child files (and directories) and reduces their modified
  56. * timestamps by the same range, rather than set all timestamps to the same time.
  57. * @param file the file to change, or the directory to change then recurse into
  58. * @param seconds how many seconds to roll the timestamp back by
  59. */
  60. public static void rollbackTimestamps(File file, long seconds) {
  61. if (null == file || !file.exists()) {
  62. return;
  63. }
  64. assumeTrue(file.setLastModified(file.lastModified() - seconds * 1000));
  65. if (file.isDirectory()) {
  66. File[] children = file.listFiles();
  67. // only possible if exception occurs, or abstract path was not valid
  68. if (children == null) {
  69. return;
  70. }
  71. for (File child : children) {
  72. rollbackTimestamps(child, seconds);
  73. }
  74. }
  75. }
  76. }