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

FileUtils.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.utils;
  17. import java.io.BufferedReader;
  18. import java.io.BufferedWriter;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.FileOutputStream;
  22. import java.io.InputStreamReader;
  23. import java.io.OutputStreamWriter;
  24. import java.nio.charset.Charset;
  25. /**
  26. * Common file utilities.
  27. *
  28. * @author James Moger
  29. *
  30. */
  31. public class FileUtils {
  32. /**
  33. * Returns the string content of the specified file.
  34. *
  35. * @param file
  36. * @param lineEnding
  37. * @return the string content of the file
  38. */
  39. public static String readContent(File file, String lineEnding) {
  40. StringBuilder sb = new StringBuilder();
  41. try {
  42. InputStreamReader is = new InputStreamReader(new FileInputStream(file),
  43. Charset.forName("UTF-8"));
  44. BufferedReader reader = new BufferedReader(is);
  45. String line = null;
  46. while ((line = reader.readLine()) != null) {
  47. sb.append(line);
  48. if (lineEnding != null) {
  49. sb.append(lineEnding);
  50. }
  51. }
  52. reader.close();
  53. } catch (Throwable t) {
  54. System.err.println("Failed to read content of " + file.getAbsolutePath());
  55. t.printStackTrace();
  56. }
  57. return sb.toString();
  58. }
  59. /**
  60. * Writes the string content to the file.
  61. *
  62. * @param file
  63. * @param content
  64. */
  65. public static void writeContent(File file, String content) {
  66. try {
  67. OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file),
  68. Charset.forName("UTF-8"));
  69. BufferedWriter writer = new BufferedWriter(os);
  70. writer.append(content);
  71. writer.close();
  72. } catch (Throwable t) {
  73. System.err.println("Failed to write content of " + file.getAbsolutePath());
  74. t.printStackTrace();
  75. }
  76. }
  77. /**
  78. * Recursively traverses a folder and its subfolders to calculate the total
  79. * size in bytes.
  80. *
  81. * @param directory
  82. * @return folder size in bytes
  83. */
  84. public static long folderSize(File directory) {
  85. if (directory == null || !directory.exists()) {
  86. return -1;
  87. }
  88. if (directory.isFile()) {
  89. return directory.length();
  90. }
  91. long length = 0;
  92. for (File file : directory.listFiles()) {
  93. if (file.isFile()) {
  94. length += file.length();
  95. } else {
  96. length += folderSize(file);
  97. }
  98. }
  99. return length;
  100. }
  101. }