java 输出文本_Java文本输出常用类

package com.ss.extract;

import java.io.*;

import org.apache.log4j.Logger;

/**

* 结果输出类

* @author Shu

*/

public class ResultOutput {

private static Logger log = Logger.getLogger(ResultOutput.class);

/**

* 将结果输出到txt文本文件中,其中的StaticValue.ABSTRACT_SAVE_PATH和StaticValue.FILE_NAME为输出

* 地址

* @param contents

*/

public void outputToTxt(String content){

File txt = creatTxtFile(StaticValue.ABSTRACT_SAVE_PATH,StaticValue.FILE_NAME);

FileOutputStream fileOutputStream = null;

BufferedOutputStream bufferedOutputStream = null;

OutputStreamWriter outputStreamWriter = null;

BufferedWriter out = null;

try {

fileOutputStream = new FileOutputStream(txt);

bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

outputStreamWriter = new OutputStreamWriter(bufferedOutputStream);

out = new BufferedWriter(outputStreamWriter);

out.write(content);

out.flush();

} catch (FileNotFoundException e) {

log.error("文件不存在!");

e.printStackTrace();

} catch (IOException e) {

log.error("文本摘要存储失败");

e.printStackTrace();

}finally{

if(out != null){

try {

out.close();

} catch (IOException e) {

log.error("BufferedWriter关闭失败!");

e.printStackTrace();

}

}

if(outputStreamWriter != null){

try {

outputStreamWriter.close();

} catch (IOException e) {

log.error("outputStreamWriter关闭失败!");

e.printStackTrace();

}

}

if(bufferedOutputStream != null){

try {

bufferedOutputStream.close();

} catch (IOException e) {

log.error("bufferedOutputStream关闭失败!");

e.printStackTrace();

}

}

if(fileOutputStream != null){

try {

fileOutputStream.close();

} catch (IOException e) {

log.error("fileOutputStream关闭失败!");

e.printStackTrace();

}

}

}

}

/**

* 创建文件

* @param path 文件存储路径

* @param name 文件名字

* @return File

*/

private File creatTxtFile(String path, String name){

File fileName = null;

fileName = new File(path + "\\" + name);

if (!fileName.exists()) {

try {

fileName.createNewFile();

} catch (IOException e) {

log.error("文件创建失败!");

e.printStackTrace();

}

}

return fileName;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值