Class ServerFile

java.lang.Object
com.oorian.utils.OorianFile
com.oorian.utils.ServerFile

public class ServerFile extends OorianFile
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

    Constructors
    Constructor
    Description
    Creates a new ServerFile wrapping the given File.
    Creates a new ServerFile for the given filesystem path.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    append(String content)
    Appends the given string to this file using UTF-8 encoding, creating the file if it does not exist.
    boolean
    Deletes this file from the filesystem.
    boolean
    Returns whether this file exists and is accessible.
    static boolean
    exists(String path)
    Returns whether a file exists at the given filesystem path.
    static ServerFile
    Creates a ServerFile by resolving a path relative to the web application root.
    Returns an InputStream for reading this file's contents.
    Returns the filename extracted from the path, handling both forward and back slashes.
    Returns an OutputStream for writing to this file.
    Returns the path of this file as provided at construction time.
    long
    Returns the time this file was last modified, in milliseconds since the epoch.
    long
    Returns the length of this file in bytes.
    static String
    read(String path)
    Reads the entire contents of a file as a UTF-8 string.
    Returns the underlying File object.
    void
    write(byte[] data)
    Writes the given byte array to this file, creating the file if it does not exist and overwriting any existing contents.
    void
    write(String content)
    Writes the given string to this file using UTF-8 encoding, creating the file if it does not exist and overwriting any existing contents.
    void
    write(String content, Charset charset)
    Writes 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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ServerFile

      public ServerFile(String path)
      Creates a new ServerFile for the given filesystem path.
      Parameters:
      path - the absolute or relative filesystem path.
    • ServerFile

      public ServerFile(File file)
      Creates a new ServerFile wrapping the given File.
      Parameters:
      file - the file to wrap.
  • Method Details

    • exists

      public static boolean exists(String path)
      Returns whether a file exists at the given filesystem path.
      Parameters:
      path - the absolute or relative filesystem path.
      Returns:
      true if a file exists at the path, false otherwise.
    • read

      public static String read(String path) throws IOException
      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

      public static ServerFile fromWebApp(String path)
      Creates a ServerFile by 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 ServerFile for the resolved path.
      Throws:
      IllegalStateException - if the application context is not available or the path cannot be resolved.
    • getInputStream

      public InputStream getInputStream() throws IOException
      Returns an InputStream for reading this file's contents.
      Specified by:
      getInputStream in class OorianFile
      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:
      exists in class OorianFile
      Returns:
      true if the file exists, false otherwise.
    • getPath

      public String getPath()
      Returns the path of this file as provided at construction time.
      Specified by:
      getPath in class OorianFile
      Returns:
      the file path.
    • getName

      public String getName()
      Returns the filename extracted from the path, handling both forward and back slashes.
      Overrides:
      getName in class OorianFile
      Returns:
      the filename portion of the path.
    • write

      public void write(String content) throws IOException
      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

      public void write(String content, Charset charset) throws IOException
      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

      public void write(byte[] data) throws IOException
      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

      public OutputStream getOutputStream() throws IOException
      Returns an OutputStream for 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

      public void append(String content) throws IOException
      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 0 if 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 0 if the file does not exist.
    • delete

      public boolean delete()
      Deletes this file from the filesystem.
      Returns:
      true if the file was successfully deleted, false otherwise.
    • toFile

      public File toFile()
      Returns the underlying File object.
      Returns:
      the wrapped File instance.