This is an example of a File Read Write Class ... (see the next 3 .java files)
First the little test program, that presumes you already have a text file ...
C:\tmp\test1.txt
that has a few short lines of text
You will need all 3 java file/programs that follow, to use this 1st file, the 'test' program file ...
//package textfiles;
/*
* This test program presumes you already have a text file
* holding a few lines of text, called test1.txt
* with (Windows) path c:\tmp\test1.txt
*
*/
import java.io.IOException;
import java.util.List;
public class TestReadWriteFile
{
public static void main( String[] args ) throws IOException
{
String fname = "c:/tmp/test1.txt";
System.out.format( "Testing readFileIntoStringArray ... %n" );
//String[] aryLines = null;
try
{
//ReadFile rf = new ReadFile( fname );
//String[] aryLines = rf.readFileIntoStringArray();
String[] aryLines = ReadFile.readFileIntoStringArray( fname );
for( String line: aryLines )
System.out.println( line );
}
catch( IOException e )
{
System.err.println( e.getMessage() );
}
System.out.format( "%nTesting readFileIntoString ... %n" );
try
{
String bigString = ReadFile.readFileIntoString( fname );
System.out.println( bigString );
}
catch( IOException e )
{
System.err.println( e.getMessage() );
}
List < String > myLst = null;
System.out.format( "%nTesting readFileIntoList ... %n" );
try
{
myLst = ReadFile.readFileIntoList( fname );
for( String line: myLst )
System.out.println( line );
}
catch( IOException e )
{
System.err.println( e.getMessage() );
}
// Write to File //
System.out.format( "%nTesting writeLineToFile (appending) ... %n" );
String line = "The quick brown fox jumped through the hole in the fence.";
try
{
//WriteFile wf = new WriteFile( fname, true ); // true = append
//wf.writeLineToFile( line );
WriteFile.writeLineToFile( fname, line, true ); // true = append //
}
catch( IOException e )
{
System.err.println( e.getMessage() );
}
try
{
String longLine = ReadFile.readFileIntoString( fname ); // false = append //
System.out.println( longLine );
}
catch( IOException e )
{
System.err.println( e.getMessage() );
}
try
{
//WriteFile wf = new WriteFile( fname, true ); // true = append
//wf.writeLineToFile( line );
WriteFile.writeListToFile( fname, myLst ); // false = append //
}
catch( IOException e )
{
System.err.println( e.getMessage() );
}
}
}
Now the two files with the file read and file write classes and methods ...
//package textfiles;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class ReadFile
{
private String path = null;
public ReadFile( String path )
{
this.path = path;
}
public String[] readFileIntoStringArray() throws IOException
{
FileReader fr = new FileReader( path );
String[] data;
try( BufferedReader br = new BufferedReader( fr ) )
{
int numOfLines = readLines();
data = new String[ numOfLines ];
int i = 0;
for( ; i < numOfLines; ++i )
data[i] = br.readLine();
}
return data;
}
public static String[] readFileIntoStringArray( String path ) throws IOException
{
ReadFile rf = new ReadFile( path );
return rf.readFileIntoStringArray();
}
int readLines() throws IOException
{
FileReader fr = new FileReader( path );
int numOfLines;
try( BufferedReader br = new BufferedReader( fr ) )
{
numOfLines = 0;
String line = null;
while( ( line = br.readLine() ) != null ) ++ numOfLines;
}
return numOfLines;
}
//1. to read whole file into one big Java String
// or ...
//2. to read whole file into an ArrayList of lines (Java Strings)
private static final int AVG_NUM_CHARS_ON_LINE = 30;
public static void readFileChars( File f, StringBuffer buf ) throws IOException
{
try( BufferedReader br = new BufferedReader( new FileReader( f )) )
{
char[] cbuf = new char[ (int)f.length() ]; // f.length() returns 'long'
br.read( cbuf );
buf.append( cbuf );
}
}
// call using ... ReadWholeFile.readFileIntoString( fname );
public static String readFileIntoString( final String fname )
throws IOException
{
File f = new File( fname );
StringBuffer sb = new StringBuffer( new StringBuilder() );
readFileChars( f, sb );
return new String( sb );
}
// call using ... ReadWholeFile.readFileIntoList( fname, avgNumCharsPerLine );
public static List < String > readFileIntoList( final String fname,
int lineLen ) throws IOException
{
File f = new File( fname );
try( BufferedReader br = new BufferedReader( new FileReader( f )) )
{
List < String > myLst = new ArrayList <> ( 1 + (int)f.length() / lineLen );
String line = null;
while( ( line = br.readLine() ) != null )
myLst.add( line );
return myLst;
}
}
// call using ... ReadWholeFile.readFileIntoList( fname );
public static List < String > readFileIntoList( final String fname )
throws IOException
{
return readFileIntoList( fname, AVG_NUM_CHARS_ON_LINE );
}
}
And ...
//package textfiles;
import java.io.IOException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.List;
public class WriteFile
{
private String path;
boolean appendToFile = false;
public WriteFile( final String path )
{
this.path = path;
}
public WriteFile( final String path, boolean appendVal )
{
this.path = path;
appendToFile = appendVal;
}
public void writeLineToFile( final String line ) throws IOException
{
FileWriter fw = new FileWriter( path, appendToFile );
try( PrintWriter pw = new PrintWriter( fw ) )
{
pw.printf( "%s%n", line );
}
}
/**
*
* @param fname
* @param line
* appendVal
* @throws IOException
*/
public static void writeLineToFile( final String fname, final String line, boolean appendVal )
throws IOException
{
WriteFile wf = new WriteFile( fname, appendVal );
wf.writeLineToFile( line );
}
/**
*
* @param fname
* @param line
* @throws IOException
*/
public static void writeLineToFile( final String fname, final String line )
throws IOException
{
WriteFile wf = new WriteFile( fname );
wf.writeLineToFile( line );
}
/**
*
* @param fname
* @param String[] lines
* appendVal
* @throws IOException
*/
public static void writeArrayToFile( final String fname,
final String[] lines, boolean appendVal )
throws IOException
{
FileWriter fw = new FileWriter( fname, appendVal );
try( PrintWriter pw = new PrintWriter( fw ) )
{
for( String line: lines )
pw.printf( "%s%n", line );
}
}
/**
*
* @param fname
* @param String[] lines
* @throws IOException
*/
public static void writeArrayToFile( final String fname,
final String[] lines ) throws IOException
{
writeArrayToFile( fname, lines, false );
}
/**
*
* @param fname
* @param String[] lines
* appendVal
* @throws IOException
*/
public static void writeListToFile( final String fname,
final List < String> lines, boolean appendVal )
throws IOException
{
FileWriter fw = new FileWriter( fname, appendVal );
try( PrintWriter pw = new PrintWriter( fw ) )
{
for( String line: lines )
pw.printf( "%s%n", line );
}
}
/**
*
* @param fname
* @param String[] lines
* @throws IOException
*/
public static void writeListToFile( final String fname,
final List < String > lines ) throws IOException
{
writeListToFile( fname, lines, false );
}
}