class="File Write Interface" name="code">package cn.newtouch.service; import java.nio.charset.Charset; /** * The fileWriterService. * * @author e360612 */ public interface SfjFileWriterServiceBatch { /** * Instantiates a new FileWriter. * * @param fileName * fileName @ if problem occurred */ void open(String fileName); /** * Write a line of the file with line separator in default charset * configured in parameter table. * * @param line * the line @ if problem occurred */ void writeLine(String line); /** * Write a line of the file with/without line separator in default charset * configured in parameter table. * * @param line * the line * @param wrapLine * if next line needs a wrap @ if problem occurred */ void writeLine(String line, boolean wrapLine); /** * Write line in customized charset. * * @param line * the line * @param charset * the charset @ the sfj batch technical exception */ void writeLine(String line, Charset charset); /** * Write a line of the file with/without line separator in customized * charset. * * @param line * the line * @param wrapLine * the wrap line * @param charset * the charset @ the sfj batch technical exception */ void writeLine(String line, boolean wrapLine, Charset charset); /** * Write a line of the file with buffer. * * @param buffer * the line * @param count * the line @ if problem occurred */ void writeLine(byte[] buffer, int count); /** * Close the file. * * @ if problem occurred */ void close(); /** * Getter compteur. * * @return the compteur */ long getCompteur(); }
?
package cn.newtouch.service.impl;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import cn.newtouch.service.SfjFileWriterServiceBatch;
/**
* FileWriterService implementation.
*
* @author e397930
*/
public class SfjFileWriterServiceBatchImpl implements SfjFileWriterServiceBatch {
/** The output stream. */
private FileOutputStream outputStream;
/** The file in channel. */
private FileChannel fileOutChannel;
/** file name. */
@SuppressWarnings("unused")
private String fileName;
/** The Constant LINE_SEPARATOR. */
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
/** Compteur... */
private long compteur;
/**
* FileWriter constructor.
*/
public SfjFileWriterServiceBatchImpl() {
}
/**
*
* {@inheritDoc}
*
* @see com.inetpsa.sfj.servicebatch.fileio.SfjFileWriterServiceBatch#open(java.lang.String)
*/
public void open(String fileName) {
try {
this.fileName = fileName;
File file = new File(fileName);
if (file.exists()) {
file.delete();
}
File folder = file.getParentFile();
folder.mkdirs();
outputStream = new FileOutputStream(fileName);
fileOutChannel = outputStream.getChannel();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
*
* {@inheritDoc}
*
* @see com.inetpsa.sfj.servicebatch.fileio.SfjFileWriterServiceBatch#writeLine(java.lang.String)
*/
public void writeLine(String line) {
this.writeLine(line, true, null);
}
/**
* {@inheritDoc}
*
* @see com.inetpsa.sfj.servicebatch.fileio.SfjFileWriterServiceBatch#writeLine(java.lang.String,
* java.nio.charset.Charset)
*/
public void writeLine(String line, Charset charset) {
this.writeLine(line, true, charset);
}
/**
*
* {@inheritDoc}
*
* @see com.inetpsa.sfj.servicebatch.fileio.SfjFileWriterServiceBatch#writeLine(java.lang.String,
* boolean)
*/
public void writeLine(String line, boolean wrapLine) {
writeLine(line, wrapLine, null);
}
/**
* {@inheritDoc}
*
* @see com.inetpsa.sfj.servicebatch.fileio.SfjFileWriterServiceBatch#writeLine(java.lang.String,
* boolean, java.nio.charset.Charset)
*/
public void writeLine(String line, boolean wrapLine, Charset charset) {
try {
Charset outputCharset = charset;
if (fileOutChannel != null) {
String content = line;
if (wrapLine) {
content = line + LINE_SEPARATOR;
}
fileOutChannel.write(ByteBuffer.wrap(content.getBytes(outputCharset)));
}
compteur++;
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* {@inheritDoc}
*
* @see com.inetpsa.sfj.servicebatch.fileio.SfjFileWriterServiceBatch#writeLine(byte[],
* int)
*/
@Override
public void writeLine(byte[] buffer, int count) {
throw new NotImplementedException();
}
/**
*
* {@inheritDoc}
*
* @see com.inetpsa.sfj.servicebatch.fileio.SfjFileWriterServiceBatch#close()
*/
public void close() {
if (fileOutChannel != null) {
try {
fileOutChannel.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Getter compteur.
*
* @return the compteur
*/
public long getCompteur() {
return compteur;
}
}
?