uno.informatics.common.io.text.TextFileRowWriter Maven / Gradle / Ivy
/*******************************************************************************
* Copyright 2016 Guy Davenport
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package uno.informatics.common.io.text;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
import uno.informatics.common.Constants;
import uno.informatics.common.ConversionUtilities;
import uno.informatics.common.io.RowWriter;
public class TextFileRowWriter extends AbstractTextFileHandler implements RowWriter
{
private BufferedWriter bufferedWriter;
private static final String BUFFERWRITER_NULL = "Buffer writer is undefined";
/**
* Constructs an initialised reader using a string reference to a text file.
*
* @param reference a text file name or URL
* @throws FileNotFoundException if the file to write is not found
* @throws IOException if an I/O error occurs
*/
public TextFileRowWriter(String reference) throws IOException, FileNotFoundException
{
super(reference) ;
initialise() ;
}
/**
* Constructs an initialised reader using a file.
*
* @param path the Path to file to be written.
* @throws FileNotFoundException if the file to write is not found
* @throws IOException if an I/O error occurs
*/
public TextFileRowWriter(Path path) throws IOException, FileNotFoundException
{
super(path) ;
initialise() ;
}
public TextFileRowWriter(BufferedWriter bufferedWriter) throws IOException
{
super() ;
if (bufferedWriter != null)
this.bufferedWriter = bufferedWriter ;
else
throw new IOException("Buffered writer undefined");
initialise() ;
}
@Override
public final void close()
{
try
{
if (bufferedWriter != null)
bufferedWriter.close();
}
catch (IOException e)
{
}
bufferedWriter = null ;
}
public final void flush()
{
try
{
if (bufferedWriter != null)
bufferedWriter.flush();
}
catch (IOException e)
{
}
}
@Override
public final void writeCellsAsArray(Object[][] cells) throws IOException
{
if (cells != null)
{
writeRowCellsAsArray(cells[0]) ;
for (int i = 1 ; i < cells.length ; ++i)
{
newRow() ;
writeRowCellsAsArray(cells[i]) ;
}
}
}
@Override
public final void writeCells(List> cells) throws IOException
{
if (cells != null)
{
Iterator> iterator = cells.iterator() ;
if (iterator.hasNext())
{
writeRowCells(iterator.next()) ;
while (iterator.hasNext())
{
newRow() ;
writeRowCells(iterator.next()) ;
}
}
bufferedWriter.flush();
}
}
@Override
public final void writeRowCellsAsArray(Object[] cells) throws IOException {
if (cells != null) {
writeCell(cells[0]);
for (int i = 1; i < cells.length; ++i) {
newColumn();
writeCell(cells[i]);
}
bufferedWriter.flush();
}
}
@Override
public final void writeRowCells(List