Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Searching a word in a compressed file

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Searching a word in a compressed file

    first, i will introduce myself in my first post.
    i'm studying java programming language in college (actually, ima noob btw),
    and i have a task to search a word in a compressed file and index the files which contain the words, and save the path

    guys, i really don't know where to start.
    i already searching some source codes to study for, but i dont have a single glimpse of light in google.
    i read there are some codes similar to my task goal, but they're using lucene which i'd never heard of. And some says it's really complicated to understand, so instead try javaIO which i don't understand either.
    Please, maybe anyone here can show me a source code that i can learn by myself + google (or maybe you could post the codes with some comments on it).
    feel free to ask the detailed task conditions (i didnt write it clear, coz i think its a silly question to post it in detail where probably everyone will ignore this question)
    *ANW, sorry for my bloody english, ima indonesian. peace & love ^_^V


  2. #2
    Junior Member
    Join Date
    Dec 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Searching a word in a compressed file

    first, i will introduce myself in my first post.
    i'm studying java programming language in college (actually, ima noob btw),
    and i have a task to search a word in a compressed file and index the files which contain the words, and save the path

    guys, i really don't know where to start.
    i already searching some source codes to study for, but i dont have a single glimpse of light in google.
    i read there are some codes similar to my task goal, but they're using lucene which i'd never heard of. And some says it's really complicated to understand, so instead try javaIO which i don't understand either.
    Please, maybe anyone here can show me a source code that i can learn by myself + google (or maybe you could post the codes with some comments on it).
    feel free to ask the detailed task conditions (i didnt write it clear, coz i think its a silly question to post it in detail where probably everyone will ignore this question)
    *ANW, sorry for my bloody english, ima indonesian. peace & love ^_^V

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,225
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: Searching a word in a compressed file

    Start with the simple, easy to do part of the program.
    Hardcode the filename of the compressed file in your program. Then using the File class create a File object and print out some statistics for that file: size, last mod date etc.

    To make sense of the bytes in the file, you will need a definition/description for how the file is compressed.
    What is the relationship between the "compressed" file and the "files to be searched"?
    If you don't understand my answer, don't ignore it, ask a question.

  4. The Following User Says Thank You to Norm For This Useful Post:

    Insence8 (December 4th, 2013)

  5. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Searching a word in a compressed file

    i dont get it bro (hardcode),
    and did you mean using the file class create a file object is : extract the file (with JavaIO? and pour it into a file object?)

    its a .7zip (lol, is it a defintion actually? )

    here the detailed :
    1.indexing a file given through path (c:/x.7z alphabetical << ex)
    2.remember the given path
    3.input some word(s) then search the files containing that given word(s). The file list should be printed with path

    here im trying to do the task 1, here's the code (still error tho, dunno how it works honestly) :


    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package folder.indexer;
    import java.io.IOException;
    import java.util.Enumeration;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;


    /**
    *
    * @author Ridel
    */
    public class FolderIndexer {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    String [] index;
    int count = 1;
    try {
    ZipFile sourceZipFile = new ZipFile("C:/Users/Ridel/Downloads/Compressed/4. Folder Indexer/20_newsgroups.7z");
    Enumeration e = sourceZipFile.entries();
    while (e.hasMoreElements()) {
    e.nextElement();
    count++;

    }
    sourceZipFile.close();
    sourceZipFile = new ZipFile("C:/Users/Ridel/Downloads/Compressed/4. Folder Indexer/20_newsgroups.7z");
    e = sourceZipFile.entries();

    index = new String [count];

    for (int i = 0; i < index.length; i++) {
    for (int j = 0; j < (index.length - i - 1); j++) {
    if (index[i].compareToIgnoreCase(index[i+1]) > 0) {
    String temp = index[j];
    index[j] = index[j+1];
    index[j+1] = temp;
    }
    }
    }

    for (int i = 0; i < index.length; i++) {
    System.out.println(index[i]);
    }

    sourceZipFile.close();

    } catch(IOException ioe) {
    System.out.println("Error opening zip file" + ioe);
    }
    }
    }


    PS: try copy the code and paste it to reply box, it should ease the reading (if you have a hard time to read it,

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,225
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: Searching a word in a compressed file

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    This is an example of hardcoding. I meant to code it in the program instead of asking the user for it. Your code does it here:
     new ZipFile("C:/Users/Ridel/Downloads/Compressed/4. Folder Indexer/20_newsgroups.7z");

    still error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Insence8 (December 4th, 2013)

  8. #6
    Junior Member
    Join Date
    Dec 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Searching a word in a compressed file

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package folder.indexer;
    import java.io.IOException;
    import java.util.Enumeration;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;
     
     
    /**
     *
     * @author Ridel
     */
    public class FolderIndexer {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            String [] index;
            int count = 1;
            try {
                ZipFile sourceZipFile = new ZipFile("C:/Users/Ridel/Downloads/Compressed/4. Folder Indexer/20_newsgroups.7z");
                Enumeration e = sourceZipFile.entries();
                while (e.hasMoreElements()) {
                    e.nextElement();
                    count++;
     
                }
                sourceZipFile.close();
                sourceZipFile = new ZipFile("C:/Users/Ridel/Downloads/Compressed/4. Folder Indexer/20_newsgroups.7z");
                e = sourceZipFile.entries();
     
                index = new String [count];
     
                for (int i = 0; i < index.length; i++) {
                    for (int j = 0; j < (index.length - i - 1); j++) {
                        if (index[i].compareToIgnoreCase(index[i+1]) > 0) {
                            String temp = index[j];
                            index[j] = index[j+1];
                            index[j+1] = temp;
                        }
                    }
                }
     
                for (int i = 0; i < index.length; i++) {
                    System.out.println(index[i]);
                }
     
                sourceZipFile.close();
     
            } catch(IOException ioe) {
                System.out.println("Error opening zip file" + ioe);
            }
        }
    }

    it always print the exception...

    btw, here i try to do some printing of the zipped file just like you told
    (i dont know how to print size, etc... i just woke up and read your reply, and the code below is written in a hurry < have a data structure class to attend in 15 mins)

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package folder.indexer;
    import java.io.IOException;
    import java.util.Enumeration;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;
     
     
    /**
     *
     * @author Ridel
     */
    public class FolderIndexer {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
     
            try {
                ZipFile sourceZipFile = new ZipFile("C:/Users/Ridel/Downloads/Compressed/4. Folder Indexer/20_newsgroups.7z");
                Enumeration e = sourceZipFile.entries();
                while (e.hasMoreElements()) {
                    System.out.println(e);
                    e.nextElement();
                }
                sourceZipFile.close();
     
     
     
     
     
     
     
     
     
     
            } catch(IOException ioe) {
                System.out.println("Error opening zip file" + ioe);
            }
        }
    }

    same error, always printing the exception.

  9. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,225
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: Searching a word in a compressed file

    it always print the exception...
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Add a call to the printStackTrace() method to the catch block to get the full error message.
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    Insence8 (December 4th, 2013)

  11. #8
    Junior Member
    Join Date
    Dec 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Searching a word in a compressed file

    nevermind dude,
    the file doesnt need to be .7z ...
    its k if it extracted.

    and now im trying the code for indexing (maybe printing the list folder),
    still stuck ini a stupid problem...
    please check my code

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package folder.indexer;
     
    import java.io.IOException;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
     
     
    /**
     *
     * @author Ridel
     */
    public class FolderIndexer {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            Scanner input =  new Scanner(System.in);
            String quit = "exit";
            System.out.print("Type the path need to be indexed : ");
            String command = input.next();
     
            while (!command.equals(quit)) {
                    try {
                        System.out.print("Type the path need to be indexed : ");
                        File file = new File(command);
                        String [] files = file.list();
                        for (int i = 0; i < files.length; i++) {
                            System.out.println(files[i]);
                        }
                    } catch (NullPointerException e) {
                        System.out.println("File Not Found. Error : " + e);
                        break;
                    }
     
     
            }
     
     
     
     
     
     
     
            /* this worked, but when i change : File file = new File(command) it stop working like i want.
            try {
                File file = new File("C:/Users/Ridel/Downloads/Compressed/4. Folder Indexer/20_newsgroups");
                String [] files = file.list();
                for (int i = 0; i < files.length; i++) {
                    System.out.println(files[i]);
     
                }
            } catch (NullPointerException e) {
                System.out.println("File Not Found. Error : " + e);
            }*/
     
     
     
     
     
        }
     
     
     
     
    }

    always throw java.lang.NullPointerException
    (i confirmed that the path i gave is correct. tried copy-paste the path that worked before and put it on the console)

  12. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,225
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: Searching a word in a compressed file

    throw java.lang.NullPointerException
    Please copy the full text of the error message and paste it here. It has important info about the error. Make sure all the catch blocks have a call to the printStackTrace() method.

    What variable has the null value? Find the variable and then backtrack to see why it does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    Insence8 (December 4th, 2013)

  14. #10
    Junior Member
    Join Date
    Dec 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Searching a word in a compressed file

    yes, it is the output already :
    java.lang.NullPointerException


    i dont think there was a variable with null value.
    please take a close look at my code above...

    Scanner input = new Scanner(System.in);
    String command = input.next(); //input through console, done.
    File file = new File(command); //how it become null?

  15. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,225
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: Searching a word in a compressed file

    Make sure all the catch blocks have a call to the printStackTrace() method. The error message will give the line number where the error happens.
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    Insence8 (December 4th, 2013)

  17. #12
    Junior Member
    Join Date
    Dec 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Searching a word in a compressed file

    oh, you mean like this ?
    Methods.java
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package folder.indexer;
    import java.io.File;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    /**
     *
     * @author Ridel
     */
    public class Methods {
        protected void searchDir(String dir) {
            File xDir = new File(dir);
            boolean found = false;
            String [] fileList = xDir.list();
            try {
                for (int i = 0; i < fileList.length; i++) {
                    File xFile = new File(fileList[i]);
                    found = readFile(xFile);
                    if (found == true) {
                        System.out.println(fileList[i]);
                    }
                }
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }
     
     
        //read a single file
        private boolean readFile(File xFile) {
            boolean found = false;
            String key = "use";
            //StringBuilder contents = new StringBuilder();
            BufferedReader reader = null;
     
            try {
                reader = new BufferedReader(new FileReader(xFile));
                while (reader.readLine() != null) {
                    if (key.equals(reader.readLine())) {
                        found = true;
                    }
                }
            } catch (IOException ioe) {
                System.out.println(ioe);
            }
            return found;
        }
    }

    package folder.indexer;
    import java.io.File;
    /*
    import java.io.FileNotFoundException;
    import java.util.Scanner;*/
     
     
     
    /**
     *
     * @author Ridel
     */
    public class FolderIndexer {
     
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Methods methods = new Methods();
            File dir = new File("C:/Users/Ridel/Downloads/Compressed/4. Folder Indexer/20_newsgroups");
     
            String [] fileList = dir.list();
            for (int i = 0; i < fileList.length; i++) {
                methods.searchDir(fileList[i]);
            }
     
        }
     
     
    }

    now i got this error repeated 10 time more/less :
    java.lang.NullPointerException
    at folder.indexer.Methods.searchDir(Methods.java:20)
    at folder.indexer.FolderIndexer.main(FolderIndexer.ja va:29)

    i dont know why i always got this stupid NullPointerException...
    im sure, there's no variable valued to null and the path i gave is 100% correct.

  18. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,225
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: Searching a word in a compressed file

    java.lang.NullPointerException
    at folder.indexer.Methods.searchDir(Methods.java:20)
    There is a variable with a null value when line 20 is executed. Look at line 20, find the variable with the null value and then backtrack in the code to see why that variable does not have a valid value.

    Add some println() statements to print out the values of all the variables that the code is using so you can see what the computer sees when the code is executed.
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    Insence8 (December 4th, 2013)

  20. #14
    Junior Member
    Join Date
    Dec 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Searching a word in a compressed file

    its a looping for. -_-
    then creating File xFile = new File(String [i])
    i backtracked to readFile method, still dont know what is wrong..
    i think, creating File file = new File("path") is the problem here.
    cos, when i changed it to File file = new File(variable) the value become null.
    so, exactly how i suppose to create object with File type and give it a value returned from a method or with a nonstatic value?

  21. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,225
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: Searching a word in a compressed file

    What variable has the null value? How did it get a null value?

    Note: Some methods return a null value when they are called if they can not find any data.
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    Insence8 (December 4th, 2013)

  23. #16
    Junior Member
    Join Date
    Dec 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Searching a word in a compressed file

    check this out dude,
    good fuking job you there!!

    package folder.indexer;
    import java.io.File;
     
    /*
    import java.io.FileNotFoundException;
    import java.util.Scanner;*/
     
     
     
    /**
     *
     * @author Ridel
     */
    public class FolderIndexer {
     
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Methods methods = new Methods();
            File dir = new File("C:/Users/Ridel/Downloads/Compressed/4. Folder Indexer/20_newsgroups");
            String [] fileList = dir.list();
            String path = "C:/Users/Ridel/Downloads/Compressed/4. Folder Indexer/20_newsgroups/";
            for (int i = 0; i < fileList.length; i++) {
                String fileName = path.concat(fileList[i]);
                methods.searchDir(fileName);
            }
     
     
     
        }
     
     
     
     
     
     
     
     
        }

    package folder.indexer;
    import java.io.File;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    /**
     *
     * @author Ridel
     */
    public class Methods {
        protected void searchDir(String dir) {
            File xDir = new File(dir);
            boolean found = false;
            String [] fileList = xDir.list();
            try {
                for (int i = 0; i < fileList.length; i++) {
                    File xFile = new File(fileList[i]);
                    String fileName = xFile.getName();
                    String x = dir;
                    x = x.concat("/" + fileName);
                    found = readFile(x);
     
     
                    if (found == true) {
                        System.out.println(fileList[i]);
                    }
                }
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }
     
     
        //read a single file
        private boolean readFile(String xFile) {
            boolean found = false;
            String key = "Xref:";
     
            BufferedReader reader = null;
     
            try {
                reader = new BufferedReader(new FileReader(xFile));
                String x = reader.readLine();
                if (x.contains(key) == true) {
                    System.out.println(xFile);
                    found = true;
                }
     
            } catch (IOException ioe) {
                System.out.println(ioe);
            }
            return found;
        }
    }

    great dude, that last words about the file isn't there....
    man, i was a fuking clumsy moron :v
    Thank you very much man, a big fuking thanks man!!! (y)

    anw, i want to learn more about java programming,
    wouldcha be my mentor? :3
    *jk, i know you won't....

  24. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,225
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: Searching a word in a compressed file

    Do you have any more questions about your program? Is so, post them.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Searching for a word in file
    By justyStepi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 4th, 2013, 08:39 AM
  2. read a file word by word
    By poornima2806 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2012, 02:14 PM
  3. Content in text file to MS Word file help!
    By ComputerSaysNo in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 27th, 2011, 11:39 AM
  4. Reading a text file word by word
    By dylanka in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 21st, 2011, 02:06 PM
  5. [SOLVED] Compressed data from XML file
    By eclipse in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: July 15th, 2010, 02:39 PM

Tags for this Thread