Package net.sf.okapi.common
Class FileUtil
- java.lang.Object
-
- net.sf.okapi.common.FileUtil
-
public final class FileUtil extends Object
Helper methods for manipulating files.
-
-
Constructor Summary
Constructors Constructor Description FileUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static FilecreateTempFile(String prefix)static FilecreateTempFile(String prefix, String extension)static voiddeleteAllFilesInDirectory(String directoryPath)Deletes all files in the specified directory.static booleandeleteFile(String fileName)static URLfileToUrl(File file)static Collection<File>getFilteredFiles(File directory, FilenameFilter filter, boolean recurse)Gets a collection of the files in a given directory.static StringgetLocaleBasedFile(String baseName, String extension, LocaleId locale)Return a path to a locale based resource using the standard java property resource resolution.static StringgetPartBeforeFile(URI uri)Gets the URI part before the file name.static List<String>guessLanguages(Reader reader)Tries to guess the language(s) declared in the given input.static List<String>guessLanguages(String path)Tries to guess the language(s) declared in the given input file.static booleanisXliffSegmented(Reader reader)Scans xliff file to see if it is segmented (has <seg-source>)static booleanisXliffSegmented(String path)Scans xliff file to see if it is segmented (has <seg-source>)static FileurlToFile(URL url)
-
-
-
Method Detail
-
getFilteredFiles
public static Collection<File> getFilteredFiles(File directory, FilenameFilter filter, boolean recurse)
Gets a collection of the files in a given directory.This method search all
Files recursively that pass theFilenameFilter. Adapted from http://snippets.dzone.com/posts/show/1875- Parameters:
directory- root directoryfilter-FilenameFilterused to filter the File candidatesrecurse- true to recurse in the sub-directories, false to not.- Returns:
CollectionofFiles
-
guessLanguages
public static List<String> guessLanguages(String path)
Tries to guess the language(s) declared in the given input file. The method should work with XLIFF, TMX, TTX and TS files.The methods looks in the file line by line, in the 10 first KB, or until a source and at least one target are detected, whichever comes first.
The encoding for the file is determined based on the BOM, if present.
- Parameters:
path- the full path of the file to process.- Returns:
- a list of strings that can be empty (never null). The first string is the possible source language, the next strings are the potential target languages.
-
guessLanguages
public static List<String> guessLanguages(Reader reader)
Tries to guess the language(s) declared in the given input. The method should work with XLIFF, TMX, TTX and TS files.The methods looks in the file line by line, in the 10 first KB, or until a source and at least one target are detected, whichever comes first.
- Parameters:
reader- a reader providing the content to examine. This reader will be closed by this method.- Returns:
- a list of strings that can be empty (never null). The first string is the possible source language, the next strings are the potential target languages.
-
isXliffSegmented
public static boolean isXliffSegmented(String path)
Scans xliff file to see if it is segmented (has <seg-source>)- Parameters:
path- the full path of the xliff file to process.- Returns:
- a true if xliff is segmented, false otherwise.
-
isXliffSegmented
public static boolean isXliffSegmented(Reader reader)
Scans xliff file to see if it is segmented (has <seg-source>)- Parameters:
reader- a reader providing the xliff content to examine. This reader will be closed by this method.- Returns:
- a true if xliff is segmented, false otherwise.
-
deleteAllFilesInDirectory
public static void deleteAllFilesInDirectory(String directoryPath)
Deletes all files in the specified directory.- Parameters:
directoryPath- - the path to the directory- Throws:
OkapiIOException- if a file cannot be deleted.
-
getLocaleBasedFile
public static String getLocaleBasedFile(String baseName, String extension, LocaleId locale)
Return a path to a locale based resource using the standard java property resource resolution. Works with any kind of files e.g., segmenter_en_US.srx or content_fr_FR.htmlWARNING: Assumes default classLoader only!!
- Parameters:
baseName- base name of the resourceextension- resource file extensionlocale- locale of the resource we are looking for- Returns:
- the path to the resource or null if not found
-
getPartBeforeFile
public static String getPartBeforeFile(URI uri)
Gets the URI part before the file name.- Parameters:
uri- The URI to process.- Returns:
- the URI part before the file name.
-
deleteFile
public static boolean deleteFile(String fileName)
-
-