Skip to content

Package: FileUtils$1

FileUtils$1

nameinstructionbranchcomplexitylinemethod
postVisitDirectory(Path, IOException)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
visitFile(Path, BasicFileAttributes)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
{...}
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package de.fhdw.wtf.file;
2:
3: import java.io.File;
4: import java.io.FileWriter;
5: import java.io.IOException;
6: import java.nio.file.FileVisitResult;
7: import java.nio.file.Files;
8: import java.nio.file.Path;
9: import java.nio.file.Paths;
10: import java.nio.file.SimpleFileVisitor;
11: import java.nio.file.attribute.BasicFileAttributes;
12: import java.util.Collection;
13: import java.util.Iterator;
14:
15: /**
16: * Collection of operations for file read and modification.
17: */
18: public final class FileUtils {
19:         
20:         /**
21:          * Constructor of {@link FileUtils}.
22:          */
23:         private FileUtils() {
24:         }
25:         
26:         /**
27:          * Line-Break.
28:          */
29:         private static final String BREAK = "\n";
30:         
31:         /**
32:          * Error for Directory-Creating.
33:          */
34:         private static final String ERROR_MKDIR = "Could not create directory.";
35:         
36:         /**
37:          * Error for File-Deleting.
38:          */
39:         private static final String ERROR_DELETE = "Could not delete file.";
40:         
41:         /**
42:          * Error for File-Creating.
43:          */
44:         private static final String ERROR_CREATE = "Could not create file.";
45:         
46:         /**
47:          * First deletes the file if the file exists. Then creates directory if it does not exist and finally creates the
48:          * file and writes the content to the new file.
49:          *
50:          * @param content
51:          * content
52:          * @param path
53:          * Path without Filename
54:          * @param file
55:          * Path with Filename
56:          * @throws IOException
57:          * possible Exception
58:          */
59:         public static void overrideToFile(final String content, final File path, final File file) throws IOException {
60:                 if (file.exists() && !file.delete()) {
61:                         throw new IOException(ERROR_DELETE);
62:                 }
63:                 if (!path.exists() && !path.mkdirs()) {
64:                         throw new IOException(ERROR_MKDIR);
65:                 }
66:                 if (!file.createNewFile()) {
67:                         throw new IOException(ERROR_CREATE);
68:                 }
69:                 try (final FileWriter writer = new FileWriter(file, false);) {
70:                         writer.write(content);
71:                         writer.flush();
72:                 }
73:         }
74:         
75:         /**
76:          * Returns the content of the given {@link File}.
77:          *
78:          * @param filePath
79:          * filePath
80:          * @return content of the file
81:          * @throws IOException
82:          * For Exceptions with reading and file handling.
83:          */
84:         public static String getFileString(final String filePath) throws IOException {
85:                 try (final FileIterator i = new FileIterator(new File(filePath));) {
86:                         final StringBuilder b = new StringBuilder();
87:                         while (i.hasNext()) {
88:                                 b.append(i.next());
89:                                 if (i.hasNext()) {
90:                                         b.append(BREAK);
91:                                 }
92:                         }
93:                         return b.toString();
94:                 }
95:         }
96:         
97:         /**
98:          * Saves the content to an already created file.
99:          *
100:          * @param content
101:          * content
102:          * @param file
103:          * file
104:          * @throws IOException
105:          * possbile Exception
106:          */
107:         public static void saveToFile(final String content, final File file) throws IOException {
108:                 try (final FileWriter writer = new FileWriter(file, false);) {
109:                         writer.write(content);
110:                         writer.flush();
111:                 }
112:         }
113:         
114:         /**
115:          * Returned the files as String.
116:          *
117:          * @param files
118:          * list of files
119:          * @return files as String
120:          * @throws java.io.FileNotFoundException
121:          * possible Exception
122:          */
123:         public static String printFileList(final Collection<File> files) {
124:                 final Iterator<File> i = files.iterator();
125:                 final StringBuilder sb = new StringBuilder();
126:                 while (i.hasNext()) {
127:                         sb.append(i.next().getAbsolutePath());
128:                         sb.append(BREAK);
129:                 }
130:                 return sb.toString();
131:         }
132:         
133:         /**
134:          * Delete the directory.
135:          *
136:          * @param dir
137:          * directory
138:          * @throws IOException
139:          * possible Exception
140:          */
141:         public static void deleteDirectory(final String dir) throws IOException {
142:                 final Path directory = Paths.get(dir);
143:                 if (new File(dir).listFiles() != null) {
144:                         Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
145:                                 @Override
146:                                 public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
147:                                         Files.delete(file);
148:                                         return FileVisitResult.CONTINUE;
149:                                 }
150:                                 
151:                                 @Override
152:                                 public FileVisitResult postVisitDirectory(final Path dir2, final IOException exc) throws IOException {
153:                                         Files.delete(dir2);
154:                                         return FileVisitResult.CONTINUE;
155:                                 }
156:                                 
157:                         });
158:                 }
159:         }
160: }