package com.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import org.apache.log4j.Logger;
import com.http.response.action.PublishAnnouncementAction;
/**
* 写文件
* @author Administrator
*
*/
public class WriteFile {
public static final Logger logger = Logger.getLogger(WriteFile.class);
/* 将对象写入文件方法 */
public void write(String content, String path, String fileName) {
FileOutputStream fileStream = null;
OutputStreamWriter os = null;
try {
File file = new File(path);
//判断文件夹是否存在,如果不存在则创建文件夹
if (!file.exists()) {
file.mkdir();
}
/* 创建存取文件 */
fileStream = new FileOutputStream(path+fileName,true);
/* 将存取文件写入对象 */
os = new OutputStreamWriter(fileStream,"utf-8");
/* 写入对象 */
os.write(content);
os.write("\r\n");
logger.info("写入数据成功"+path+fileName+"内容:"+content);
/* 关闭OutputStreamWriter */
} catch (FileNotFoundException e) {
e.printStackTrace();
StackTraceElement[] se = e.getStackTrace();
for(StackTraceElement s : se){
logger.error("异常原因:"+e.toString()+"异常地址:"+s.toString());
}
} catch (IOException e) {
e.printStackTrace();
StackTraceElement[] se = e.getStackTrace();
for(StackTraceElement s : se){
logger.error("异常原因:"+e.toString()+"异常地址:"+s.toString());
}
}finally{
try {
os.close();
fileStream.close();
} catch (IOException e) {
e.printStackTrace();
StackTraceElement[] se = e.getStackTrace();
for(StackTraceElement s : se){
logger.error("异常原因:"+e.toString()+"异常地址:"+s.toString());
}
}
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import org.apache.log4j.Logger;
import com.http.response.action.PublishAnnouncementAction;
/**
* 写文件
* @author Administrator
*
*/
public class WriteFile {
public static final Logger logger = Logger.getLogger(WriteFile.class);
/* 将对象写入文件方法 */
public void write(String content, String path, String fileName) {
FileOutputStream fileStream = null;
OutputStreamWriter os = null;
try {
File file = new File(path);
//判断文件夹是否存在,如果不存在则创建文件夹
if (!file.exists()) {
file.mkdir();
}
/* 创建存取文件 */
fileStream = new FileOutputStream(path+fileName,true);
/* 将存取文件写入对象 */
os = new OutputStreamWriter(fileStream,"utf-8");
/* 写入对象 */
os.write(content);
os.write("\r\n");
logger.info("写入数据成功"+path+fileName+"内容:"+content);
/* 关闭OutputStreamWriter */
} catch (FileNotFoundException e) {
e.printStackTrace();
StackTraceElement[] se = e.getStackTrace();
for(StackTraceElement s : se){
logger.error("异常原因:"+e.toString()+"异常地址:"+s.toString());
}
} catch (IOException e) {
e.printStackTrace();
StackTraceElement[] se = e.getStackTrace();
for(StackTraceElement s : se){
logger.error("异常原因:"+e.toString()+"异常地址:"+s.toString());
}
}finally{
try {
os.close();
fileStream.close();
} catch (IOException e) {
e.printStackTrace();
StackTraceElement[] se = e.getStackTrace();
for(StackTraceElement s : se){
logger.error("异常原因:"+e.toString()+"异常地址:"+s.toString());
}
}
}
}
}