问题描述
原文引用:1.下载apache-Log4jFileUtil-1.2.16.tar.gz支持Logger.getLogger方法http://archive.apache.org/dist/logging/Log4jFileUtil/1.2.16/apache-Log4jFileUtil-1.2.16.tar.gzant-1.6.5.jarhttp://repo1.maven.org/maven2/ant/ant/1.6.5/ant-1.6.5.jareclipse-jee-helios-SR1-win32.ziphttp://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/helios/SR1/eclipse-jee-helios-SR1-win32.zip&url=http://download.actuatechina.com/eclipse/technology/epp/downloads/release/helios/SR1/eclipse-jee-helios-SR1-win32.zip&mirror_id=385jdk-6u21-windows-i586.exe下面的地址需要自己选择自己要的jdkhttp://www.oracle.com/technetwork/java/archive-139210.html2.提取包从apache-Log4jFileUtil-1.2.16.tar.gz中apache-Log4jFileUtil-1.2.16提取如下包apache-Log4jFileUtil-1.2.16.jar下载ant-1.6.5.jarhttp://repo1.maven.org/maven2/ant/ant/1.6.5/ant-1.6.5.jar把这些包拷贝到一个Log4j-FileUtil-lib文件夹中(这个文件夹自己创建一个,主要用途是临时保存这些包)3.安装JDK和解压Eclipse文件4.Eclipse配置Tomcat和JDKa.启动Eclipse(我用的是Eclipse3.6forJEE)Window-->Preferences-->Java-->Add...-->StandardVM-->Next-->Directory...-->执行jdk1.6.0_21的文件夹(我的在C:ProgramFilesJavajdk1.6.0_21)-->Finish-->选择jdk1.6.0_21-->OK-->JRE:jdk1.6.0_21-->Finish-->OK5.创建第1个例子a.创建Hibernate项目点击菜单File-->New-->Other...-->Java-->JavaProject-->Next-->Projectname:Log4jFileUtil-->Finish-->Yes-->b.配置项目为UTF-8格式打开右键点击Log4jFileUtil-->Properties-->Resource-->Textfileencoding选择Other:UTF-8-->OKc.配置程序1)在Log4jFileUtilWebRootWEB-INFlib下,把提取出来的放在Log4j-FileUtil-lib文件夹中的jar包拷贝到这里-->右键点击Log4jFileUtil-->BuildPath-->ConfigureBuildPath...-->Libraries-->AddJARs-->选中Log4jFileUtillib下所有的Jar包(按住Shift从头选到尾)-->-->OK-->OK-->2)右键点击Log4jFileUtilsrc-->New-->File-->Filename:Log4jFileUtil.properties-->Finish-->Log4jFileUtil.properties修改成如下:#SetrootloggerleveltoerrorLog4jFileUtil.rootLogger=INFO,Console,File
解决方案二:
解决方案三:
Consoleappenderdefinition
解决方案四:
解决方案五:
##AlloutputscurrentlysettobeaConsoleAppender.Log4jFileUtil.appender.Console=org.apache.Log4jFileUtil.ConsoleAppenderLog4jFileUtil.appender.Console.layout=org.apache.Log4jFileUtil.PatternLayoutLog4jFileUtil.appender.Console.layout.ConversionPattern=%d{ABSOLUTE}%-5p[%c{3}]%m%n
解决方案六:
解决方案七:
Fileappenderdefinition
解决方案八:
解决方案:
#Log4jFileUtil.appender.File=org.apache.Log4jFileUtil.DailyRollingFileAppenderLog4jFileUtil.appender.File.File=system.logLog4jFileUtil.appender.File.Append=falseLog4jFileUtil.appender.File.layout=org.apache.Log4jFileUtil.PatternLayoutLog4jFileUtil.appender.File.layout.ConversionPattern=%d{ABSOLUTE}%-5p[%c]%m%n3)建立文件操作类;右键点击Log4jFileUtilsrc-->New-->Class-->Packege:com.common.util.file-->Name:FileUtil-->Finish-->FileUtil.java修改成如下:packagecom.common.util.file;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.Enumeration;importorg.apache.log4j.Logger;importorg.apache.tools.zip.ZipEntry;importorg.apache.tools.zip.ZipFile;importorg.apache.tools.zip.ZipOutputStream;publicclassFileUtil{protectedstaticLoggerlog=Logger.getLogger(FileUtil.class);/****压缩文件**@paraminputFileName*要压缩的文件或文件夹路径,例如:c:\a.txt,c:\a**@paramoutputFileName*输出zip文件的路径,例如:c:\a.zip*/publicstaticbooleanzip(StringinputFileName,StringoutputFileName)throwsException{if(inputFileName==null||outputFileName==null){returnfalse;}ZipOutputStreamout=null;try{out=newZipOutputStream(newFileOutputStream(outputFileName));zip(out,newFile(inputFileName),"");log.debug("压缩完成!");}catch(Exceptione){log.error("",e);throwe;}finally{if(out!=null){out.closeEntry();out.close();}}returntrue;}/****增加压缩文件**@paramout*org.apache.tools.zip.ZipOutputStream**@paramfile*待压缩的文件**@parambase*压缩的根目录*/privatestaticvoidzip(ZipOutputStreamout,Filefile,Stringbase)throwsException{if(file.isDirectory()){File[]fl=file.listFiles();base=base.length()==0?"":base+File.separator;for(inti=0;i<fl.length;i++){zip(out,fl[i],base+fl[i].getName());}}else{out.putNextEntry(newZipEntry(base));log.debug("添加压缩文件:"+base);FileInputStreamin=newFileInputStream(file);intb;while((b=in.read())!=-1){out.write(b);}in.close();}}/****解压zip文件**@paramzipFileName*待解压的zip文件路径,例如:c:\a.zip**@paramoutputDirectory*解压目标文件夹,例如:c:\a*/publicstaticvoidunZip(StringzipFileName,StringoutputDirectory)throwsException{ZipFilezipFile=newZipFile(zipFileName);try{Enumeration<?>e=zipFile.getEntries();ZipEntryzipEntry=null;createDirectory(outputDirectory,"");while(e.hasMoreElements()){zipEntry=(ZipEntry)e.nextElement();log.debug("解压:"+zipEntry.getName());if(zipEntry.isDirectory()){Stringname=zipEntry.getName();name=name.substring(0,name.length()-1);Filef=newFile(outputDirectory+File.separator+name);f.mkdir();log.debug("创建目录:"+outputDirectory+File.separator+name);}else{StringfileName=zipEntry.getName();fileName=fileName.replace('\','/');if(fileName.indexOf("/")!=-1){createDirectory(outputDirectory,fileName.substring(0,fileName.lastIndexOf("/")));fileName=fileName.substring(fileName.lastIndexOf("/")+1,fileName.length());}Filef=newFile(outputDirectory+File.separator+zipEntry.getName());f.createNewFile();InputStreamin=zipFile.getInputStream(zipEntry);FileOutputStreamout=newFileOutputStream(f);byte[]by=newbyte[1024];intc;while((c=in.read(by))!=-1){out.write(by,0,c);}in.close();out.close();}}}catch(Exceptionex){System.out.println(ex.getMessage());}finally{zipFile.close();log.debug("解压完成!");}}privatestaticvoidcreateDirectory(Stringdirectory,StringsubDirectory){Stringdir[];Filefl=newFile(directory);try{if(subDirectory==""&&fl.exists()!=true){fl.mkdir();}elseif(subDirectory!=""){dir=subDirectory.replace('\','/').split("/");for(inti=0;i<dir.length;i++){FilesubFile=newFile(directory+File.separator+dir[i]);if(subFile.exists()==false)subFile.mkdir();directory+=File.separator+dir[i];}}}catch(Exceptionex){System.out.println(ex.getMessage());}}/****拷贝文件夹中的所有文件到另外一个文件夹**@paramsrcDirector*源文件夹**@paramdesDirector*目标文件夹*/publicstaticvoidcopyFileWithDirector(StringsrcDirector,StringdesDirector)throwsIOException{(newFile(desDirector)).mkdirs();File[]file=(newFile(srcDirector)).listFiles();for(inti=0;i<file.length;i++){if(file[i].isFile()){log.debug("拷贝:"+file[i].getAbsolutePath()+"-->"+desDirector+"/"+file[i].getName());FileInputStreaminput=newFileInputStream(file[i]);FileOutputStreamoutput=newFileOutputStream(desDirector+"/"+file[i].getName());byte[]b=newbyte[1024*5];intlen;while((len=input.read(b))!=-1){output.write(b,0,len);}output.flush();output.close();input.close();}if(file[i].isDirectory()){log.debug("拷贝:"+file[i].getAbsolutePath()+"-->"+desDirector+"/"+file[i].getName());copyFileWithDirector(srcDirector+"/"+file[i].getName(),desDirector+"/"+file[i].getName());}}}/****删除文件夹**@paramfolderPath*folderPath文件夹完整绝对路径*/publicstaticvoiddelFolder(StringfolderPath)throwsException{//删除完里面所有内容delAllFile(folderPath);StringfilePath=folderPath;filePath=filePath.toString();FilemyFilePath=newFile(filePath);//删除空文件夹myFilePath.delete();}/****删除指定文件夹下所有文件**@parampath*文件夹完整绝对路径*/publicstaticbooleandelAllFile(Stringpath)throwsException{booleanflag=false;Filefile=newFile(path);if(!file.exists()){returnflag;}if(!file.isDirectory()){returnflag;}String[]tempList=file.list();Filetemp=null;for(inti=0;i<tempList.length;i++){if(path.endsWith(File.separator)){temp=newFile(path+tempList[i]);}else{temp=newFile(path+File.separator+tempList[i]);}if(temp.isFile()){temp.delete();}if(temp.isDirectory()){//先删除文件夹里面的文件delAllFile(path+"/"+tempList[i]);//再删除空文件夹delFolder(path+"/"+tempList[i]);flag=true;}}returnflag;}}-->保存-->来源于网络,回归于网络。我的邮箱:happy.every.day@126.comQQ:48399956快乐!2010年10月26日
解决方案
解决方案:
4)建立一个测试类:右键点击Log4jFileUtil-->New-->SourceFolder-->Foldername:test-->Finish-->右键点击Log4jFileUtil-->BuildPath-->ConfigureBuildPath...-->Libraries-->AddLibrary...-->JUnit-->Next-->JUnitlibrarayversion:JUnit4(没有就选择JUnit3)-->Finish-->OK-->右键点击Log4jFileUtiltest-->New-->Class-->Packege:com.common.util.file-->Name:FileUtilTest-->Finish-->FileUtilTest.java修改成如下:packagecom.common.util.file;importjava.io.File;importjava.io.IOException;importorg.apache.log4j.Logger;importjunit.framework.TestCase;publicclassFileUtilTestextendsTestCase{protectedstaticLoggerlog=Logger.getLogger(FileUtilTest.class);//在执行每个test之前,都执行setUp;publicvoidsetUp(){FiledirFile=newFile("testDir");booleanbFile=dirFile.exists();if(bFile==true){dirFile.delete();}else{dirFile.mkdir();}Filefilename=newFile("testDir\showTest.txt");if(!filename.exists()){try{filename.createNewFile();}catch(IOExceptione){log.error("",e);}}}//在执行每个test之后,都要执行tearDownpublicvoidtearDown(){Filefilename=newFile("d.zip");if(filename.exists()){filename.delete();}filename=newFile("testDir\showTest.zip");if(filename.exists()){filename.delete();}filename=newFile("testDir\showTest.txt");if(filename.exists()){filename.delete();}FiledirFile=newFile("testDir");if(dirFile.exists()){dirFile.delete();}}//压缩一个正常的文件publicvoidtestFileUtilZip(){try{assertTrue(FileUtil.zip("testDir\showTest.txt","testDir\showTest.zip"));}catch(Exceptione){log.error("",e);assertTrue(false);}}//输入一个非正常的路径,路径是空的publicvoidtestFileUtilZip01(){try{assertFalse(FileUtil.zip(null,null));}catch(Exceptione){log.error("",e);assertFalse(true);return;}}//输入一个非正常的路径,路径是空的publicvoidtestFileUtilZip02(){try{assertFalse(FileUtil.zip(null,""));}catch(Exceptione){log.error("",e);assertFalse(true);return;}}//输入一个非正常的路径,路径是空的publicvoidtestFileUtilZip03(){try{assertFalse(FileUtil.zip("",null));}catch(Exceptione){log.error("",e);assertFalse(true);return;}}publicvoidtestFileUtilZip04(){try{assertFalse(FileUtil.zip("d\d","d.zip"));}catch(Exceptione){log.error("",e);assertTrue(true);return;}}}-->保存-->5)右键点击Log4jFileUtiltest-->New-->File-->Filename:Log4jFileUtil.properties-->Finish-->Log4jFileUtil.properties修改成如下:#Setrootloggerleveltoerrorlog4j.rootLogger=DEBUG,Console,File
解决方案:
解决方案:
Consoleappenderdefinition
解决方案:
解决方案:
##AlloutputscurrentlysettobeaConsoleAppender.log4j.appender.Console=org.apache.log4j.ConsoleAppenderlog4j.appender.Console.layout=org.apache.log4j.PatternLayoutlog4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE}%-5p[%c{3}]%m%n
解决方案:
解决方案:
Fileappenderdefinition
解决方案:
解决方案:
#log4j.appender.File=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.File.File=test.loglog4j.appender.File.Append=falselog4j.appender.File.layout=org.apache.log4j.PatternLayoutlog4j.appender.File.layout.ConversionPattern=%d{ABSOLUTE}%-5p[%c]%m%n6)运行FileUtilTesta.选择FileUtilTest.java-->右击-->RunAs-->JavaApplication-->在Console看结果:17:35:39,046DEBUG[util.file.FileUtil]添加压缩文件:17:35:39,046DEBUG[util.file.FileUtil]压缩完成!17:35:39,093DEBUG[util.file.FileUtil]添加压缩文件:17:35:39,093ERROR[util.file.FileUtil]java.io.FileNotFoundException:dd(系统找不到指定的路径。)atjava.io.FileInputStream.open(NativeMethod)atjava.io.FileInputStream.<init>(UnknownSource)atcom.common.util.file.FileUtil.zip(FileUtil.java:74)atcom.common.util.file.FileUtil.zip(FileUtil.java:36)atcom.common.util.file.FileUtilTest.testFileUtilZip04(FileUtilTest.java:98)atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource)atsun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource)atjava.lang.reflect.Method.invoke(UnknownSource)atjunit.framework.TestCase.runTest(TestCase.java:168)atjunit.framework.TestCase.runBare(TestCase.java:134)atjunit.framework.TestResult$1.protect(TestResult.java:110)atjunit.framework.TestResult.runProtected(TestResult.java:128)atjunit.framework.TestResult.run(TestResult.java:113)atjunit.framework.TestCase.run(TestCase.java:124)atjunit.framework.TestSuite.runTest(TestSuite.java:232)atjunit.framework.TestSuite.run(TestSuite.java:227)atorg.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)atorg.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)atorg.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)17:35:39,140ERROR[util.file.FileUtilTest]java.io.FileNotFoundException:dd(系统找不到指定的路径。)atjava.io.FileInputStream.open(NativeMethod)atjava.io.FileInputStream.<init>(UnknownSource)atcom.common.util.file.FileUtil.zip(FileUtil.java:74)atcom.common.util.file.FileUtil.zip(FileUtil.java:36)atcom.common.util.file.FileUtilTest.testFileUtilZip04(FileUtilTest.java:98)atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource)atsun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource)atjava.lang.reflect.Method.invoke(UnknownSource)atjunit.framework.TestCase.runTest(TestCase.java:168)atjunit.framework.TestCase.runBare(TestCase.java:134)atjunit.framework.TestResult$1.protect(TestResult.java:110)atjunit.framework.TestResult.runProtected(TestResult.java:128)atjunit.framework.TestResult.run(TestResult.java:113)atjunit.framework.TestCase.run(TestCase.java:124)atjunit.framework.TestSuite.runTest(TestSuite.java:232)atjunit.framework.TestSuite.run(TestSuite.java:227)atorg.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)atorg.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)atorg.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)atorg.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)这里的测试用例还没有写完,抛给大家实践一下。整体框架已经出来。来源于网络,回归于网络。我的邮箱:happy.every.day@126.comQQ:48399956快乐!2010年10月26日