2013-11-29 2 views
0

같은 이름의 파일을 찾으려면 파일 이름을 다른 확장자와 비교해야합니다. 나는 그 확장에 관심이 없다. SimpleFileVisitor 메서드 visitFile()을 사용하여 파일을 탐색합니다. 난 그냥 루트 디렉토리 이름이나 디렉토리 이름을 가지고 walkFileTree() 파일을 얻기 위해, 그래서 파일의 수를 인식하지 오전 사용하고 있습니다. 프로세스는 배경 process.I 수있는 스레드를 사용하여 걷기 위해 생각하고있다. 파일을 통해 일치 시키지만 구현 방법은 알지 못합니다. 그렇다면 비교 시나리오를 어떻게 추가합니까? 파일 이름을 얻는 방법은 다음과 같습니다. -디렉토리를 반복하면서 파일 이름을 비교하는 중

package trigger; 

import java.io.IOException; 
import java.nio.file.*; 
import java.nio.file.attribute.BasicFileAttributes; 

public class ProcessFilePath extends SimpleFileVisitor<Path>{ 

     String [] str; 
     String name; 

     @Override 
     public FileVisitResult visitFile(Path aFile, BasicFileAttributes aAttrs) throws IOException 

     { 
      str= aFile.toFile().getName().split("\\."); 
      name=str[0]; 
      System.out.println("filename : " + name); 
      return FileVisitResult.CONTINUE; 

     } 



    } 

답변

1

경로에서 파일 이름을 가져 오려면.

File f = new File("C:\\Hello\\AnotherFolder\\The File Name.PDF"); 
System.out.println(f.getName()); 

사용하면 파일 이름을 비교 파일 확장자

str.substring(0, str.lastIndexOf('.')); 

를 제거합니다.

// These two have the same value 
new String("test").equals("test") ==> true 

// ... but they are not the same object 
new String("test") == "test" ==> false 

// ... neither are these 
new String("test") == new String("test") ==> false 

// ... but these are because literals are interned by 
// the compiler and thus refer to the same object 
"test" == "test" ==> true 

// concatenation of string literals happens at compile time resulting in same objects 
"test" == "te" + "st" ==> true 

// but .substring() is invoked at runtime, generating distinct objects 
"test" == "!test".substring(1) ==> false 
+0

이 정보가 도움이 되셨습니까? – KethanKumar

+0

@ user2553442 ... 실제로 나는 루트 디렉토리 이름 또는 디렉토리 이름을 가져 와서 walkFileTree()를 사용하여 걷고 있습니다. 나는 일치해야하는 파일의 정확한 수를 알 수 없다. 그래서 당신의 대답은 약간 혼란스러워 보입니다. – user3007767

+0

다른 디렉토리의 파일을 일치 시키려고합니까? 문자열 배열에 파일 이름을 저장하고 파일 이름을 비교하십시오. – KethanKumar

관련 문제