2013-06-13 2 views
2

저는 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를 얻을 수 있는지 잘못된 결과에 대한 수 있습니다

+3

결과가 예상 한 것과 어떻게 다릅니 까? –

+0

위의 방법을 시도했지만 아직 쓰지 않습니다. 파일이 비어 있습니다. 나는 프로젝트를 바꾸려고 노력했지만 여전히 같은 결과를 보였다. –

+0

팁 : @AdrianJandl (또는 누구든지 - @는 중요합니다)을 추가하여 * 새 코멘트를 * 알리도록하십시오. –

답변

0
if(iter.next().equals("mapping") 
0

첫째, 지역 변수에 iter.next()를 할당 . 그런 다음 나머지 코드에서이 fileName 변수를 사용하십시오.

또 다른 실수는 문자열을 ==과 비교하는 것입니다. 문자열은 불변의 객체이고 ==을 사용하면 객체가 아니라 객체 참조를 비교하는 것입니다. 따라서 두 문자열이 완전히 일치하면 ==과 비교할 때 false가 생성 될 수 있습니다. .equalsTo() 대신 :

if (fileName.equals("mapping")) 
+0

나는 equals()와 비교하려고 시도했지만 작동하지 않습니다. 데이터를 입력하는 것을 제외하고 파일과 디렉토리를 만드는 것으로 모든 것이 멋지게 보입니다. –

+0

두 개의 문자열이 실제로 동일하다는 것을 디버그하십시오. 그 경우에는 equals가 작동해야합니다. IDE (예 : Eclipse) – darijan

+0

예. Netbeans IDE를 사용하고 있습니다. –

관련 문제