ArrayUtils
int n = ArrayUtils.indexOf(displayItem, "abc");
傳回String[] displayItem中含有字串"abc"的位置
if ( ArrayUtils.contains( displayItem, "abc" ) ) {
// Do some stuff.
}
String[] displayItem中含有字串"abc"則傳回真值
FileUtils
FileUtils.cleanDirectory("/abc");
清除整個資料夾及子資料夾, 但不含資料夾abc本身
FileUtils.deleteDirectory("/abc");
清除整個資料夾及子資料夾, 含資料夾abc本身
File f = new File("/sd/abc.txt");
String str = FileUtils.readFileToString(f, code);
將文字檔案f整個讀入一個字串內, code是該檔案的編碼格式(utf-8,big5...)
FileUtils.writeStringToFile(f, fileContent, "big5");
將字串fileContent寫出到f
File file = new File("c:/test.txt");
List lines = FileUtils.readLines(file, "UTF-8");
將test.txt一行一行讀入List內
File src = new File("src.txt");
File dest = new File("dest.txt");
FileUtils.copyFile(src, dest);
複製檔案並重命名
File srcfile = new File("c:\\Test.java");
File destDir = new File("D:\\");
FileUtils.copyFileToDirectory(srcfile, destDir);
複製檔案到目錄下
InputStream in = new
URL("http://www.baidu.com/img/baidu_logo.gif").openStream();
byte [] gif = IOUtils.toByteArray(in);
//IOUtils.write(gif,new FileOutputStream(new File("c:/test.gif")));
FileUtils.writeByteArrayToFile(new File("c:/test.gif"),gif);
IOUtils.closeQuietly(in);
下载文件到本地
URL url = new URL("http://www.163.com");
File file = new File("c:\\163.html");
FileUtils.copyURLToFile(url, file);
網頁保存成本地文件
FilenameUtils
String ext = FilenameUtils.getExtension(file.toString());
取得副檔名
String baseName = FilenameUtils.getBaseName(f.toString());
取得主檔名(不含副檔名及小數點)
IOUtils
將inputStream轉成字串
InputStream in = new URL( "http://laoyu.info" ).openStream();
try {
System.out.println( IOUtils.toString( in ) );
} finally {
IOUtils.closeQuietly(in);
}