0

(Without including any external libraries.)

What's the most efficient way to remove the extension of a filename's list in a folder in Java? i try to create a remove method but it doesn't work on my code,what is wrong?

here is my code:

public static void main(String args[]) {         
    FileVisitor<Path> simpleFileVisitor = new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult preVisitDirectory(Path dir,BasicFileAttributes attrs)
          throws IOException {
            System.out.println("-------------------------------------");
            System.out.println("DIRECTORY NAME:"+ dir.getFileName() );
                          // + "LOCATION:"+ dir.toFile().getPath());
            System.out.println("-------------------------------------");
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path visitedFile,
              BasicFileAttributes fileAttributes) throws IOException {
            System.out.println("FILE NAME: "+ visitedFile.getFileName());
            return FileVisitResult.CONTINUE;
        }

        public void removeExtension(String FileName) {
            FileName= FileName.substring(0, FileName.lastIndexOf(".pdf"));
            System.out.println(FileName); 
        }
    };
    FileSystem fileSystem = FileSystems.getDefault();
    Path rootPath = fileSystem.getPath("C:\\Users\\admin\\Desktop\\fatto");
    try {
      Files.walkFileTree(rootPath, simpleFileVisitor);
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
}
Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
mark josh
  • 1
  • 1
  • 1
    Since you make no calls to *removeExtension* I can't see how the code is not working as expected... – dbl Aug 30 '18 at 08:21
  • Also in the *removeExtension* (if called at all) you are setting the value of the method parameter not the actual filename value... "visitedFile.setFileName({value})" is never invoked as well... – dbl Aug 30 '18 at 08:23
  • 1
    Hint `String#lastIndexOf` – MadProgrammer Aug 30 '18 at 08:34

0 Answers0