다음은 Java를 사용하여 여러 파일의 이름을 바꾸는 코드입니다 -
예시
import java.io.File;
import java.io.IOException;
public class Demo{
public static void main(String[] argv) throws IOException{
String path_to_folder = "path\\to\\folder\\where\\multiple\\files\\are\\present";
File my_folder = new File(path_to_folder);
File[] array_file = my_folder.listFiles();
for (int i = 0; i < array_file.length; i++){
if (array_file[i].isFile()){
File my_file = new File(path_to_folder + "\\" + array_file[i].getName());
String long_file_name = array_file[i].getName();
String[] my_token = long_file_name.split("\\s");
String new_file = my_token[1];
System.out.println(long_file_name);
System.out.print(new_file);
my_file.renameTo(new File(path_to_folder + "\\" + new_file + ".pdf"));
}
}
}
} 출력
The files in the folder will be renamed to .pdf
Demo라는 클래스에는 여러 파일을 포함하는 폴더에 대한 apth가 정의되는 주요 기능이 포함되어 있습니다. 언급된 경로에 새 폴더가 생성됩니다.
파일 목록은 'listFiles' 함수를 사용하여 가져옵니다. 배열의 파일을 반복하여 파일을 만나면 새로운 파일 경로를 생성하고 파일명을 얻어서 분할한다. 파일 이름이 .pdf로 바뀝니다. 파일 이름은 'long_file_name'의 첫 번째 공백 이후에 시작하는 부분 문자열을 얻음으로써 단축됩니다.