分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

Apache Common-IO 使用

发布时间:2023-09-06 01:24责任编辑:赖小花关键词:暂无标签

Apache Common-IO 是什么?

  Apache File 工具类,能够方便的操作 File

运行环境

  jdk 1.7

  commons-io 2.6

测试代码

 1 package com.m.basic; 2 ?3 import org.apache.commons.io.FileUtils; 4 import org.junit.Test; 5 ?6 import java.io.File; 7 import java.io.IOException; 8 import java.net.URL; 9 10 public class FileUtilTest {11 12 ????String path = "D:/malin/xxx/file.txt";13 14 ????@Test15 ????public void makeFileTest() throws IOException {16 ????????final File file = new File("D:/malin/sss/");17 ????????FileUtils.forceMkdir(file);18 ????}19 20 ????@Test21 ????public void forceMkdirParentTest() throws IOException {22 ????????File file = new File(path);23 ????????FileUtils.forceMkdirParent(file);24 ????????if (!file.createNewFile()) {25 ????????????String message = "Unable to create File " + path;26 ????????????throw new IOException(message);27 ????????} else {28 ????????????System.out.println("create success");29 ????????}30 ????}31 32 ????@Test33 ????public void writingFileTest() throws IOException {34 ????????File file = new File(path);35 ????????for (int i = 0; i < 3; i++)36 ????????????FileUtils.writeStringToFile(file, "Hello World" + System.getProperty("line.separator"), "UTF-8", true);37 ????}38 39 ????@Test40 ????public void readingFIleTest() throws IOException {41 ????????File file = new File(path);42 ????????String content = FileUtils.readFileToString(file, "utf-8");43 ????????System.out.println(content);44 ????}45 46 ????@Test47 ????public void copyingFileTest() throws IOException {48 ????????File srcFile = new File(path);49 ????????File destFile = new File("D:/malin/sss/file.txt");50 ????????FileUtils.copyFile(srcFile, destFile);51 ????}52 53 ????@Test54 ????public void deletingFileTest() throws IOException {55 ????????FileUtils.deleteDirectory(new File(path).getParentFile());56 ????}57 58 ????@Test59 ????public void convertingTest() throws IOException {60 ????????FileUtils.copyURLToFile(new URL("http://www.baidu.com"), new File(path));61 ????}62 }
View Code

参考

http://commons.apache.org/proper/commons-io/description.html

Apache Common-IO 使用

原文地址:http://www.cnblogs.com/linma/p/7826226.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved