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 11 of 11

Thread: Problem related to file handling example

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    6
    My Mood
    Sad
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Problem related to file handling example

    Well hello everyone. I am facing a problem while executing a task. My task is to make such a code which will search for my source code file (abc.java) in all directories and then displays the code in textarea inside frame and along with that i also have to display the path of the file in text field. I have coded something but i am really not getting anywhere near my task. Sincere help needed urgently.Thankx in advance.

     
     
    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
    import java.util.*
     
    public class Lab extends JFrame {
     
        static JTextField jf;
        static String Files;
        static File ff = new File("Lab.java");
        static File ff1 = new File("D:\\");
     
        public static void main(String[] args) {
            Lab bf = new Lab();					//Making Frame
            bf.setBounds(20, 20, 400, 400);
            bf.setLayout(new BorderLayout());
     
            JTextField jf = new JTextField("");
     
            TextArea jt = new TextArea("                                                                 ");
            Search(ff1);
            bf.add(jf, BorderLayout.NORTH);						//Now adding the  ALL panelS to frame
            bf.add(jt, BorderLayout.CENTER);
            bf.setVisible(true);
     
        }       //Main Ends
     
        public static void Search(File file) {
     
            try {
                FileInputStream fis = new FileInputStream(file);
                /* if (!ff.exists()) {
                 throw new FileNotFoundException("file doesn't exist");
                 }*/
     
                File[] listOfFiles = file.listFiles();
     
                for (int i = 0; i < listOfFiles.length; i++) {
     
                    if (listOfFiles[i].isDirectory()) {
                        Search(listOfFiles[i]);
     
                    } else if (!(listOfFiles[i].isDirectory())) {
     
                        Files = listOfFiles[i].getName();
                        if (Files.equals("Lab.java")) {
                            jf.setText("file found");
     
                        }
     
                    }
                }
                String path = ff.getAbsolutePath();
     
            } catch (Exception e) {
     
            }
     
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem related to file handling example

    Can you explain what problems you are having and ask some specific questions about your task?
    What steps for the task have you completed and tested?
    What steps need to be done?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    6
    My Mood
    Sad
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem related to file handling example

    Well i am have been able to create JtextField and TextArea . Now i am trying to search the file in D drive ... I pasted my source code file (.java file) in D drive but it is not giving me the path in TextField . After that i will read the file and display the code in TextArea

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem related to file handling example

    Work on the problems one at a time: search the file in D drive
    What steps is the code taking to do that?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    6
    My Mood
    Sad
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem related to file handling example

    I will get the path of file using getabsolutepath and then i will list files in that directory and check for equality with my file name ... using

    if(file.equals("abc.text"){}

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem related to file handling example

    OK, is that working? If not where are you having problems?
    Where does the code test for subdirectories and search them?

    For developing and testing the search code, write a small simple program to do that without any GUI. It should have just the code for the search.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2014
    Posts
    6
    My Mood
    Sad
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem related to file handling example

    The problem is that i have to submit the code after three hours and everything is messed up here.. I m doing coding at the moment.. Please help me if possible or at least stay in touch while i code the problem and ask you the answers to my question

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem related to file handling example

    Start by making a small program with a method that searchs for the desired file. After you get that to work, the code/technique can be copied to the larger program.

    You need to decide on the steps the program should take BEFORE writing code.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2014
    Posts
    6
    My Mood
    Sad
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem related to file handling example

    Got it .. DONE THANKX FOR THE GUIDANCE
    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
    import java.util.*;
     
    public class Lab2 extends JFrame {
     
        static String temp;
        static File Base = new File(".//Lab2.java");
        static File ff = new File("D:\\");
        static JTextField jf = new JTextField("finding");
        static TextArea jt = new TextArea("                                                                 ");
     
        public static void main(String[] args) {
            Lab2 bf = new Lab2();					//Making Frame
            bf.setBounds(20, 20, 400, 400);
            bf.setLayout(new BorderLayout());
     
            bf.add(jf, BorderLayout.NORTH);						//Now adding the  ALL panelS to frame
            bf.add(jt, BorderLayout.CENTER);
            bf.setVisible(true);
            Search(ff);
        }       //Main Ends
     
        public static void Search(File file) {
     
            String absolutepath = file.getAbsolutePath();
            File ff1 = new File(absolutepath);
            File[] List = ff1.listFiles(); //Listing all the files
     
            /*if (List == null) {
             return;
             }*/        try {
                for (File f : List) {
                    if (f.isDirectory()) { //If file is directory we call the method again 
                        Search(f);
                    } else if (!(f.isDirectory())) { //If not directory  then we check for file
                        if (f.getName().equals("Lab2.java")) {
                            temp = f.getAbsolutePath();
                            jf.setText(f.getAbsolutePath());
                            FileInputStream fis = new FileInputStream(f);
                            byte[] b = new byte[(int) f.length()];  //Now we read bytes to input stream
                            fis.read(b);
     
                            for (int j = 0; j < b.length; j++) {
                                                                    //Show output stream
                                jt.append("" + (char) b[j]);
                            }
                            break;
     
                        }
                    }
                }
            } catch (Exception e) {
            }
        } // Search Ends
     
    }   //Class ends[COLOR="Silver"]
     
    --- Update ---
     
    [/COLOR]Got it :D .. DONE THANKX FOR THE GUIDANCE
    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
    import java.util.*;
     
    public class Lab2 extends JFrame {
     
        static String temp;
        static File Base = new File(".//Lab2.java");
        static File ff = new File("D:\\");
        static JTextField jf = new JTextField("finding");
        static TextArea jt = new TextArea("                                                                 ");
     
        public static void main(String[] args) {
            Lab2 bf = new Lab2();					//Making Frame
            bf.setBounds(20, 20, 400, 400);
            bf.setLayout(new BorderLayout());
     
            bf.add(jf, BorderLayout.NORTH);						//Now adding the  ALL panelS to frame
            bf.add(jt, BorderLayout.CENTER);
            bf.setVisible(true);
            Search(ff);
        }       //Main Ends
     
        public static void Search(File file) {
     
            String absolutepath = file.getAbsolutePath();
            File ff1 = new File(absolutepath);
            File[] List = ff1.listFiles(); //Listing all the files
     
            /*if (List == null) {
             return;
             }*/        try {
                for (File f : List) {
                    if (f.isDirectory()) { //If file is directory we call the method again 
                        Search(f);
                    } else if (!(f.isDirectory())) { //If not directory  then we check for file
                        if (f.getName().equals("Lab2.java")) {
                            temp = f.getAbsolutePath();
                            jf.setText(f.getAbsolutePath());
                            FileInputStream fis = new FileInputStream(f);
                            byte[] b = new byte[(int) f.length()];  //Now we read bytes to input stream
                            fis.read(b);
     
                            for (int j = 0; j < b.length; j++) {
                                                                    //Show output stream
                                jt.append("" + (char) b[j]);
                            }
                            break;
     
                        }
                    }
                }
            } catch (Exception e) {
            }
        } // Search Ends
     
    }   //Class ends
    Last edited by Shaz1234; May 6th, 2014 at 02:34 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem related to file handling example

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Shaz1234 (May 6th, 2014)

  12. #11
    Junior Member
    Join Date
    May 2014
    Posts
    6
    My Mood
    Sad
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem related to file handling example

    Done so is the code good enough?

Similar Threads

  1. [SOLVED] File handling issue
    By aesguitar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 11th, 2014, 08:55 PM
  2. help me in this problem related to ml-java
    By Haneen tar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 21st, 2012, 02:26 PM
  3. Layout related problem
    By Gaurav18 in forum Member Introductions
    Replies: 3
    Last Post: September 14th, 2011, 05:12 AM
  4. Regarding File Handling
    By ravjot28 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:45 PM
  5. File handling in java
    By srikanth in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: February 22nd, 2010, 02:11 AM