Package com.oorian.utils
Class ServerFile
java.lang.Object
com.oorian.utils.OorianFile
com.oorian.utils.ServerFile
A file backed by the local filesystem with read and write support.
For reading files deployed within a web application, use the fromWebApp(String)
factory method, which resolves paths relative to the application root via
OorianSession.getAppContext().
Usage examples:
// Absolute filesystem path
ServerFile logFile = new ServerFile("/var/log/app.log");
String contents = logFile.read();
// Write to a file
ServerFile output = new ServerFile("/tmp/report.txt");
output.write("Report contents here");
// Web application resource
ServerFile webResource = ServerFile.fromWebApp("/WEB-INF/config.xml");
String xml = webResource.read();
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionServerFile(File file) Creates a newServerFilewrapping the givenFile.ServerFile(String path) Creates a newServerFilefor the given filesystem path. -
Method Summary
Modifier and TypeMethodDescriptionvoidAppends the given string to this file using UTF-8 encoding, creating the file if it does not exist.booleandelete()Deletes this file from the filesystem.booleanexists()Returns whether this file exists and is accessible.static booleanReturns whether a file exists at the given filesystem path.static ServerFilefromWebApp(String path) Creates aServerFileby resolving a path relative to the web application root.Returns anInputStreamfor reading this file's contents.getName()Returns the filename extracted from the path, handling both forward and back slashes.Returns anOutputStreamfor writing to this file.getPath()Returns the path of this file as provided at construction time.longReturns the time this file was last modified, in milliseconds since the epoch.longlength()Returns the length of this file in bytes.static StringReads the entire contents of a file as a UTF-8 string.toFile()Returns the underlyingFileobject.voidwrite(byte[] data) Writes the given byte array to this file, creating the file if it does not exist and overwriting any existing contents.voidWrites the given string to this file using UTF-8 encoding, creating the file if it does not exist and overwriting any existing contents.voidWrites the given string to this file using the specified charset, creating the file if it does not exist and overwriting any existing contents.Methods inherited from class com.oorian.utils.OorianFile
read, readAsBytes, readAsLines, readAsString
-
Constructor Details
-
ServerFile
Creates a newServerFilefor the given filesystem path.- Parameters:
path- the absolute or relative filesystem path.
-
ServerFile
Creates a newServerFilewrapping the givenFile.- Parameters:
file- the file to wrap.
-
-
Method Details
-
exists
Returns whether a file exists at the given filesystem path.- Parameters:
path- the absolute or relative filesystem path.- Returns:
trueif a file exists at the path,falseotherwise.
-
read
Reads the entire contents of a file as a UTF-8 string.- Parameters:
path- the absolute or relative filesystem path.- Returns:
- the file contents as a string.
- Throws:
IOException- if the file does not exist or an I/O error occurs.
-
fromWebApp
Creates aServerFileby resolving a path relative to the web application root.Uses
OorianSession.getAppContext()to obtain the real filesystem path. This requires an active application context (i.e., the application must be running).- Parameters:
path- the web application-relative path (e.g.,"/WEB-INF/config.xml").- Returns:
- a new
ServerFilefor the resolved path. - Throws:
IllegalStateException- if the application context is not available or the path cannot be resolved.
-
getInputStream
Returns anInputStreamfor reading this file's contents.- Specified by:
getInputStreamin classOorianFile- Returns:
- an input stream for this file.
- Throws:
IOException- if the file cannot be opened or does not exist.
-
exists
public boolean exists()Returns whether this file exists and is accessible.- Specified by:
existsin classOorianFile- Returns:
trueif the file exists,falseotherwise.
-
getPath
Returns the path of this file as provided at construction time.- Specified by:
getPathin classOorianFile- Returns:
- the file path.
-
getName
Returns the filename extracted from the path, handling both forward and back slashes.- Overrides:
getNamein classOorianFile- Returns:
- the filename portion of the path.
-
write
Writes the given string to this file using UTF-8 encoding, creating the file if it does not exist and overwriting any existing contents.- Parameters:
content- the string content to write.- Throws:
IOException- if an I/O error occurs.
-
write
Writes the given string to this file using the specified charset, creating the file if it does not exist and overwriting any existing contents.- Parameters:
content- the string content to write.charset- the charset to use for encoding.- Throws:
IOException- if an I/O error occurs.
-
write
Writes the given byte array to this file, creating the file if it does not exist and overwriting any existing contents.- Parameters:
data- the byte array to write.- Throws:
IOException- if an I/O error occurs.
-
getOutputStream
Returns anOutputStreamfor writing to this file.The caller is responsible for closing the returned stream.
- Returns:
- an output stream for this file.
- Throws:
IOException- if the file cannot be opened for writing.
-
append
Appends the given string to this file using UTF-8 encoding, creating the file if it does not exist.- Parameters:
content- the string content to append.- Throws:
IOException- if an I/O error occurs.
-
length
public long length()Returns the length of this file in bytes.- Returns:
- the file length in bytes, or
0if the file does not exist.
-
lastModified
public long lastModified()Returns the time this file was last modified, in milliseconds since the epoch.- Returns:
- the last modified time, or
0if the file does not exist.
-
delete
public boolean delete()Deletes this file from the filesystem.- Returns:
trueif the file was successfully deleted,falseotherwise.
-
toFile
Returns the underlyingFileobject.- Returns:
- the wrapped
Fileinstance.
-