比如如下配置
### set log levels ###
log4j.rootLogger =debug,stdout,D,E
###CONSOLE###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
###FILE###
log4j.appender.D =org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = D:\\logs\\log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %l:%t:%r ] - [ %p ] %m%n
###ERROR TO ANOTHER FILE###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = D:\\logs\\error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %l:%t:%r ] - [ %p ] %m%n
import java.io.Fileimport java.io.FileInputStreamimport java.io.FileNotFoundExceptionimport java.io.FileOutputStreamimport java.io.IOExceptionimport java.io.InputStreamimport java.io.OutputStreampublic class FileCutter { /** * *sourceFile:源文件的路径 *targetDirectory:保存文件的目录(例:'C:\\') *prefix:是分割后文件的前缀(例:'2015-09-09') *size:是分隔后单一文件的大小单位是2kb的倍数,size传10,分割后单一文件就是20K。传100,文件就是2M一个。 * **/ public static void cutToMoreFile(String sourceFile, String targetDirectory, String prefix, int size) { //加载源文件 File source = new File(sourceFile) InputStream in = null OutputStream out = null int len = 0 int fileIndex = 1 //设置一次加载的大小 byte[] buffer = new byte[2048] try{//把源文件读到InputStream中in = new FileInputStream(source) //循环while(true){//分割后的文件流 out = new FileOutputStream(targetDirectory + File.separator + prefix + fileIndex++ + ".txt")for(int i = 0i <sizei++) {//如果文件读取完就退回方法。 if((len = in.read(buffer)) != -1) { //写入分割后的文件 out.write(buffer, 0, len) }else { //执行finally内容后,退出方法 return } }} } catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace() } catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace() }finally {try {//关系流 in.close()out.close() } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace() } } } }欢迎分享,转载请注明来源:内存溢出
评论列表(0条)