Advertisement
Guest User

Untitled

a guest
Feb 13th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. package ru.ifmo.rain.kokorin.walk;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.IOException;
  6. import java.nio.file.*;
  7.  
  8. public class RecursiveWalk {
  9.     private static void processFiles(BufferedReader reader, BufferedWriter writer) {
  10.         FileVisitor visitor = new FileVisitor(writer);
  11.         try {
  12.             String line;
  13.             while ((line = reader.readLine()) != null) {
  14.                 try {
  15.                     Files.walkFileTree(Paths.get(line), visitor);
  16.                 } catch (InvalidPathException | IOException e) {
  17.                     writer.write("00000000");
  18.                     writer.newLine();
  19.                 }
  20.             }
  21.         } catch (IOException e) {
  22.             System.out.println("Cannot process file " + e.getMessage());
  23.         }
  24.     }
  25.  
  26.     public static void main(String[] args) {
  27.         if (args == null || args.length != 2 || args[0] == null || args[1] == null) {
  28.             System.out.println("ERROR - 2 non-null arguments required");
  29.             return;
  30.         }
  31.         String inputFileName = args[0];
  32.         String outputFileName = args[1];
  33.         Path pathToInputFile;
  34.         Path pathToOutputFile;
  35.  
  36.         try {
  37.             pathToInputFile = Paths.get(inputFileName);
  38.             pathToOutputFile = Paths.get(outputFileName);
  39.         } catch (InvalidPathException e) {
  40.             System.out.println("ERROR - Incorrect path to file: " + e.getMessage());
  41.             return;
  42.         }
  43.  
  44.         try {
  45.             if (pathToOutputFile.getParent() != null) {
  46.                 Files.createDirectories(pathToOutputFile.getParent());
  47.             }
  48.         } catch (IOException e) {
  49.             System.out.println("Couldn't access output file " + e.getMessage());
  50.             return;
  51.         }
  52.  
  53.         try (BufferedReader reader = Files.newBufferedReader(pathToInputFile)) {
  54.             try (BufferedWriter writer = Files.newBufferedWriter(pathToOutputFile)) {
  55.                 processFiles(reader, writer);
  56.             } catch (IOException e) {
  57.                 System.out.println("ERROR - Couldn't process output file " + e.getMessage());
  58.             }
  59.         } catch (IOException e) {
  60.             System.out.println("ERROR - couldn't open input file");
  61.         }
  62.  
  63.         //System.out.println("Processing completed!");
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement