FileUtils类
主要提供方便操作文件/目录的方法
主要方法
writing toa file
readingfrom a file
make a directory including parent directories
copying files and directories
deleting files and directories
converting to and from a URL
listing files and directories by filter and extension
comparing file content
file last changed date
calculating a checksum
1.//拷贝文件 --这里会覆盖--而非追加
File src = new File("D:\\workspace\\j2ee\\mycommon\\src\\com\\commons\\io\\abc.txt");
File dest = new File("D:\\workspace\\j2ee\\mycommon\\src\\com\\commons\\io\\abcd.txt");
FileUtils.copyFile(src, dest);
2.//拷贝文件到某一路径
File src = new File("D:\\workspace\\j2ee\\mycommon\\src\\com\\commons\\io\\abcd.txt");
File dir = new File("D:\\");
FileUtils.copyFileToDirectory(src, dir);
3.//写字符串到一个文件--此种为覆盖的方法
String string = "Blah blah blah";
File dest = new File("D:\\workspace\\j2ee\\mycommon\\src\\com\\commons\\io\\abcd.txt");
FileUtils.writeStringToFile(dest, string, "ISO-8859-1");
4. 删除文件实例
File file = new File( ("D:\\workspace\\j2ee\\mycommon\\src\\com\\commons\\io\\abcd.txt") );
FileUtils.forceDelete(file);
5. 读取文件每一行数据的功能
File file = new File("D:\\workspace\\j2ee\\mycommon\\src\\com\\commons\\io\\abc.txt");
List lines = FileUtils.readLines(file, "UTF-8");
for(int i=0;i<lines.size();i++) {
System.out.println(lines.get(i));
}```
##IOUtils类
IOUtils主要提供更便捷的操作流的方法
主要方法
toXxx/read- these methods read data from a stream
write -these methods write data to a stream
copy -these methods copy all the data from one stream to another
contentEquals- these methods compare the content of two streams
```java
1.比较两个输入流的内容是否相等
IOUtils.contentEquals(InputStream input1, InputStream input2)
(URLurl = new URL(url);
InputStream is = url.openStream();ur的内容读出来)
InputStream in = new URL("http://www.apache.org").openStream();
InputStream in2 = new URL("http://www.apache.org").openStream();
IOUtils.contentEquals(in, in2)
IOUtils.toString将缓冲区的内容以utf-8的编码方式以字符串的形式输出
2.将字节从 InputStream复制到OutputStream中
IOUtils.copy(InputStream input, OutputStream output)
ByteArrayOutputStream out = **new** ByteArrayOutputStream();
IOUtils.copy(in2, out);
3.返回一个装有输入字节行数的Iterator对象,使用特定的字符编码(如果没用声明的话则用默认编码)
IOUtils.lineIterator(InputStream input, Charset encoding)
InputStream in3 = **new** URL("http://www.apache.org").openStream();
try {
LineIterator it = IOUtils.lineIterator(in3, "UTF-8");
System.out.println(it.hasNext());
if (it.hasNext()) {
String line = it.nextLine();
//System.out.println(line);
}
} finally {
IOUtils.closeQuietly(in);
}
4、从输入流中读取字节(通常返回输入流的字节数组的长度)
IOUtils.read(InputStream input, byte[] buffer)
InputStream in4 = new URL("http://www.apache.org").openStream();
byte[] buffer = new byte[100000];
System.out.println(IOUtils.read(in4, buffer));
5、获得输入流的内容放回一个List<String>类型的容器,每一行为这个容器的一个入口,使用特定的字符集(如果为空就使用默认的字符集)
IOUtils.readLines(InputStream input, Charset encoding)
InputStream in5 = new URL("http://www.apache.org").openStream();
List<String> list = IOUtils.readLines(in5, "UTF-8");
Iterator<String> iter = list.iterator();
while(iter.hasNext()){
String s = iter.next();
//System.out.println(s);
}```