jdk7 NIO的读取目录下所有图片(文件)
2013-11-19 00:00
113 查看
public class FileTest {
@Test
public void test() throws IOException{
long start = System.currentTimeMillis();
String path="G:\\图片";
Path p = Paths.get(path);
//放文件对应的Path对象,可以通过toFile方法得到File对象
//这里没有语法错误,是jdk7的新特性
List<Path> result = new ArrayList<>();
//筛选.jpg,.png,.gif格式图片
try (DirectoryStream<Path> stream = Files.newDirectoryStream(p, "*.{jpg,png,gif}")) {
for (Path entry: stream) {
result.add(entry);
}
//同样是jdk7的新特性,避免庞大的catch块
} catch (DirectoryIteratorException | IOException e) {
e.printStackTrace();
}
//制定hashmap的容量,防止rehashing的巨大开销
Map<String,byte[]> map = Maps.newHashMapWithExpectedSize(200);
BufferedInputStream b = null;
ByteArrayOutputStream out = null;
BufferedOutputStream o = null;
String fileName = null;
for(Path currentPath:result
){
if(Files.isReadable(currentPath)){
try{
b = new BufferedInputStream(Files.newInputStream(currentPath, StandardOpenOption.READ));
out = new ByteArrayOutputStream();
o = new BufferedOutputStream(out);
fileName = currentPath.getFileName().toString();
System.out.println("============文件名=================="+fileName);
byte[] transition = new byte[1024];
int i = 0;
while((i=b.read(transition))!=-1){
o.write(transition);
}
map.put(fileName, out.toByteArray());
}finally{
if(b!=null){
b.close();
}
if(o!=null){
o.close();
}
}
}else{
throw new FileNotFoundException("读取图片出错");
}
}
long end = System.currentTimeMillis();
//265个图片858毫秒,由于上边我的hashmap定义的容量是200,所以中间经过了rehashing,因此时间其实可以更短
System.out.println("时间差================"+(end-start));
}
u can do anything u set your mind to man!————Eminem ,《8 miles》
@Test
public void test() throws IOException{
long start = System.currentTimeMillis();
String path="G:\\图片";
Path p = Paths.get(path);
//放文件对应的Path对象,可以通过toFile方法得到File对象
//这里没有语法错误,是jdk7的新特性
List<Path> result = new ArrayList<>();
//筛选.jpg,.png,.gif格式图片
try (DirectoryStream<Path> stream = Files.newDirectoryStream(p, "*.{jpg,png,gif}")) {
for (Path entry: stream) {
result.add(entry);
}
//同样是jdk7的新特性,避免庞大的catch块
} catch (DirectoryIteratorException | IOException e) {
e.printStackTrace();
}
//制定hashmap的容量,防止rehashing的巨大开销
Map<String,byte[]> map = Maps.newHashMapWithExpectedSize(200);
BufferedInputStream b = null;
ByteArrayOutputStream out = null;
BufferedOutputStream o = null;
String fileName = null;
for(Path currentPath:result
){
if(Files.isReadable(currentPath)){
try{
b = new BufferedInputStream(Files.newInputStream(currentPath, StandardOpenOption.READ));
out = new ByteArrayOutputStream();
o = new BufferedOutputStream(out);
fileName = currentPath.getFileName().toString();
System.out.println("============文件名=================="+fileName);
byte[] transition = new byte[1024];
int i = 0;
while((i=b.read(transition))!=-1){
o.write(transition);
}
map.put(fileName, out.toByteArray());
}finally{
if(b!=null){
b.close();
}
if(o!=null){
o.close();
}
}
}else{
throw new FileNotFoundException("读取图片出错");
}
}
long end = System.currentTimeMillis();
//265个图片858毫秒,由于上边我的hashmap定义的容量是200,所以中间经过了rehashing,因此时间其实可以更短
System.out.println("时间差================"+(end-start));
}
u can do anything u set your mind to man!————Eminem ,《8 miles》
相关文章推荐
- 读取指定目录下的所有文件生成一个excel
- matlab读取一个目录中所有文件的方法
- java 文件 读取目录下的所有文件(包括子目录)
- 读取一个文件下的所有图片
- java递归读取目录下所有文件的方法
- 读取文件中的所有图片的路径,保存到txt文件中。
- python 读取一个目录下的所有目录和文件
- 一段可以在UNIX下,读取当前目录下所有文件(包括子目录中的文件)的文件名的程序
- 如何读取一个文件夹下的所有图像文件,并制作负样本到指定目录
- Python实现读取目录所有文件的文件名并保存到txt文件代码
- C#.NET读取一个文件目录下所有excel文件
- 如何读取某个指定文件夹下所有文件,并筛选出其中的图片文件(jpeg,bmp)展示出来。
- Python 读取某个目录下所有的文件实例
- C#读取目录下所有指定类型文件的方法
- php 怎样读取指定目录下面的所有文件?
- 读取文件中的所有图片的路径,保存到txt文件中。
- 扫描指定目录下所有图片文件
- 循环读取本地目录下的所有本件及子目录中的文件
- 对目录内所有文件(包括子文件夹文件)读取处理
- php 读取目录所有文件信息dir()