Michael Thomas Flanagan's Java Scientific Library

FileManipulate Class:      Reading Characters from a File and Reorganising Line Lengths

     

Last update: 20 May 2012
Main Page of Michael Thomas Flanagan's Java Scientific Library

This class contains easy to use methods for reading in a file character by character, including non-print characters, e.g. line return character, as and
This file incorporates and replaces FileInputAsChar. FileInputAsChar will be retained for compatiblity.

import directive: import flanagan.io.FileManipulate;
Related classes:

SUMMARY OF CONSTRUCTOR AND METHODS

Constructor public FileManipulate(String filename)
Read the next character as char public final synchronized char readchar()
Read the next character as Char public final synchronized Character readCharacter()
Read the next character as ISO code (int) public final synchronized int readint()
Return file with lines of given length public void toLineLength(int requiredLineLength, String filename)
public void toLineLength(int requiredLineLength)
Return file of a single line public void toSingleLineFile(String filename)
public void toSingleLineFile(int requiredLineLength)
Convert all lines to a single line String public String toSingleLine()
End of file status public String eof()
File existence status public boolean fileFound()
Close the file public final synchronized void close()
Get the file name public String getFileName()
Get the file name without its extension public String getStemName()
Get the path name public String getPathName()
Get the directory path public String getDirPath()

METHODS

CONSTRUCTOR
public FileManipulate(String filename)
Usage:                      FileManipulate fin = new FileManipulate(filename);
Creates a new instance of FileInput that opens an existing text file called filename. The argument may be simply the file name, e.g. ""inputData.txt, if opening a file in the working directory. If the file is in another directorry, filename must be the full path name, e.g. on a MicroSoft operating system; "C:\\JavaProg\\Experiments\\Exp1\\inputData2.txt". The file name should contain the relevant extension, e.g. filename = "xydata.txt". See FileChooser for choosing the filename through an Open File dialog window.
Remember you must also close the file opened once you have finished reading from it (see close() method).

ENTERING CHARACTERS (char)
ENTERING THE NEXT CHARACTER IN THE FILE AS TYPE char
public final synchronized char readchar()
Usage:                      char x = fin.readchar();
A method for reading the next character in the text file accessed by fin, as type char. Returns '\u0000' if attempt to read beyond the end of file is made.

ENTERING THE NEXT CHARACTER IN THE FILE AS A Character INSTANCE
public final synchronized Character readCharacter()
Usage:                      Character x = fin.readCharacter();
A method for reading the next character in the text file accessed by fin, as an instance of Character. Returns null if attempt to read beyond the end of file is made.

ENTERING THE NEXT CHARACTER IN THE FILE AS ITS ISO CODE (type int)
public final synchronized int readint()
Usage:                      int x = fin.readint();
A method for reading the next character in the text file accessed by fin, as its ISO code int equivalent (http://unicode.org/). Returns -1 if attempt to read beyond the end of file is made.



RETURN FILE WITH LINES OF GIVEN LENGTH
public void toLineLength(int requiredLineLength, String filename)
public void toLineLength(int requiredLineLength)
Usage:                      int x = fin.toLineLength(requiredLineLength, filename);
This method reads in all the lines of the file designated in the constructor argument and outputs the content as a text file in which the lines are of a maximum number of characters passed as the argument requiredLineLength. The name of the output file is the name passed as the argument, filename.

Usage:                      int x = fin.toLineLength(requiredLineLength);
This method performs the same actions as toLineLength(requiredLineLength, filename), above, with the exception that a dialogue box will now request the output file name and will offfer a default name based on the input file name.

RETURN FILE OF A SINGLE LINE
public void toSingleLineFile(String filename)
public void toSingleLineFile()
Usage:                      fin.toSingleLineFile(filename);
This method reads in all the lines of the file designated in the constructor argument and outputs the content as a text file of a single line. The name of the output file is the name passed as the argument, filename.

Usage:                      x = fin.toSingleLineFile();
This method performs the same actions as toSingleLineFile(filename), above, with the exception that a dialogue box will now request the output file name and will offfer a default name based on the input file name.

CONVERT ALL LINES TO A SINGLE LINE String
public String toSingleLine()
Usage:                      String x = fin.toSingleLine();
This method reads in all the lines of the file designated in the constructor argument and returns them as a single line String, i.e. as a String in which all control characters have been removed.



END OF FILE STATUS
public boolean eof()
Usage:                      boolean bl = fin.eof();
Returns false if no attempt to read past the emd of file is made. Returns true if an attempt is made to read past the end of the file. Attempts to read a double, float, int or long after the end of file leads to zero being returned. An attempt to read a char after the end of file leads to a space being returned. An attempt to read a String after the end of file leads to an empty string being returned. In each case an error message is written to the screen.

FILE EXISTENCE STATUS
public boolean fileFound()
Usage:                      boolean ffd = fin.fileFound();
Returns true if the file whose filename has been passed to the instance of FileInput, fin, was found. Returns false if that file was not found.

GETTING THE PATH
public String getFilePath()
Usage:                      String path = fin.getPathName();
A method for returning the path name, e.g. "C:\JavaProg\Experiments\Exp1\inputData2.txt".

GETTING THE FILE NAME
public String getFileName()
Usage:                      String name = fin.getFileName();
A method for returning the file name, e.g. "inputData2.txt".

GETTING THE FILE NAME WITHOUT ITS EXTENSION
public String getStemName()
Usage:                      String name = fin.getStemName();
A method for returning the file name without its extension, e.g. "inputData2".

GETTING THE PATH TO THE DIRECTORY HOLDING THE INPUT FILE
public String getDirPath()
Usage:                      String dir = fin.getDirPath();
A method for returning the path to the directory holding bthe input file, e.g. "C:\JavaProg\Experiments\Exp1" where the full path name is "C:\JavaProg\Experiments\Exp1\inputData2.txt".

CLOSING THE FILE
public final synchronized void close()
Usage:                      fin.close();
A method for closing the text file opened by fin.



OTHER CLASSES USED BY THIS CLASS

This class uses the following classes in this library:



This page was prepared by Michael Thomas Flanagan