Java程式更改多個檔案的名字

來源:互聯網
上載者:User

因為下的視頻教程名字好長好長,總想改一下視頻的名字,讓它變的簡潔一點。而手改要一定的時間,而且不爽。為了看著舒服,所以用java寫了個程式讓它幫我改,自己測試了一下可以用。

改完名後:

程式碼:

package com.xk.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class ChangeFilesName
{
    public static void main(String[] args) throws Exception
    {
        File file = new File("c:/cfn");
        File[] subFiles = file.listFiles();
        InputStream is = null;
        byte[] buffer = null;
        
        for(int i=0;i<subFiles.length;i++)
        {
            try
            {
                 is = new FileInputStream(subFiles[i]);
                int length = is.available();
                buffer = new byte[length];
                
                is.read(buffer);
                
                
            }
            catch (FileNotFoundException e1)
            {
                e1.printStackTrace();
            }
            
            //改名為fn
            String fn = "Struts2教程"+(i+1)+".png";
            
            System.out.println(fn);
            File file2 = new File("c:/cfn/"+fn);
            OutputStream os = null;
            try
            {
                os = new FileOutputStream(file2);
                os.write(buffer);
                is.close();
                os.close();
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
            
            //將之前的檔案刪除
            subFiles[i].delete();
        }
        
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.