저는 Java를 처음 사용하며 디렉토리와 해당 파일을 만드는 함수를 구현하려고 시도하고 있습니다. 파일 중 하나는 NodeA라는 폴더 안에있는 "mapping.txt"입니다. mapping.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());
next_file에 한 번에 생성 된 모든 파일이 있습니다. 파일을 편집 할 수 있도록 파일을 만든 후 각 파일을 검사 할 수있는 방법을 알아야합니다.
감사합니다. 당신이 그것을 호출 다음에, 반복자는 다음 값으로 이동하기 때문에
String fileName= iter.next();
, 당신은 NoSuchElementException
를 얻을 수 있는지 잘못된 결과에 대한 수 있습니다
결과가 예상 한 것과 어떻게 다릅니 까? –
위의 방법을 시도했지만 아직 쓰지 않습니다. 파일이 비어 있습니다. 나는 프로젝트를 바꾸려고 노력했지만 여전히 같은 결과를 보였다. –
팁 : @AdrianJandl (또는 누구든지 - @는 중요합니다)을 추가하여 * 새 코멘트를 * 알리도록하십시오. –