Top Description Fields Constructors Methods
org.jabref.logic.util.io

public Class FileUtil

extends Object
Class Inheritance
Imports
java.io.File, .IOException, .UncheckedIOException, java.nio.file.FileSystems, .Files, .InvalidPathException, .Path, .StandardOpenOption, java.util.ArrayDeque, .ArrayList, .Arrays, .Collections, .Comparator, .Deque, .List, .Locale, .Objects, .Optional, java.util.regex.Pattern, java.util.stream.Collectors, .Stream, org.jabref.logic.citationkeypattern.BracketedPattern, org.jabref.logic.util.StandardFileType, org.jabref.model.database.BibDatabase, .BibDatabaseContext, org.jabref.model.entry.BibEntry, org.jabref.preferences.FilePreferences, org.slf4j.Logger, .LoggerFactory

The idea of this class is to add general functionality that could possibly even in the Java NIO package, such as getting/adding file extension etc.

Field Summary

Modifier and TypeField and Description
private static final int[]
ILLEGAL_CHARS

MUST ALWAYS BE A SORTED ARRAY because it is used in a binary search

public static final boolean
private static final Logger
public static final int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Path

Returns:

the with the modified file name
addExtension
(Path
the path to add the extension to
path
,
String
the extension to add
extension
)

Adds an extension to the given file name.

public static boolean

Returns:

boolean Whether the copy succeeded, or was stopped due to the file already existing.
copyFile
(Path
Path Source file
pathToSourceFile
,
Path
Path Destination file
pathToDestinationFile
,
boolean
boolean Determines whether the copy goes on even if the file exists.
replaceExisting
)

Copies a file.

public static String

Returns:

a suggested dirName
createDirNameFromPattern
(BibDatabase
the database, where the entry is located
database
,
BibEntry
the entry to which the directory should be linked to
entry
,
String
the dirname pattern
directoryNamePattern
)

Determines directory name provided by an entry in a database

public static String

Returns:

a suggested fileName
createFileNameFromPattern
(BibDatabase
the database, where the entry is located
database
,
BibEntry
the entry to which the file should be linked to
entry
,
String
the filename pattern
fileNamePattern
)

Determines filename provided by an entry in a database

public static boolean

Returns:

Boolean whether there is an illegal name.
detectBadFileName
(String
the fileName to detect
fileName
)

Detect illegal characters in given filename.

public static Optional<Path>
find(final BibDatabaseContext databaseContext, String fileName, FilePreferences filePreferences)

public static Optional<Path>
find(String fileName, List<Path> directories)

Converts a relative filename to an absolute one, if necessary.

public static Optional<Path>

Returns:

an empty optional if the file does not exist, otherwise, the absolute path
find
(String
the filename (e.g., a .pdf file), may contain path separators
fileName
,
Path
the directory which should be search starting point
directory
)

Converts a relative filename to an absolute one, if necessary.

public static List<Path>

Returns:

a list including all found paths to files that match the defined conditions
findListOfFiles
(String
the name of the file that should be found
filename
,
List<Path>
the directories that will be searched
directories
)

Finds a file inside a list of directory structures.

public static Optional<Path>

Returns:

the path to the first file that matches the defined conditions
findSingleFileRecursively
(String
the name of the file that should be found
filename
,
Path
the rootDirectory that will be searched
rootDirectory
)

Finds a file inside a directory structure.

public static String
getBaseName(String fileNameWithExtension)

Returns the name part of a file name (i.e., everything in front of last ".").

public static String
getBaseName(Path fileNameWithExtension)

Returns the name part of a file name (i.e., everything in front of last ".").

public static Optional<String>

Returns:

the extension (without leading dot), trimmed and in lowercase.
getFileExtension
(String fileName)

Returns the extension of a file name or Optional.empty() if the file does not have one (no "." in name).

public static Optional<String>

Returns:

the extension (without leading dot), trimmed and in lowercase.
getFileExtension
(Path file)

