Package de.jplag.util
Class FileUtils
java.lang.Object
de.jplag.util.FileUtils
Encapsulates various interactions with files to prevent issues with file encodings.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancheckParentWritable(File file) Checks if the parent file can be written to.static booleancheckWritable(File file) Checks if the given file can be written to.static CharsetdetectCharset(File file) Detects the charset of a file.static CharsetdetectCharsetFromMultiple(Collection<File> files) Detects the most probable charset over the whole set of files.static BufferedReaderopenFileReader(File file) Opens a file reader, guessing the charset from the content.static WriteropenFileWriter(File file) Opens a file writer, using the default charset for JPlagstatic StringreadFileContent(File file) Reads the contents of a file into a single string.static voidWrites the given content into the given file using the default charset
-
Method Details
-
openFileReader
Opens a file reader, guessing the charset from the content. Also, if the file is encoded in a UTF* encoding and a bom exists, it is removed from the reader.- Parameters:
file- The file to open for read- Returns:
- The reader, configured with the best matching charset
- Throws:
IOException- If the file does not exist for is not readable
-
readFileContent
Reads the contents of a file into a single string.- Parameters:
file- The file to read- Returns:
- The files content as a string
- Throws:
IOException- If an IO error occurs- See Also:
-
detectCharset
Detects the charset of a file. Prefer usingopenFileReader(File)orreadFileContent(File)if you are only interested in the content.- Parameters:
file- The file to detect- Returns:
- The most probable charset
- Throws:
IOException- If an IO error occurs
-
detectCharsetFromMultiple
Detects the most probable charset over the whole set of files.- Parameters:
files- The files to check- Returns:
- The most probable charset
- Throws:
ParsingException
-
openFileWriter
Opens a file writer, using the default charset for JPlag- Parameters:
file- The file to write- Returns:
- The file writer, configured with the default charset
- Throws:
IOException- If the file does not exist or is not writable
-
write
Writes the given content into the given file using the default charset- Parameters:
file- The filecontent- The content- Throws:
IOException- If any error occurs
-
checkWritable
Checks if the given file can be written to. If the file does not exist checks if it can be created.- Parameters:
file- The file to check- Returns:
- true, if the file can be written to
-
checkParentWritable
Checks if the parent file can be written to.- Parameters:
file- The file to check- Returns:
- true, if the parent can be written to
-