实用java小代码,获取qq存放位置下的图片,其它的也可以
现在qq存放图片的位置多了几层看不懂的文件夹,找图片没有以前方便了,以下代码直接用即可,转移后的文件夹目录自己提前建一下,省懒了。
static String regex = ".+\\.[p][n][g]";//正则表达式 用来判读是否是图片格式的文件
static String regex2 = ".+\\.[j][p][g]";
static String regex3 = ".+\\.[b][m][p]";
static int x=0;
public static void main(String[] args) throws IOException {
File file = new File("E:\\qqfile\\你的qq号\\Image\\Group2");//需要查找的文件夹 可以改成自己需要的,确认下自己电脑上qq文件存放位置
copyPhoto(file);
System.out.println("传输完成!");
}
private static void copyPhoto(File file) throws IOException { //复制图片的方法
File[] files = file.listFiles(); //将路径封装成File数组
for (File file2 : files) {//遍历这个数组
if (file2.isDirectory()) { //判断是否是文件夹
copyPhoto(file2); //是的话就继续调用这个方法
} else if (file2.getName().matches(regex)||file2.getName().matches(regex2)||file2.getName().matches(regex3)) { //不是的话就匹配是否是图片
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file2)) ;//创建输入的管道
byte[] buf = new byte[1024*20];//创建一个小数组
int lenght = 0;
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream( //创建输出管道
"D:\\qqphotos\\" + file2.getName())); //图片会拷贝到这里 这边文件夹自己建一下
File f=new File(file2.getPath());
x++;
while ((lenght=bis.read(buf)) != -1) {
bos.write(buf, 0, lenght);
}
bos.close();
bis.close();file2.delete();
}
}
}

浙公网安备 33010602011771号