Returns the extension of a file or Optional.empty() if the file does not have one (no .

public static Path

Returns:

Path of current panel database directory or the standard working directory in case the database was not saved yet
getInitialDirectory
(BibDatabaseContext databaseContext, Path workingDirectory)

public static List<Path>

Returns:

list of files. May be empty
getListOfLinkedFiles
(List<BibEntry>
list of BibTeX entries
bes
,
List<Path>
list of directories to try for expansion
fileDirs
)

Returns the list of linked files.

public static Optional<String>
getUniquePathDirectory(List<String>
List of paths as Strings
paths
,
Path
The to be tested path
comparePath
)

Looks for the unique directory, if any, different to the provided paths

public static Optional<String>
getUniquePathFragment(List<String>
List of paths as Strings
paths
,
Path
The to be shortened path
comparePath
)

Looks for the shortest unique path of the in a list of paths

public static String
getValidFileName(String fileName)

Returns a valid filename for most operating systems.

public static boolean

Returns:

True if file extension is ".bib", false otherwise
isBibFile
(Path
The file to check
file
)

Test if the file is a bib file by simply checking the extension to be ".bib"

public static boolean
isCharLegal(char c)

public static boolean

Returns:

True if file extension is ".pdf", false otherwise
isPDFFile
(Path
The file to check
file
)

Test if the file is a pdf file by simply checking the extension to be ".pdf"

public static Path
relativize(Path
the file to be shortened
file
,
List<Path>
directories to check
directories
)

Converts an absolute file to a relative one, if possible.

public static Path
relativize(Path
the file path to be shortened
path
,
BibDatabaseContext databaseContext, FilePreferences filePreferences)

Converts an absolute file to a relative one, if possible.

public static String
toPortableString(Path path)

Creates a string representation of the given path that should work on all systems.

public static List<String>

Returns:

the minimal unique path substring for each file path
uniquePathSubstrings
(List<String>
the file paths
paths
)

Creates the minimal unique path substring for each file among multiple file paths.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ILLEGAL_CHARSback to summary
private static final int[] ILLEGAL_CHARS

MUST ALWAYS BE A SORTED ARRAY because it is used in a binary search

IS_POSIX_COMPLIANTback to summary
public static final boolean IS_POSIX_COMPLIANT
LOGGERback to summary
private static final Logger LOGGER
MAXIMUM_FILE_NAME_LENGTHback to summary
public static final int MAXIMUM_FILE_NAME_LENGTH

Constructor Detail

FileUtilback to summary
private FileUtil()

Method Detail

addExtensionback to summary
public static Path addExtension(Path path, String extension)

Adds an extension to the given file name. The original extension is not replaced. That means, "demo.bib", ".sav" gets "demo.bib.sav" and not "demo.sav"

Warning

If "ext" is passed, this is literally added. Thus addExtension("tmp.txt", "ext") leads to "tmp.txtext"

Parameters
path:Path

the path to add the extension to

extension:String

the extension to add

Returns:Path

the with the modified file name

copyFileback to summary
public static boolean copyFile(Path pathToSourceFile, Path pathToDestinationFile, boolean replaceExisting)

Copies a file.

Parameters
pathToSourceFile:Path

Path Source file

pathToDestinationFile:Path

Path Destination file

replaceExisting:boolean

boolean Determines whether the copy goes on even if the file exists.

Returns:boolean

boolean Whether the copy succeeded, or was stopped due to the file already existing.

createDirNameFromPatternback to summary
public static String createDirNameFromPattern(BibDatabase database, BibEntry entry, String directoryNamePattern)

Determines directory name provided by an entry in a database

Parameters
database:BibDatabase

the database, where the entry is located

entry:BibEntry

the entry to which the directory should be linked to

directoryNamePattern:String

the dirname pattern

Returns:String

a suggested dirName

createFileNameFromPatternback to summary
public static String createFileNameFromPattern(BibDatabase database, BibEntry entry, String fileNamePattern)

Determines filename provided by an entry in a database

Parameters
database:BibDatabase

the database, where the entry is located

entry:BibEntry

the entry to which the file should be linked to

fileNamePattern:String

the filename pattern

Returns:String

a suggested fileName

detectBadFileNameback to summary
public static boolean detectBadFileName(String fileName)

Detect illegal characters in given filename.

Parameters
fileName:String

the fileName to detect

Returns:boolean

Boolean whether there is an illegal name.

See Also
org.jabref.logic.util.io.FileNameCleaner#cleanFileName
findback to summary
public static Optional<Path> find(final BibDatabaseContext databaseContext, String fileName, FilePreferences filePreferences)
findback to summary
public static Optional<Path> find(String fileName, List<Path> directories)

Converts a relative filename to an absolute one, if necessary. Returns an empty optional if the file does not exist.

Will look in each of the given directories starting from the beginning and returning the first found file to match if any.

findback to summary
public static Optional<Path> find(String fileName, Path directory)

Converts a relative filename to an absolute one, if necessary.

Parameters
fileName:String

the filename (e.g., a .pdf file), may contain path separators

directory:Path

the directory which should be search starting point

Returns:Optional<Path>

an empty optional if the file does not exist, otherwise, the absolute path

findListOfFilesback to summary
public static List<Path> findListOfFiles(String filename, List<Path> directories)

Finds a file inside a list of directory structures. Will also look for the file inside nested directories.

Parameters
filename:String

the name of the file that should be found

directories:List<Path>

the directories that will be searched

Returns:List<Path>

a list including all found paths to files that match the defined conditions

findSingleFileRecursivelyback to summary
public static Optional<Path> findSingleFileRecursively(String filename, Path rootDirectory)

Finds a file inside a directory structure. Will also look for the file inside nested directories.

Parameters
filename:String

the name of the file that should be found

rootDirectory:Path

the rootDirectory that will be searched

Returns:Optional<Path>

the path to the first file that matches the defined conditions

getBaseNameback to summary
public static String getBaseName(String fileNameWithExtension)

Returns the name part of a file name (i.e., everything in front of last ".").

getBaseNameback to summary
public static String getBaseName(Path fileNameWithExtension)

Returns the name part of a file name (i.e., everything in front of last ".").

getFileExtensionback to summary
public static Optional<String> getFileExtension(String fileName)

Returns the extension of a file name or Optional.empty() if the file does not have one (no "." in name).

Returns:Optional<String>

the extension (without leading dot), trimmed and in lowercase.

getFileExtensionback to summary
public static Optional<String> getFileExtension(Path file)

Returns the extension of a file or Optional.empty() if the file does not have one (no . in name).

Returns:Optional<String>

the extension (without leading dot), trimmed and in lowercase.

getInitialDirectoryback to summary
public static Path getInitialDirectory(BibDatabaseContext databaseContext, Path workingDirectory)
Returns:Path

Path of current panel database directory or the standard working directory in case the database was not saved yet

getListOfLinkedFilesback to summary
public static List<Path> getListOfLinkedFiles(List<BibEntry> bes, List<Path> fileDirs)

Returns the list of linked files. The files have the absolute filename

Parameters
bes:List<BibEntry>

list of BibTeX entries

fileDirs:List<Path>

list of directories to try for expansion

Returns:List<Path>

list of files. May be empty

getUniquePathDirectoryback to summary
public static Optional<String> getUniquePathDirectory(List<String> paths, Path comparePath)

Looks for the unique directory, if any, different to the provided paths

Parameters
paths:List<String>

List of paths as Strings

comparePath:Path

The to be tested path

getUniquePathFragmentback to summary
public static Optional<String> getUniquePathFragment(List<String> paths, Path comparePath)

Looks for the shortest unique path of the in a list of paths

Parameters
paths:List<String>

List of paths as Strings

comparePath:Path

The to be shortened path

getValidFileNameback to summary
public static String getValidFileName(String fileName)

Returns a valid filename for most operating systems.

Currently, only the length is restricted to 255 chars, see MAXIMUM_FILE_NAME_LENGTH.

For "real" cleaning, FileNameCleaner#cleanFileName(String) should be used.

isBibFileback to summary
public static boolean isBibFile(Path file)

Test if the file is a bib file by simply checking the extension to be ".bib"

Parameters
file:Path

The file to check

Returns:boolean

True if file extension is ".bib", false otherwise

isCharLegalback to summary
public static boolean isCharLegal(char c)
isPDFFileback to summary
public static boolean isPDFFile(Path file)

Test if the file is a pdf file by simply checking the extension to be ".pdf"

Parameters
file:Path

The file to check

Returns:boolean

True if file extension is ".pdf", false otherwise

relativizeback to summary
public static Path relativize(Path file, List<Path> directories)

Converts an absolute file to a relative one, if possible. Returns the parameter file itself if no shortening is possible.

This method works correctly only if directories are sorted decent in their length i.e. /home/user/literature/important before /home/user/literature.

Parameters
file:Path

the file to be shortened

directories:List<Path>

directories to check

relativizeback to summary
public static Path relativize(Path path, BibDatabaseContext databaseContext, FilePreferences filePreferences)

Converts an absolute file to a relative one, if possible. Returns the parameter file itself if no shortening is possible.

Parameters
path:Path

the file path to be shortened

toPortableStringback to summary
public static String toPortableString(Path path)

Creates a string representation of the given path that should work on all systems. This method should be used when a path needs to be stored in the bib file or preferences.

uniquePathSubstringsback to summary
public static List<String> uniquePathSubstrings(List<String> paths)

Creates the minimal unique path substring for each file among multiple file paths.

Parameters
paths:List<String>

the file paths

Returns:List<String>

the minimal unique path substring for each file path