Commons-IO 的 IOUtils 的几个简单例子

//使用JDK的方法
InputStream in = new URL( "http://www.oschina.net" ).openStream();
try {
   InputStreamReader inR = new InputStreamReader( in );
   BufferedReader buf = new BufferedReader( inR );
   String line;
   while ( ( line = buf.readLine() ) != null ) {
     System.out.println( line );
   }
} finally {
   in.close();
}

 

// 使用 IOUtils 的方法
InputStream in = new URL( "http://www.oschina.net" ).openStream();
try {
   System.out.println( IOUtils.toString( in ) );
} finally {
   IOUtils.closeQuietly(in);
}

 

//读取文件所有行
File file = new File("/commons/io/project.properties");
List<String> lines = FileUtils.readLines(file, "UTF-8");

 

文件路径处理

String filename = "C:/commons/io/../lang/project.xml";
String normalized = FilenameUtils.normalize(filename);
// result is "C:/commons/lang/project.xml"

 

 

磁盘剩余空间

long freeSpace = FileSystemUtils.freeSpace("C:/");

posted @ 2013-05-04 06:44  十二十二  阅读(688)  评论(0编辑  收藏  举报