Class TextFile

java.lang.Object
com.netlang.io.TextFile

public class TextFile extends Object
Class for reading and writing text files. After opening a file for read or write it must be closed.
Example - Write lines to a new file.

  		TextFile file1 =  new TextFile(new File("C:\\Temp\\test.txt"),TextFile.CREATE,"unicode-1-1-utf-8");
  		file1.write("Hello World!");
  		file1.close();
  

Example - Read 10 lines from a text file at the local file system.

  		TextFile file1 =  new TextFile(new File("C:\\Temp\\test.txt"),TextFile.OPEN,"UTF-8");
  		for(int i = 0; i < 10; i++) {
  			Log.info(file1.readLine(), null);
  		}
  		file1.close();
  
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static int
    Append mode
    static int
    Create mode
    static int
    Open only mode
    static int
    Temporary file.
  • Constructor Summary

    Constructors
    Constructor
    Description
    TextFile(NetlangFile file, int openoption, String encoding)
    Create a new Text file for reading or writing.
    TextFile(com.netlang.session.NetlangSession session, NetlangFile file, int openoption, String encoding)
    Create a new Text file for reading or writing.
    TextFile(com.netlang.session.NetlangSession session, String id, String path, int openoption, String encoding)
    Create a new Text file for reading or writing.
    TextFile(File file, int openoption, String encoding)
    Create a new Text file for reading or writing.
    TextFile(String filepath, int openoption, String encoding)
    Create a new Text file for reading or writing.
    TextFile(String id, String path, int openoption, String encoding)
    Create a new Text file for reading or writing.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Close this file.
    static void
    main(String[] args)
     
    void
    Open the file for read or write, this function will be called when a new instance of this class is created.
    Read all lines in this file.
    Read one line from the file.
    void
    Writes the passed string to the file
    void
    Writes the passed string with EOL.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • CREATE

      public static int CREATE
      Create mode
    • OPEN

      public static int OPEN
      Open only mode
    • APPEND

      public static int APPEND
      Append mode
    • TEMPORARY

      public static int TEMPORARY
      Temporary file.
  • Constructor Details

    • TextFile

      public TextFile(String id, String path, int openoption, String encoding) throws com.net4z.APIException
      Create a new Text file for reading or writing.
      Parameters:
      id -
      path -
      openoption -
      encoding -
      Throws:
      com.net4z.APIException
    • TextFile

      public TextFile(String filepath, int openoption, String encoding) throws com.net4z.APIException
      Create a new Text file for reading or writing.
      Parameters:
      filepath -
      openoption -
      encoding -
      Throws:
      com.net4z.APIException
    • TextFile

      public TextFile(File file, int openoption, String encoding) throws com.net4z.APIException
      Create a new Text file for reading or writing.
      Parameters:
      file -
      openoption -
      encoding -
      Throws:
      com.net4z.APIException
    • TextFile

      public TextFile(NetlangFile file, int openoption, String encoding) throws com.net4z.APIException
      Create a new Text file for reading or writing.
      Parameters:
      file -
      openoption -
      encoding -
      Throws:
      com.net4z.APIException
    • TextFile

      public TextFile(com.netlang.session.NetlangSession session, NetlangFile file, int openoption, String encoding) throws com.net4z.APIException
      Create a new Text file for reading or writing.
      Parameters:
      session -
      file -
      openoption -
      encoding -
      Throws:
      com.net4z.APIException
    • TextFile

      public TextFile(com.netlang.session.NetlangSession session, String id, String path, int openoption, String encoding) throws com.net4z.APIException
      Create a new Text file for reading or writing.
      Parameters:
      session - the Netlang session
      id - the resource id
      path - the relative path to the file
      openoption - Append, Create or Open
      encoding - the text encoding, e.g. "UTF-8"
      Throws:
      com.net4z.APIException
  • Method Details

    • main

      public static void main(String[] args) throws Exception
      Throws:
      Exception
    • open

      public void open() throws com.net4z.APIException
      Open the file for read or write, this function will be called when a new instance of this class is created.
      Throws:
      com.net4z.APIException
    • write

      public void write(String v) throws com.net4z.APIException
      Writes the passed string to the file
      Parameters:
      v -
      Throws:
      Exception
      com.net4z.APIException
    • writeLine

      public void writeLine(String v) throws com.net4z.APIException
      Writes the passed string with EOL.
      Parameters:
      v -
      Throws:
      Exception
      com.net4z.APIException
    • readLine

      public String readLine() throws com.net4z.APIException
      Read one line from the file.
      Returns:
      Throws:
      com.net4z.APIException
    • readAll

      public String readAll() throws com.net4z.APIException
      Read all lines in this file.
      Returns:
      Throws:
      com.net4z.APIException
    • close

      public void close() throws com.net4z.APIException
      Close this file.
      Throws:
      com.net4z.APIException