Я новичок в Java и пытаюсь реализовать функцию, которая создает каталоги и соответствующие файлы. Одним из файлов является «mapping.txt», который находится внутри папки с именем NodeA. Содержимое map.txt является абсолютным путем NodeA.Запись пути к файлу (путь к папке, содержащей файл)
Код, приведенный ниже, я пробовал. Я прокомментировал все возможные места для лучшего понимания. Я хочу знать причину, по которой я не могу писать в файле. Любая помощь будет глубоко оценена.
//-------------------------- To create Node and their respective Files ------------------------------------------------------
for(int j=0;j<alpha.length;j++){ //alpha is a String array which contains {"A","B","C","D"}
node_directory=Node.concat(alpha[j]); // nodde_directory is "C:\Users\Desktop\Node. I concat A,B,C and D to create different directories.
File dir = new File(node_directory);
if(!dir.exists()){
dir.mkdirs();
}
System.out.println("Path: \n" +dir.getAbsolutePath());
System.out.println();
List<String> fileList = Arrays.asList(files); // files is an array of string containing "mapping" and "data".
Iterator <String> iter = fileList.iterator(); //I traverse through the list.
while(iter.hasNext()){
File next_file = new File(node_directory+"\\"+iter.next()+".txt"); //while traversing, I append the current "iter.next" to "node_directory" and append ".txt" to create files.
System.out.println("The Files are: \n" +next_file);
System.out.println();
// I created the Directories, mapping and data Files for each directory.
/*I am stuck here, as it is not writing the path in the mapping File */
if(iter.next()=="mapping"){ // I check if iter.next is mapping, so that i can write in the mapping file, the path of Folder containing mapping file.
BufferedWriter br = new BufferedWriter(new FileWriter(next_file));
br.write(next_file.getAbsolutePath());
}
if(!next_file.exists()){
System.out.println(next_file.createNewFile());
}
Я понял, что происходит. Поскольку я просматриваю массив строк по списку. Проблема здесь:
next_file = new File(node_directory+"\\"+iter.next()+".txt"); // This line creates files by appending the required data. Since iter.next() returns all the Files in one go,
BufferedWriter br = new BufferedWriter(new FileWriter(next_file));
br.write(dir.getAbsolutePath());
В следующем файле есть все файлы, созданные за один раз. Мне нужно знать, как я могу проверить каждый файл после его создания, чтобы я мог редактировать файл.
спасибо.
Как результат отличается от ожидаемого? –
Я попытался вывести этот метод и он все равно не пишет. Файл пуст. Я попытался изменить проект и попробовать и все тот же результат. –
Совет: добавьте @AdrianJandl (или кто бы то ни было - '@' важен), чтобы они * уведомили * о новом комментарии. –