Good day to all. I am trying to write a program that should bypass all the subdirectories in the folder and change the contents in the subdirectory. Essence: there is a folder, in it there are several hundreds / thousands of other folders, and in each of them there are already several files. It is necessary to bypass all of them, go into each, and depending on the extension, rename according to a template: go to the 1st folder - see blbl.json - rename to 1.json, the same with text, etc.
Code:
import java.io.File; public class Sorter{ //main procedure public static void main(String[] arg) { String Path = Utils.pathFileData; printTreeFiles(Path); } public static void printTreeFiles(String Path) { File Directory = new File(Path);//ccΡΠ»Ρ Π½Π° Π³Π»Π°Π²Π½ΡΡ ΠΏΠ°ΠΏΠΊΡ, Π²Π½ΡΡΡΠΈ ΠΊΠΎΡΠΎΡΠΎΠΉ 1000 ΠΏΠ°ΠΏΠΎΠΊ File[] SubDirectory = Directory.listFiles(); getContent(SubDirectory); System.out.println(); } //recursive procedure for finding the contents of a directory public static void getContent(File[] SubDirectory) {//ΡΡΡΡΡΠΌ ΠΏΠ°ΠΏΠΊΠΈ for (int i=0; i< SubDirectory.length; i++) {// // System.out.print("\t"); // System.out.println(Directory.getName()); //File[] SubDirectory = Directory.listFiles(); for (File SubWay:SubDirectory) {//Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠ°ΠΉΠ»Π° ΠΈΠ· ΠΎΠ΄Π½ΠΎΠΉ ΠΈΠ· 1000 ΠΏΠ°ΠΏΠΎΠΊ // getContent(SubWay, i + 1); String buf = SubWay.getName(); // ΡΠΈΡΠ°Π΅ΠΌ ΡΠ΅ΠΊΡΡΠ΅Π΅ ΠΈΠΌΡ ΡΠ°ΠΉΠ»Π° // System.out.println(buf); if (buf.contains("ARTICLE"))// { buf = buf.replace(".txt", "Article" + i + ".txt"); SubWay.renameTo(new File(Utils.pathFileData + buf)); } else if (buf.endsWith(".json")) { buf = buf.replaceFirst(".json", i + ".json"); SubWay.renameTo(new File(Utils.pathFileData + buf)); } else buf = buf.replaceFirst(".txt", i + ".txt"); SubWay.renameTo(new File(Utils.pathFileData + buf)); } } } }
The code instead does the following: appends the name of the file with the extension .txt to the folder in which the file is located. Please help me figure out what needs to be changed in order to smell it not to askew.
Similar threads have already been read here, as the disc took the code from here: Recursive counting of files in the directory
Utils.pathFileData + buf
->Utils.pathFileData + "/" + buf
? - user194374