0

I have the following code seen below, this code looks through a directory and then prints all of the different file names. Now my question is, how would I go about changing my code, so that it would also print out all of the content within the files which it finds/prints? As an example, lets say the code finds 3 files in the directory, then it would print out all the content within those 3 files.

import java.io.File;
import java.io.IOException;

public class EScan {


static String usernamePc = System.getProperty("user.name");
final static File foldersPc = new File("/Users/" + usernamePc + "/Library/Mail/V2");

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

    listFilesForFolder(foldersPc);

}

public static void listFilesForFolder(final File foldersPc) throws IOException {
    for (final File fileEntry : foldersPc.listFiles()) {
        if (fileEntry.isDirectory()) {
            listFilesForFolder(fileEntry);
        } else {
            System.out.println(fileEntry.getName());
        }
    }
}

}

3 Answers3

1

I tested it before posting. it is working.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author EdwinAdeola
 */
public class TestPrintAllFiles {

    public static void main(String[] args) {
        //Accessing the folder path
        File myFolder = new File("C:\\Intel");
        File[] listOfFiles = myFolder.listFiles();
        String fileName, line = null;
        BufferedReader br;
        //For each loop to print the content of each file
        for (File eachFile : listOfFiles) {
            if (eachFile.isFile()) {
                try {
                    //System.out.println(eachFile.getName());
                    fileName = eachFile.getAbsolutePath();
                    br = new BufferedReader(new FileReader(fileName));

                    try {
                        while ((line = br.readLine()) != null) {
                            System.out.println(line);
                        }
                    } catch (IOException ex) {
                        Logger.getLogger(TestPrintAllFiles.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(TestPrintAllFiles.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
Edwinfad
  • 515
  • 6
  • 14
  • I appreciate the code, I have just tested it and while it does compile without any problems. The output of the code is some really weird characters with like a black square with a ? in it. I am not sure if it has something to do with the actual files it is reading. The files within my folder are .emlx – fsharpsquare Nov 22 '16 at 15:32
0

You may use Scanner to read the contents of the file

try {
        Scanner sc = new Scanner(fileEntry);

        while (sc.hasNextLine()) {
            String s = sc.nextLine();
            System.out.println(s);
        }

        sc.close();
    } catch (Exception e) {
        e.printStackTrace();
}
  • I appreciate the code, where would I go about adding this to my code? – fsharpsquare Nov 22 '16 at 15:45
  • I have just tested it, I put it within the else statement and it seems to be working! Thank you very much. If you have the time on your hands, do you know whether there is a way to only have it print the emails within the files. So like if it finds a word including .com or @ then it would print that word? – fsharpsquare Nov 22 '16 at 15:56
  • You may use regex to validate first if the String is a valid email. http://stackoverflow.com/questions/8204680/java-regex-email – Jessie Brian Revil Nov 23 '16 at 00:56
0

You can try one more way if you find suitable :

package com.grs.stackOverFlow.pack10;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;

public class EScan {

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

        File dir=new File("C:/your drive/");
        List<File> files = Arrays.asList(dir.listFiles(f->f.isFile())); 
//if you want you can filter files like f->f.getName().endsWtih(".csv")

        for(File f: files){
            List<String> lines = Files.readAllLines(f.toPath(),Charset.defaultCharset());
            //processing line  
            lines.forEach(System.out::println);
        }
    }
}

Above code can me exploited in number of ways like processing line can be modified to add quotes around lines as below:

lines.stream().map(t-> "'" + t+"'").forEach(System.out::println);

Or print only error messages lines

lines.stream().filter(l->l.contains("error")).forEach(System.out::println);

Above codes and variations are tested.

Goro
  • 516
  • 4
  • 15
  • I appreciate the code, I have just tested it. It doesn't have any errors in the start, but when compiling the code it gets a couple of different errors. Please check out this screen shot to see. (https://snag.gy/keN3Wo.jpg) – fsharpsquare Nov 22 '16 at 15:44
  • @fsharpsquare : i have added new update to use default charset while reading files. It should work now. But if problem persist you can try changing charset like Files.readAllLines(f.toPath(),Charset.forName("UTF-8"); – Goro Nov 22 '16 at 16:55