Results 1 to 8 of 8
Thread: hope to help me
- 05-27-2010, 03:34 PM #1
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
hope to help me
am working on my retreival project and the project is to open a directory and take the text files from this directory and count each word occurrence am success to open the directory and build my program but i have aproplem opening the files
i take all the text files into array and open it sequentially but the program say cannot open file and it is exist please help me and here is my code and by the way i need to print the result in a file
hope to get quick replyPHP Code:package learn; import java.util.*; // Provides TreeMap, Iterator, Scanner import java.io.*; // Provides FileReader, FileNotFoundException public class retreival { public static void main(String[ ] args) { // **THIS CREATES A TREE MAP** TreeMap<String, Integer> frequencyData = new TreeMap<String, Integer>( ); readWordFile(frequencyData); printAllCounts(frequencyData); } public static int getCount (String word, TreeMap<String, Integer> frequencyData) { if (frequencyData.containsKey(word)) { // The word has occurred before, so get its count from the map return frequencyData.get(word); // Auto-unboxed } else { // No occurrences of this word return 0; } } public static void printAllCounts(TreeMap<String, Integer> frequencyData) { System.out.println("-----------------------------------------------"); System.out.println(" Occurrences Word"); for(String word : frequencyData.keySet( )) { System.out.printf("%15d %s\n", frequencyData.get(word), word); } System.out.println("-----------------------------------------------"); } public static void readWordFile(TreeMap<String, Integer> frequencyData) { Scanner wordFile; String word; // A word read from the file Integer count; // The number of occurrences of the word String path; Scanner sc =new Scanner(System.in); System.out.println("enter your path plz"); path=sc.nextLine(); if (path.isEmpty()||path==null) { System.out.println("emty path" + " " + path); } else { File dir=new File(path); String[] h=dir.list(); for (int i=0; i<h.length; i++) { String filename = h[i]; } FilenameFilter filter = new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".txt"); } }; h = dir.list(filter); //**FOR LOOP TO READ THE DOCUMENTS** for (int x=0; x<h.length; x++) { //start of for loop [* try { wordFile = new Scanner(new FileReader(h[x])); } catch (FileNotFoundException e) { System.err.println(e); return; } while (wordFile.hasNext( )) { // Read the next word and get rid of the end-of-line marker if needed: word = wordFile.next( ); // This makes the Word lower case. word = word.toLowerCase(); // Get the current count of this word, add one, and then store the new count: count = getCount(word, frequencyData) + 1; frequencyData.put(word, count); } } //End of for loop *] } } }
thank u at all
- 05-27-2010, 03:53 PM #2
Print out the name with delimiters (">" + filename +"<") and look at it closely to be sure there are no extra characters.program say cannot open file
What line in the program has the problem?
- 05-27-2010, 04:15 PM #3
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
sory for missunderstanding i mean that their is a problem with opening the files and here is the output with errors
and i check the names of files like u til me and here is the outputPHP Code:run: enter your path plz C:\h ----------------------------------------------- Occurrences Word java.io.FileNotFoundException: b.txt (The system cannot find the file specified) ----------------------------------------------- BUILD SUCCESSFUL (total time: 2 seconds)
PHP Code:run: enter your path plz C:\h >b.txt< >c.txt< BUILD SUCCESSFUL (total time: 6 seconds)
- 05-27-2010, 04:27 PM #4
So the dir.list() call returns 2 files and the FileReader() call fails? Does it fail on the first file or when?What line in the program has the problem?
- 05-27-2010, 04:30 PM #5
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
yea it is in the first file
and i dont know in wich line the error occur
- 05-27-2010, 04:35 PM #6
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Don't use list(), but rather listFiles() to obtain the Files in the directory. It probably has something to do with not using the entire path to the file, but rather just the file name.
- 05-27-2010, 04:41 PM #7
What folder is the FileReader looking in? Is it the same folder that the dir.list() method got the list of files from?
- 05-27-2010, 04:43 PM #8
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
hope there is an easier way.
By IYIaster in forum New To JavaReplies: 6Last Post: 10-14-2009, 07:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks