Maven Dependency
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.fool.commons-io</groupId> <artifactId>commons-io</artifactId> <version>1</version> <dependencies> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
delete directory recursively
package org.fool.commons.io; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; /** * delete directory recursively */ public class DeleteDirectoryRecursive { public static void main(String[] args) { File directory = new File("C:/temp1"); try { // Deletes a directory recursively. FileUtils.deleteDirectory(directory); } catch (IOException e) { e.printStackTrace(); } } }
copy directory with all its contents to another directory
package org.fool.commons.io; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; /** * copy directory with all its contents to another directory */ public class DirectoryCopy { public static void main(String[] args) { // An existing directory to copy. File srcDir = new File("C:/temp"); // The destination directory to copy to. This directory // doesn't exists and will be created during the copy // directory process. File destDir = new File("C:/temp1"); try { // Copy source directory into destination directory // including its child directories and files. When // the destination directory is not exists it will // be created. This copy process also preserve the // date information of the file. FileUtils.copyDirectory(srcDir, destDir); } catch (IOException e) { e.printStackTrace(); } } }
move directory to another directory with its entire contents
package org.fool.commons.io; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; /** * move directory to another directory with its entire contents */ public class DirectoryMove { public static void main(String[] args) { File srcDir = new File("C:/temp1"); File destDir = new File("C:/temp2"); try { // Move the source directory to the destination directory. // The destination directory must not exists prior to the // move process. FileUtils.moveDirectory(srcDir, destDir); } catch (IOException e) { e.printStackTrace(); } } }
calculate directory size
package org.fool.commons.io; import java.io.File; import org.apache.commons.io.FileUtils; /** * calculate directory size */ public class DirectorySizeExample { public static void main(String[] args) { // Using FileUtils.sizeOfDirectory() we can calculate // the size of a directory including it sub directory long size = FileUtils.sizeOfDirectory(new File("C:/temp")); System.out.println("Size: " + size + " bytes"); } }
get free space of a drive or volume
package org.fool.commons.io; import java.io.IOException; import org.apache.commons.io.FileSystemUtils; import org.apache.commons.io.FileUtils; /** * get free space of a drive or volume */ public class DiskFreeSpace { public static void main(String[] args) { try { String path = "C:"; long freeSpaceKB = FileSystemUtils.freeSpaceKb(path); long freeSpaceMB = freeSpaceKB / FileUtils.ONE_KB; long freeSpaceGB = freeSpaceKB / FileUtils.ONE_MB; System.out.println("Size of " + path + " = " + freeSpaceKB + " KB"); System.out.println("Size of " + path + " = " + freeSpaceMB + " MB"); System.out.println("Size of " + path + " = " + freeSpaceGB + " GB"); } catch (IOException e) { e.printStackTrace(); } } }
create a copy of a file
package org.fool.commons.io; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; /** * create a copy of a file */ public class FileCopyExample { public static void main(String[] args) { File source = new File("pom.xml"); File target = new File("pom-backup.xml"); File targetDir = new File("C:/temp"); try { System.out.println("Copying " + source + " file to " + target); FileUtils.copyFile(source, target); System.out.println("Copying " + source + " file to " + targetDir); FileUtils.copyFileToDirectory(source, targetDir); } catch (IOException e) { e.printStackTrace(); } } }
sort files base on their last modified date
package org.fool.commons.io; import java.io.File; import java.util.Arrays; import org.apache.commons.io.comparator.LastModifiedFileComparator; /** * sort files base on their last modified date */ public class FileSortByLastModified { public static void main(String[] args) { File dir = new File("C:/temp"); File[] files = dir.listFiles(); // Sort files in ascending order base on last modification date. Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR); for (File file : files) { System.out.printf("File %s - %2$tm %2$te,%2$tY%n", file.getName(), file.lastModified()); } System.out.println(); // Sort files in descending order base on last modification date. Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE); for (File file : files) { System.out.printf("File %s - %2$tm %2$te,%2$tY%n", file.getName(), file.lastModified()); } } }
read a file into byte array
package org.fool.commons.io; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import org.apache.commons.io.IOUtils; /** * read a file into byte array */ public class FileToByteArray { public static void main(String[] args) { File file = new File("sample.txt"); try { InputStream is = new FileInputStream(file); byte[] bytes = IOUtils.toByteArray(is); System.out.println("Byte array size: " + bytes.length); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
get the content of an InputStream as a String
package org.fool.commons.io; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import org.apache.commons.io.IOUtils; /** * get the content of an InputStream as a String */ public class InputStreamToString { public static void main(String[] args) { InputStream is = null; try { is = new FileInputStream(new File("sample.txt")); String contents = IOUtils.toString(is, "UTF-8"); System.out.println(contents); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(is); } } }
create a human-readable file size
package org.fool.commons.io; import java.io.File; import org.apache.commons.io.FileUtils; /** * create a human-readable file size */ public class ReadableFileSize { public static void main(String[] args) { File file = new File( "C:/TDDOWNLOAD/JavaTools/JDK/jdk-6u38-windows-x64.exe"); long size = file.length(); String display = FileUtils.byteCountToDisplaySize(size); System.out.println("Name = " + file.getName()); System.out.println("size = " + size); System.out.println("Display = " + display); } }
read content line by line
package org.fool.commons.io; import java.io.File; import java.io.IOException; import java.util.List; import org.apache.commons.io.FileUtils; /** * read content line by line */ public class ReadFileToListSample { public static void main(String[] args) { File file = new File("sample.txt"); List<String> contents = null; try { contents = FileUtils.readLines(file); } catch (IOException e) { e.printStackTrace(); } for (String line : contents) { System.out.println(line); } } }
write string data to file
package org.fool.commons.io; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; /** * write string data to file */ public class WriteStringToFileExample { public static void main(String[] args) { File file = new File("sample.txt"); String data = "Apache Commons IO"; try { FileUtils.writeStringToFile(file, data); } catch (IOException e) { e.printStackTrace(); } } }
read file contents to string
package org.fool.commons.io; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; /** * read file contents to string */ public class ReadFileToStringExample { public static void main(String[] args) { File file = new File("sample.txt"); String data = null; try { data = FileUtils.readFileToString(file); } catch (IOException e) { e.printStackTrace(); } System.out.println(data); } }
search for files recursively
package org.fool.commons.io; import java.io.File; import java.util.Collection; import org.apache.commons.io.FileUtils; /** * search for files recursively */ public class SearchFileRecursive { public static void main(String[] args) { File root = new File("C:/temp"); String[] extensions = { "xml", "java", "dat" }; boolean recursive = true; Collection<File> files = FileUtils.listFiles(root, extensions, recursive); for (File file : files) { System.out.println("File = " + file.getAbsolutePath()); } } }
touch a file
package org.fool.commons.io; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; /** * touch a file */ public class TouchFileExample { public static void main(String[] args) { File file = new File("touch.dat"); try { // Touch the file, when the file is not exist a new file will be // created. If the file exist change the file timestamp. FileUtils.touch(file); } catch (IOException e) { e.printStackTrace(); } } }
相关推荐
- `jccook-code.zip`和`javathreads3.examples.zip`: 可能包含了Jakarta Commons IO库和Java线程编程的示例代码。 - `effective2.zip`: 可能是指《Effective Java》一书的第二版中的代码,这本书讲述了编写高质量...
poi-examples-3.10-FINAL-20140208.jar poi-excelant-3.10-FINAL-20140208.jar poi-ooxml-3.10-FINAL-20140208.jar poi-ooxml-schemas-3.10-FINAL-20140208.jar poi-scratchpad-3.10-FINAL-20140208.jar stax-api-...
LeanKit-java-examples Leankit.samples.boards示例类,用于列出属于一个 Leankit 帐户的所有板。 Leankit.samples.board用于从特定板检索所有卡片和相应信息的示例类... commons-io-2.4.jar json-simple-1.1.1.jar
poi-examples-3.15.jar poi-excelant-3.15.jar poi-ooxml-3.15.jar poi-ooxml-schemas-3.15.jar poi-scratchpad-3.15.jar spring-aop-4.0.0.RELEASE.jar 使用Spring 的AOP 特性时所需的类和源码级元数据支持 spring-...
poi-examples-3.12.jar poi-excelant-3.12.jar poi-ooxml-3.12.jar poi-ooxml-schemas-3.12.jar poi-scratchpad-3.12.jar ridl-3.0.0.jar simple-spring-memcached-3.5.0.jar slf4j-api-1.6.0.jar slf4j-log...
Commons-FileUpload(以及Commons-IO) 注:加*的包可使用MyEclipse自带的类库。 Web框架(跨浏览器) FckEditor 可视化编辑HTML XLoadTree 动态加载XML生成JavaScript树组件 jQuery AJAX框架-查询DOM对象,简洁,...
10. **Java标准库和第三方库**:了解并熟悉Java标准库中的类和方法,同时适时引入Apache Commons、Spring Framework、Hibernate等第三方库,能提升开发效率和代码质量。 通过解压并探索"tut-examples.zip",学习者...
5. `poi-examples-3.7-20101029.jar`:包含示例代码,帮助开发者理解和使用Apache POI库。 6. `xmlbeans-2.6.0.jar`:XMLBeans是另一个Apache项目,它提供了一个API来操作XML文档,与Apache POI结合使用,用于解析和...
- **poi-examples-3.11-20141221.jar**: 包含了Apache POI的示例代码,可以帮助开发者理解如何使用POI库进行实际操作。 - **poi-scratchpad-3.11-20141221.jar**: 这个库包含了POI项目的一些实验性或未完成的功能...
- **poi-examples-3.9-20121203.jar**: 包含了一些示例代码,可以帮助开发者更好地理解和使用Apache POI。 - **commons-io-2.4.jar**: Apache Commons IO库,提供了一些基本的文件和输入/输出操作的辅助工具。 - ...
4. **poi-examples-4.1.0.jar**: 这是Apache POI的示例库,版本为4.1.0,比上述的poi-3.7.jar更现代。这个库包含了使用POI进行Excel操作的各种示例代码,可以帮助开发者快速理解和学习如何使用POI进行实际开发。...
org.apache.commons.net.io org.apache.commons.net.nntp org.apache.commons.net.ntp org.apache.commons.net.pop3 org.apache.commons.net.smtp org.apache.commons.net.telnet org.apache.commons.net.tftp...
【标题】"graduation-thesis-examples"是一个与毕业论文相关的项目,其核心是提供一系列在实际毕业论文中可能用到的示例。这表明该项目可能是为了帮助学生或研究人员更好地理解和应用理论知识到实际的毕业论文写作中...
这个"java-examples-master"目录可能包含了这些知识点的具体实现,通过阅读和运行其中的代码,开发者可以深入理解Java的各个方面,并提升实际编程能力。对于初学者来说,这是一个极好的学习资源,而对于有经验的...
This collection provides expert tips for using the utilities of the Java-based Jakarta Commons open source project. You don't have to be an expert, the book's solution-based format contains code ...
1. jpivot加入其它应用中 解压jpivot.war 文件,在你的应用当中你需要下面的这些文件: /wcf/** /jpivot/** /WEB-INF/wcf/** /WEB-INF/jpivot/** /WEB-INF/lib/** 最后你还需要对比 jpivot.war 里的 web.xml ...
"JGrega_Java_Examples-master"这个压缩包可能包含了以上各个方面的具体代码实例,通过学习和实践这些例子,开发者能够深化对Java语言的理解,提升编程技能。每个例子都是一次动手学习的机会,通过阅读源码、编译...
- **Apache Commons, Guava**:提供各种实用工具类和功能丰富的库。 10. **单元测试** - **JUnit**:Java的单元测试框架,用于编写和运行测试用例。 以上只是部分可能包含在"examples:不同的Java示例"中的内容,...
- `$HADOOP_HOME/share/hadoop/common/lib/commons-cli-1.2.jar` 此外,我们还可以通过运行`hadoop classpath`命令来获取运行Hadoop程序所需的完整classpath信息,这对于理解整个系统所依赖的所有JAR文件非常有帮助...
通过`myid_examples-master` 这个文件名,我们可以猜测这是一个Git仓库的克隆或下载,包含源代码和可能的测试用例。在这个仓库中,开发者可能为每个ID处理场景创建了一个单独的Java类,每个类都展示了特定的用法或...