一張圖片用一個文件夾裝,比如說有100張圖片,就自動創建100個文件夾,每個文件夾裝一張圖片,這功能要怎麼實現?
java代碼可以嗎,就這麼個邏輯 shell或python應該更簡單
public static void main(String[] args) {
File file = new java.io.File("/home/jerome/Pictures");
file.getAbsoluteFile();
String[] files = file.list();
for (String string : files) {
System.out.println(string);
File dir = new File(file.getAbsolutePath() + "/"
+ string.substring(0, string.lastIndexOf(".")));
dir.mkdir();
copyFile(file+"/"+string, dir.getAbsolutePath()+"/"+string);
delFile(file+"/"+string);
}
}
public static void delFile(String filePathAndName) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
java.io.File myDelFile = new java.io.File(filePath);
myDelFile.delete();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) {
InputStream inStream = new FileInputStream(oldPath);
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}