axjs2 file 实现log4j
2011-12-06 00:21
253 查看
package com.server;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileTest {
private static int TOTAL = 0;
private static int SUCCESS_COUNT=0;
private static String FILE_NAME = "D:\\writeFile\\wlog.log";
public int getRes(int infoType,String arg1,String arg2,String arg3,String arg4,String arg5,String arg6){
StringBuffer sb = new StringBuffer();
sb.append("\r\n");
sb.append("-------------------------getSomeInfo params list start----------------------");
sb.append("\r\n");
sb.append("infoType:"+infoType);
sb.append("\r\n");
sb.append("arg1:"+arg1);
sb.append("\r\n");
sb.append("arg2:"+arg2);
sb.append("\r\n");
sb.append("arg3:"+arg3);
sb.append("\r\n");
sb.append("arg4:"+arg4);
sb.append("\r\n");
sb.append("arg5:"+arg5);
sb.append("\r\n");
sb.append("arg6:"+arg6);
sb.append("\r\n");
sb.append("-------------------------getSomeInfo params list end----------------------");
sb.append("\r\n");
TOTAL = TOTAL+1;
int intRes = -1;
if(null == arg1 || "".equals(arg1)){
return intRes;
}
if("good".equalsIgnoreCase(arg1) ){
SUCCESS_COUNT = SUCCESS_COUNT +1;
}
StringBuffer res = new StringBuffer();
res.append("\r\n");
res.append("-------------------------counter list start----------------------");
res.append("\r\n");
res.append("TOTAL:"+TOTAL);
res.append("\r\n");
res.append("SUCCESS_COUNT:"+SUCCESS_COUNT);
res.append("\r\n");
res.append("-------------------------counter list end----------------------");
res.append("\r\n");
try {
toWriteFile(FILE_NAME,sb.toString());
toWriteFile(FILE_NAME, res.toString());
} catch (IOException e) {
e.printStackTrace();
}
return intRes;
}
public void toWriteFile(String fileName,String content) throws IOException{
File file = new File(fileName);
if(!file.exists()){
file.createNewFile();
}else {
FileInputStream fis = new FileInputStream(file);
if(fis.available()/1000 >= 1){
String newFileName = "D:\\writeFile\\"+getCurrentTime("yyyyMMddHHmmss")+".log";
file = new File(newFileName);
file.createNewFile();
}
}
FileOutputStream out = new FileOutputStream(file,true);
OutputStreamWriter os = new OutputStreamWriter(out);
BufferedWriter bw = new BufferedWriter(os);
bw.write(getCurrentTime("yyyy-MM-dd HH:mm:ss"));
bw.write(content);
bw.close();
}
public String getCurrentTime(String format){
Date currentTime = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
String dateString = dateFormat.format(currentTime);
return dateString;
}
public static void main(String[] args) {
// int res = getRes(2,"a","b","c","d","e","f");
// System.out.println("res:"+res);
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileTest {
private static int TOTAL = 0;
private static int SUCCESS_COUNT=0;
private static String FILE_NAME = "D:\\writeFile\\wlog.log";
public int getRes(int infoType,String arg1,String arg2,String arg3,String arg4,String arg5,String arg6){
StringBuffer sb = new StringBuffer();
sb.append("\r\n");
sb.append("-------------------------getSomeInfo params list start----------------------");
sb.append("\r\n");
sb.append("infoType:"+infoType);
sb.append("\r\n");
sb.append("arg1:"+arg1);
sb.append("\r\n");
sb.append("arg2:"+arg2);
sb.append("\r\n");
sb.append("arg3:"+arg3);
sb.append("\r\n");
sb.append("arg4:"+arg4);
sb.append("\r\n");
sb.append("arg5:"+arg5);
sb.append("\r\n");
sb.append("arg6:"+arg6);
sb.append("\r\n");
sb.append("-------------------------getSomeInfo params list end----------------------");
sb.append("\r\n");
TOTAL = TOTAL+1;
int intRes = -1;
if(null == arg1 || "".equals(arg1)){
return intRes;
}
if("good".equalsIgnoreCase(arg1) ){
SUCCESS_COUNT = SUCCESS_COUNT +1;
}
StringBuffer res = new StringBuffer();
res.append("\r\n");
res.append("-------------------------counter list start----------------------");
res.append("\r\n");
res.append("TOTAL:"+TOTAL);
res.append("\r\n");
res.append("SUCCESS_COUNT:"+SUCCESS_COUNT);
res.append("\r\n");
res.append("-------------------------counter list end----------------------");
res.append("\r\n");
try {
toWriteFile(FILE_NAME,sb.toString());
toWriteFile(FILE_NAME, res.toString());
} catch (IOException e) {
e.printStackTrace();
}
return intRes;
}
public void toWriteFile(String fileName,String content) throws IOException{
File file = new File(fileName);
if(!file.exists()){
file.createNewFile();
}else {
FileInputStream fis = new FileInputStream(file);
if(fis.available()/1000 >= 1){
String newFileName = "D:\\writeFile\\"+getCurrentTime("yyyyMMddHHmmss")+".log";
file = new File(newFileName);
file.createNewFile();
}
}
FileOutputStream out = new FileOutputStream(file,true);
OutputStreamWriter os = new OutputStreamWriter(out);
BufferedWriter bw = new BufferedWriter(os);
bw.write(getCurrentTime("yyyy-MM-dd HH:mm:ss"));
bw.write(content);
bw.close();
}
public String getCurrentTime(String format){
Date currentTime = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
String dateString = dateFormat.format(currentTime);
return dateString;
}
public static void main(String[] args) {
// int res = getRes(2,"a","b","c","d","e","f");
// System.out.println("res:"+res);
}
}
相关文章推荐
- 自定义实现log4j的FileAppender
- Log4j 重写 RollingFileAppender 实现按日期生成文件夹文件名
- 利用File类来实现对文件和文件夹数目的统计
- 简单获取input file 选中的图片,并在一个div的img里面赋值src实现预览图片
- input type=file 选择图片并且实现预览效果的实例
- SystemTimeToFileTime、FileTimeToLocalFileTime、LocalFileTimeToFileTime三函数的跨平台实现
- log4j1 修改FileAppender解决当天的文件没有日期后缀
- HFileInputFormat实现
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- Yii2实现log输出到file及database的方法
- __FILE__,__LINE__,FUNCTION__实现代码跟踪调试(linux下c语言编程 )
- Yii2使用自带的UploadedFile实现的文件上传
- Android学习之实现WebView中input="file"选择文件,处理选择图片无法返回类型问题
- 用tarball实现liferay自动安装部署13-从Storage Server下载tomcat zip file
- Spring MVC使用commons fileupload实现文件上传功能
- js 实现 input file 文件上传
- Log4j实现对Java日志的配置全攻略
- 最近使用ajaxFileUpload和Jcrop来实现图片上传和截图,出现一个图片无法更换的问题
- C++ 实现Web File Server
- 使用jquery插件uploadify结合commons fileupload实现多文件上传