0

i am a nibble in java. i have my own efforts to get the things done. but certainly i am facing a challenge. i have a dummy program that searches for files of a particular extension(.txt) supplied as a command line argument. i am trying to make file objects of these searched file for further manipulations. but i can't understand how to do this in my code.. here is my code sample...

public class Find {

public static class Finder extends SimpleFileVisitor<Path> {

    private final PathMatcher matcher;
    private int numMatches = 0;

    Finder(String pattern) {
        matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
    }

     void find(Path file) {
        Path name = file.getFileName();
        if (name != null && matcher.matches(name)) {
            numMatches++;
            System.out.println(file);
        }
    }

    // Prints the total number of
    // matches to standard out.
    void done() {
        System.out.println("Matched: "+ numMatches);
    }

    // Invoke the pattern matching
    // method on each file.
    //@Override
    public FileVisitResult visitFile(Path file,
            BasicFileAttributes attrs) {
        find(file);
        return CONTINUE;
    }

    // Invoke the pattern matching
    // method on each directory.
    //@Override
    public FileVisitResult preVisitDirectory(Path dir,
            BasicFileAttributes attrs) {
        find(dir);
        return CONTINUE;
    }

    //@Override
    public FileVisitResult visitFileFailed(Path file, IOException exc) {
        System.err.println(exc);
        return CONTINUE;
    }
}


public static void main(String[] args) throws IOException {

    Iterable<Path> root;
    root = FileSystems.getDefault().getRootDirectories();
   // System.out.println(name.getAbsolutePath());
    for (Path startingDir : FileSystems.getDefault().getRootDirectories()) {
       String pattern = args[0];

        Finder finder = new Finder(pattern);
        Files.walkFileTree(startingDir, finder);

    }
}

}

Vicky
  • 23
  • 1
  • 3
  • 9
  • So where exactly are you having problems? – Kayaman Jul 31 '13 at 07:24
  • I suggest you to give a look at the [File](http://docs.oracle.com/javase/6/docs/api/java/io/File.html) class. Let me forward you to [this question](http://stackoverflow.com/questions/6142901/how-to-create-a-file-in-a-directory-in-java) and [this question](http://stackoverflow.com/questions/2885173/java-how-to-create-and-write-to-a-file); – MaVVamaldo Jul 31 '13 at 07:25
  • @Kayaman the output of my program is a long list of text files with their absolute path. now i want to make objects of these files so that i can upload these to a URL. to upload them i have to make a stream with file object to be sent. – Vicky Jul 31 '13 at 07:37

1 Answers1

0

here is what i am trying to do. the output of my program is a long list of text files with their absolute path. now i want to make objects of these files so that i can upload these to a URL. to upload them i have to make a stream with file object to be sent..how to get absoluteFilename?? to get this you must have a file object...right.... and my revised question is : how to make file objects of searched files???

FileInputStream fileInputStream = null;

    try {
        new FileInputStream("absoluteFilename");

        byte[] buffer = new byte[MAX_SIZE];
        int bufferIndex = 0;
        while (fileInputStream.available() > 0) {
            buffer[bufferIndex++] = (byte) fileInputStream.read();
        }
        byte[] fileContent = new byte[bufferIndex];
        System.arraycopy(buffer,0,fileContent,0,bufferIndex);

        URL serverUrl = new URL(url);
        URLConnection connection = serverURL.openConnection();
        connection.setConnectTimeout(60000);
        connection.getOutputStream().write(fileContent);

    } catch (Exception fatal) {
        //proper handling??
Vicky
  • 23
  • 1
  • 3
  • 9