Java / S de archivos problema: archivo ahora escrito por newBufferedWriter

Michael May:

Escribí una pequeña herramienta para imprimir todos los nombres de archivos y directorios bajo un directorio dado en un archivo. El programa compila bien, pero después de ejecutar el programa, el archivo no está escrito. Esto parece extraño para mí. El código de programa se enumeran a continuación.

En la línea 49 en el código, cuando solía archivo sólo como argumento al método, no había ningún problema y el archivo de salida se escriben. Por favor, intente salir y ver el resultado. Pero cuando utilicé file.getFileName () como argumento, el archivo de salida no fue escrito en absoluto!

Muchas gracias por su ayuda.

/**
 * This program walks a directory tree
 * and prints out the directory name and the file names under it.
 * @author Michael Mei
 * @version 1.0 22-03-2020
 */

package walkDirectory;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import static java.nio.file.FileVisitResult.*;
import static java.nio.file.FileVisitOption.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class DirWalkerPrinter extends SimpleFileVisitor<Path> {
    private Path outPath;
    private Writer out;
    private int fileCount;
    private int dirCount;

    DirWalkerPrinter (Path outPath) throws IOException {
        this.outPath = outPath;
        out = Files.newBufferedWriter(outPath, StandardCharsets.UTF_16, StandardOpenOption.WRITE);
    }

    public int getFileCount () {
        return fileCount;
    }

    public int getDirCount () {
        return dirCount;
    } 

    public void writeResults(Path p) throws IOException {
        // Using System.out.println(p.toString()) was also working.
        out.write(p.toString());
        out.write("\n");
    }

    public void done () throws IOException {
        out.write(fileCount + " of files found in " + dirCount);
    }

    @Override
    public FileVisitResult visitFile (Path file, BasicFileAttributes attrs) throws IOException {
        writeResults(file.getFileName()); // line 49
        fileCount++;
        return CONTINUE;
    }

    public FileVisitResult postVisitDirectory (Path dir, BasicFileAttributes attrs) throws IOException {
        writeResults(dir);
        dirCount++;
        return CONTINUE;
    }
    @Override
    public FileVisitResult visitFileFailed (Path file, IOException e) {
        System.err.println(e);
        return CONTINUE;
    }

    public static void main(String[] args) throws IOException {
        if (args.length < 2) {
        System.err.println("java DirWalkerPrinter source-path destination-file");
        System.exit(-1);
    }
        Path startingDir = Paths.get(args[0]);
        Path writeToDir = Paths.get(args[1]);
        DirWalkerPrinter dirWalkerPrinter = new DirWalkerPrinter(writeToDir);
        Files.walkFileTree(startingDir, dirWalkerPrinter);
        dirWalkerPrinter.done();
        int fileCount = dirWalkerPrinter.getFileCount();
        int dirCount = dirWalkerPrinter.getDirCount();
        System.out.println(fileCount + " of files found in " + dirCount);
    }
}
RR_IL:
file.getFileName()

Esta función sólo devolverá el nombre de archivo - sin la ruta completa. Su código tratará de crear el archivo en el mismo directorio que se está ejecutando desde. (Debido a que introduzca el nombre de archivo)

Este es probablemente el problema.

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=345192&siteId=1
Recomendado
Clasificación