Results 1 to 7 of 7
  1. #1
    gcclinux is offline Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    20
    Rep Power
    0

    Question One class calling another class

    Hi I am new to this and I hope to be using this forum allot from now on.

    My first question is that I created a little java class called ListFiles.class that when I run it gives me a output of a list of files using:

    System.out.println(list);

    The output looks like this:

    C:\Users\Admin\Testing\bkdb.txt
    C:\Users\Admin\Testing\FolderOne\FolderTwo\MainTex tFile.txt
    C:\Users\Admin\Testing\FolderOne\MainImage.bmp
    C:\Users\Admin\Testing\Main_Document.doc

    What I want is for a second class (separate file) to use this output and write it to a txt_file.txt, preferably if read the txt_file.txt and if any line already exist then ignore it, only adding new lines.

    Is that possible?

  2. #2
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    Please see api docs for FileWriter and BufferedWriter.

    And you will have to apply logic to check the condition you mentioned.

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    For updating the txt_file.txt it's easier to create a new file and then replace the old one.
    So that new class will contain one method that takes a List<String> as a parameter.
    Then it will have a loop that reads all the lines in the old file into a List<String>.
    After that, addAll the values in both Lists into a Set<String> and write out the results (Set's values) to the new file. Then rename the new file to the old file's name.

  4. #4
    gcclinux is offline Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    For updating the txt_file.txt it's easier to create a new file and then replace the old one.
    So that new class will contain one method that takes a List<String> as a parameter.
    Then it will have a loop that reads all the lines in the old file into a List<String>.
    After that, addAll the values in both Lists into a Set<String> and write out the results (Set's values) to the new file. Then rename the new file to the old file's name.
    Hi r035198x,

    Thank you for your advise I do see the benefit on creating a new file and then replace the old one. Second part I will try to figure out as I only a shell scripter and trying to learn java while converting a few of my scripts :)

    Thanks for all the help.

  5. #5
    gcclinux is offline Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    20
    Rep Power
    0

    Default

    I think I am doing something wrong as the output is not what I expect, I am sorry for my stupidity but can some one please advise.

    THIS IS THE RESULT WHEN I RUN THE FIRST CLASS

    C:\>java -cp Details.jar packages.ListFiles
    C:\temp\bkdb.txt
    C:\temp\nbu6.5.6\BitmapImage.bmp
    C:\temp\nbu6.5.6\Something.txt
    C:\temp\New Bitmap Image.bmp
    C:\temp\New Text Document.txt

    NEW CLASS

    Java Code:
    package packages;
    
    import java.util.List;
    import packages.ListFiles;
    
    public class Test {
     public static void main(String[] args) {
    
        List<String> ListFiles = null;
    
        for (String aList : ListFiles) {
        System.out.println(aList);
         }
      }
    }

    THIS IS THE RESULT WHEN I RUN THE SECOND CLASS

    C:\>java -cp Details.jar packages.Test
    Exception in thread "main" java.lang.NullPointerException
    at packages.Test.main(Test.java:16)


    I know I am duplicating work but this is just step one for me to understand how I can call one class with another and I would expect to be able to see the same result when I run both classes.

    Any help would be greatly appreciated.

    :)

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You have set
    Java Code:
    List<String> ListFiles = null;
    which is basically setting it to null you get Nullpointer problems when you try to use it like that.
    How you call your other method depends on how it is written.
    First change it so that instead of pronting the files to the console, it builds a List of the file and returns it.
    Then if the method is static you can do
    Java Code:
    List<String> listFiles = YourFirstClassName.yourMethodName();
    If it's not static you would need something like
    Java Code:
    List<String> listFiles = new YourFirstClassName().yourMethodName();

  7. #7
    gcclinux is offline Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    20
    Rep Power
    0

    Default

    r035198x you being great and really helping and cheating me, unfortunately this is only a new hobbie for me so I am not going spend money on courses and just learn by playing/ trying with it.

    Now, you mentioned that I had to change the first class which has taken me weeks to get it to work by looking at examples and adding / removing bit of code until it did what I wanted.

    So I have another question then, can I take this code I have with a for loop on specific files and convert that to a method so that the second class can access it?

    Java Code:
    public static void main(String[] args) throws FileNotFoundException {
        // USING java.io.File
        File folder = new File("C:/temp");
        // USING java.util.List
          List<File> contents = ListFiles1.getFileListing(folder);
    
    
          // PRINT OUT THE LIST OF FILES
    
    
            for (File list : contents) {
                if (list.isFile() && list.getName().toLowerCase().endsWith(".txt")
                     || list.getName().toLowerCase().endsWith(".bmp")
                     || list.getName().toLowerCase().endsWith(".doc")
            )
             System.out.println(list);
            }
    }
    This is the end bit from the First Class ( ListFiles.class )

    Sorry about the comments, I have put allot of comments in to help me to remind what is what :)

Similar Threads

  1. calling a class from another class
    By Cian in forum New To Java
    Replies: 9
    Last Post: 11-27-2010, 10:20 PM
  2. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  3. Calling a class method from another class
    By caro in forum New To Java
    Replies: 4
    Last Post: 06-10-2009, 01:12 AM
  4. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 07:35 PM
  5. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM

Posting Permissions

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