Package de.monticore.io
Class FileReaderWriter
java.lang.Object
de.monticore.io.FileReaderWriter
This class handles all I/O commands in Monticore.
Important conventions: File locations are always encoded as Path or URL objects. When creating
Path objects please make use of the Path.resolve(java.nio.file.Path) method whenever applicable. This
convention extends beyond the FileHandler. Do not exchange information between classes
using String literals or File objects. Doing so within classes is permitted, though discouraged.
Failure to adhere to such a convention has caused innumerable trivial bugs in past
implementations.
This class uses UTF_8 encoding per default and is realized as singleton.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Charsetprotected static FileReaderWriterSaves allJarFiles opened bygetReader(URL)if the protocol "jar:" is used. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedUses the default encoding UTF_8;protectedFileReaderWriter(Charset charset) -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean_existsFile(Path sourcePath) protected Reader_getReader(URL location) _getResource(ClassLoader classLoader, String name) protected String_readFromFile(Reader reader) protected String_readFromFile(URL sourcePath) protected String_readFromFile(Path sourcePath) protected void_storeInFile(Path targetPath, String content) static voidCloses all jar files opened bygetReader(URL).static booleanexistsFile(Path sourcePath) protected static FileReaderWriterstatic ReaderObtains the reader for a passed model coordinate.getResource(ClassLoader classLoader, String name) Tries to load a resource with the passed name in the file system using the passed classLoader.static voidinit()static voidinit(FileReaderWriter instance) static voidstatic StringreadFromFile(Reader reader) static StringreadFromFile(URL sourcePath) Reads the String content from a file using the specified encoding.static StringreadFromFile(Path sourcePath) Reads the String content from a file using the specified encoding.voidsetCharset(Charset charset) Sets the encoding for all subsequent operations until another encoding is assigned.static voidstoreInFile(Path targetPath, String content) Writes a String to a file using the specified encoding.
-
Field Details
-
INSTANCE
-
charset
-
openedJarFiles
Saves allJarFiles opened bygetReader(URL)if the protocol "jar:" is used. These files must be closed at the end of programm viacloseOpenedJarFiles().
-
-
Constructor Details
-
FileReaderWriter
protected FileReaderWriter()Uses the default encoding UTF_8; -
FileReaderWriter
- Parameters:
charset- The initial encoding to be used until another encoding is assigned.- See Also:
-
-
Method Details
-
setCharset
Sets the encoding for all subsequent operations until another encoding is assigned.- Parameters:
charset- The encoding to be used
-
init
public static void init() -
init
-
init
-
getFileReaderWriter
-
storeInFile
Writes a String to a file using the specified encoding.- Parameters:
targetPath- The location of the file to be written to.content- The String that's supposed to be written into the file- See Also:
-
_storeInFile
-
readFromFile
Reads the String content from a file using the specified encoding.- Parameters:
sourcePath- The absolute location (fully specifies the filename) of the file to be read- Returns:
- The String content of the file
- See Also:
-
_readFromFile
-
readFromFile
Reads the String content from a file using the specified encoding. URLs can also address files within jars.- Parameters:
sourcePath- The absolute location (fully specifies the filename) of the file to be read- Returns:
- The String content of the file
- See Also:
-
_readFromFile
-
readFromFile
-
_readFromFile
-
getResource
Tries to load a resource with the passed name in the file system using the passed classLoader. If multiple resources are found, throws an ambiguity exception. If no resource is found, returnsOptional.empty()- Parameters:
classLoader- The classloader employed to load the resourcename- The name of the resource to load- Returns:
- The optional URL of the resource, if present, or
Optional.empty()else
-
_getResource
-
existsFile
-
_existsFile
-
getReader
Obtains the reader for a passed model coordinate. The resulting reader can be used as argument for a parse method of a language's parser.- Parameters:
location-- Returns:
-
_getReader
-
closeOpenedJarFiles
public static void closeOpenedJarFiles()Closes all jar files opened bygetReader(URL). Must be called at the end of the program to ensure all resources are freed.
-