Results 1 to 2 of 2
  1. #1
    moomoo is offline Member
    Join Date
    Apr 2008
    Posts
    1
    Rep Power
    0

    Default load all files in a directory

    hi all! im new to java and im worderin if u can help me out!
    ive wrote a small application which analysises words in a text file, and what i need to do is load all the files in a directory, rather than one at a time like in the following code. I understand that a loop is needed but i really dont know where to start! please help!
    thanks for your time
    moomoo

    import java.io.BufferedReader;

    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.HashMap;




    public class eval {


    private String[] words;
    private int[] values;
    private int num;
    private HashMap <String, Integer> hashMap;

    private arrays a;
    private String[] negwords;
    private int[] negvalues;
    private HashMap hashMap2;
    private int negnum;

    public eval() {
    initiatemap();
    BufferedReader in = null;
    try {
    in = new BufferedReader (new FileReader ("text.txt")); //id like this to load all files in a directory

    String str;
    String s;
    while ((str = in.readLine()) != null)
    {

    str = str.replaceAll("[\\p{Punct}&&[^]]", " ");
    String[]temp = str.split(" ");




    for (int i = 0; i < temp.length; i++) {
    if (hashMap.containsKey(temp[i])) {

    num++;
    }}
    for (int i = 0; i < temp.length; i++) {
    if (hashMap2.containsKey(temp[i])) {

    negnum++;

    }
    }

    }

    if (num > 0 && negnum > 0 ){

    double total = num + negnum;

    double txt1 = round((num/total)*100, 2);


    if (txt1 < 50)

    System.out.println("This text is " + txt1 + "% positive and is therefore not a happy text.");
    else if (txt1 >= 50 && txt1 <=65)
    System.out.println("This text is " + txt1 + "% positive, but does contain " + (100 - txt1) + "% negative words, it is therefore probably not a happy text.");
    else if (txt1 > 65)
    System.out.println("This text is " + txt1 + "% positive, and is therefore a happy text.");
    in.close();
    }}

    catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    public static double round(double val, int places) {
    long factor = (long)Math.pow(10,places);

    // Shift the decimal the correct number of places
    // to the right.
    val = val * factor;

    // Round to the nearest integer.
    long tmp = Math.round(val);

    // Shift the decimal the correct number of places
    // back to the left.
    return (double)tmp / factor;
    }

    public void initiatemap()
    {
    a = new arrays();
    words = a.initiateWords();
    negwords = a.initiateNegWords();
    values = a.initiateValues();
    negvalues = a.initiateNegValues();
    hashMap = a.initiateMap();
    hashMap2 = a.initiateNegMap();
    for (int i = 0; i < words.length; i++) {
    hashMap.put(words[i], values[i]);}

    for (int j = 0; j < negwords.length; j++) {
    hashMap2.put(negwords[j], negvalues[j]);}
    }

    public static void main(String[] args) {
    eval eval = new eval();
    }
    }

  2. #2
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    8

    Default

    Hey moomoo,

    Welcome to the Java-Forums!

    You can use this code to list all files in a directory:

    Java Code:
    String path = ".";
    String files;
    File folder = new File(path);
    File[] listOfFiles = folder.listFiles(); 
    		   		   
    for (int i = 0; i < listOfFiles.length; i++) {
    if (listOfFiles[i].isFile()) {
    files = listOfFiles[i].getName();
    System.out.println(files);
     }
    }
    Add your code into the for loop.

    If you want to list .txt files only then use this code:

    Java Code:
    String path = ".";
    String files;
    File folder = new File(path);
    File[] listOfFiles = folder.listFiles(); 
    		   		   
    for (int i = 0; i < listOfFiles.length; i++) {
    if (listOfFiles[i].isFile()) {
    files = listOfFiles[i].getName();
    if (files.endsWith(".txt") || files.endsWith(".TXT")){
    
    System.out.println(files);
      }
     }
    }
    Did this post help you? Please me! :cool:

Similar Threads

  1. how to find files in given directory
    By cecily in forum New To Java
    Replies: 7
    Last Post: 07-04-2014, 12:03 PM
  2. Load URL that contains spaces?
    By barkster in forum Java Applets
    Replies: 0
    Last Post: 01-30-2008, 09:40 PM
  3. Deleting an directory/subdirectory/files
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 07:18 AM
  4. How can I get list of files in a directory
    By karma in forum New To Java
    Replies: 2
    Last Post: 12-14-2007, 11:20 PM
  5. how to load a lot of files?
    By gabriel in forum Java Servlet
    Replies: 1
    Last Post: 08-07-2007, 06:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •