annotjiang
2013-12-10 08:48
采纳率: 100%
浏览 3.3k

如何把图片移到文件夹中并自动创建文件夹?

一张图片用一个文件夹装,比如说有100张图片,就自动创建100个文件夹,每个文件夹装一张图片,这功能要怎么实现?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • Jerome_Song 2013-12-10 13:01
    已采纳

    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();
    
        }
    
      }
    
    点赞 评论

相关推荐 更多相似问题