Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-19-2008, 02:57 PM
Member
 
Join Date: Apr 2008
Posts: 1
moomoo is on a distinguished road
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

Quote:
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();
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-21-2008, 11:18 AM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
Hey moomoo,

Welcome to the Java-Forums!

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

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:

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
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Load URL that contains spaces? barkster Java Applets 0 01-30-2008 10:40 PM
Deleting an directory/subdirectory/files Java Tip Java Tips 0 01-13-2008 08:18 AM
How can I get list of files in a directory karma New To Java 2 12-15-2007 12:20 AM
how to load a lot of files? gabriel Java Servlet 1 08-07-2007 07:04 PM
how to find files in given directory cecily New To Java 1 08-05-2007 05:26 AM


All times are GMT +3. The time now is 07:41 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org