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

Thread: FileDisplay Class

  1. #1

    Default FileDisplay Class

    Write a class named FileDisplay with the following methods:

    1.) constructor: the class's constructor should take the name of a fil as an arugment.
    2.) displayHead: This method should display only the first five lines of the file's contents

    Here is the following code I have made so far

    import java.io.*;
    public class FileDisplay
    {
    	private String filename;
     
    	public FileDisplay(String Filename) throws IOException
    	{
    		filename=Filename;
    		FileWriter file=new FileWriter(filename);
    		PrintWriter fout=new PrintWriter(filename);
     
    	}
     
    	public void displayHead()
    	{
     
     
    	}
    }

    First, in my constructor I have taken in an argument and used that argument to open up an output file. Meanwhile, I'm trying to work n the displayhead method to print out information and to read data to. I haven't opened up my input file yet, but I'm not understand how can I read a print data to an output file. in
    public void displayHead()
    {FileWriter file=new FileWriter(Filename)}
    do I make create another instance of the filewriter class to output data?

    --- Update ---

    Im simple words, suppose to I want to display some messages on my output file in the displayhead function. Since I already have opened up the file in the constructor, how do I combine that in this method...


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: FileDisplay Class

    who decides to read the file using FileWriter and PrintWriter?
    as what you noticed in their name, they are writer not a reader.

  3. #3

    Default Re: FileDisplay Class

    I know I have double posts going but I do have different questions now dicdic. And yes I do know that, I was just frustrated not knowing what to do so I wrote down everything quickly here. Apologies for that! I have a question here regarding opening files. If I were to use only one method in this class, I would just create an object of "FileWriter,PrintWriter and File" inside one of the method or constructor; however, if I have two or more methods, how can I possibly make methods that read and write from the same files. I'm failing to comprehend on that part so please explain that! Thank you in advance...

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: FileDisplay Class

    okay, each Object are created every time you use new keyword.
    FileWriter writer; does not create object of FileWriter but create an Object Reference Variable of class FileWriter. (But points to nowhere/nullpointer)
    new FileWriter(arg); creates an Object of FileWriter.
    FileWriter writer = new FileWriter(arg); creates an Object Reference Variable of that points to object FileWriter.
    do you get the difference between the 3 of them?

    Okay I'll give example on creating different Objects
    public class MyClass {
     
          private String name;
     
          public MyClass(String name) {
                 this.name = name;
          }
     
          // here are some methods
          public void printMe() {
                 System.out.println(name);
          }
    }

    public class Test {
     
          public static void main(String[] args) {
                MyClass my = new MyClass("dicdic");
                my.printMe();
                MyClass you = new MyClass("khan");
                you.printMe();
          }
    }
    output:
    dicdic
    khan
    as you noticed when you run that, they have different results since my and you
    have different attributes although same object.

    In your case, you can declare PrintWriter and FileWriter as global/instance.
    create object of them using constructor,
    and you can now access that in different methods.
    For Example:
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.BufferedReader;
     
    public class ReaderWriter {
            private FileReader reader;
            private FileWriter writer;
     
            public ReaderWriter(String file) throws IOException {
                  // create object of file
                  reader = new FileReader(file);
                  writer= new FileWriter(file);
            }
     
            // since you have created the object for writing and reading
            // you can easily access them in you methods
            public void printFile() throws IOException {
                  // reader variable here is the object
                  // of FileReader we have created in constructor
                  BufferedReader buffer = new BufferReader(reader);
                  String line;
                  while((line = buffer.readLine()) != null) {
                          System.out.println(line);
                  }
            }
     
            public void writeToFile(String content) {
                  // do some stuff here that writes the file
                  // using FileWriter.
            }
    }

    when you created an Object of ReaderWriter, you can easily write and read the file you passed on the constructor.
    not sure if the above codes have compile error, I just created the code here and not compiled it.
    Last edited by dicdic; March 18th, 2014 at 11:26 PM.

  5. #5

    Default Re: FileDisplay Class

    Thank you for suggestions, once again. Apologize if I'm asking to many questions but I have want to understand it completely rather then being foggy about this topic.

    1.) what's the difference if I do
    private FileReader read
    or
    FileReader read.
    I know if I have public it will be accessed from outside the class. if I use private it will only be used in this class....if I do neither public or private then wouldn't a non access specifier work since it's only in this scope.

    2.) in your sample code, this part of code creates the object of file and also opens the file, correct?
                  // create object of file
                  reader = new FileReader(file);
                  writer= new FileWriter(file);

  6. #6
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: FileDisplay Class

    1.) what's the difference if I do
    private FileReader read
    or
    FileReader read.
    Access Modifier:
    public make your object accessible by any class in any package/directory.
    private make your object not accessible by other class.
    default this is the one you used Example FileWriter writer; that declaration has default access modifier,
    actually it is accessible by other class if and only they are in the same package or directory,
    protected make your object accessible only in the same package just like default access modifier, the difference is, you can access it even in different package provided that you inherited/extends the class.

    In your case you used default, so other class in same directory can easily to modify the object we instantiate.
    and according to my trainer during training times in java, it is not conventional to use default access modifier for global/instance variable, its either you use public, private or protected.

    And base on my experience now (8 months of developing), it is better not to use default access for global/instance variables. Well, I don't know what professionals would say about my above statements.

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

    fahman_khan75@yahoo.com (March 20th, 2014)

  8. #7

    Default Re: FileDisplay Class

    Understood you , but when the
    public class rectangle
    {
    private int length;
    private int width;
    FileReader read;
    }

    This class will only remain within this scope of the class. I do understand that being public would be accessible by the classes outside and being private would only be accessible by this class. In your case this is default, which I agree but to understand it why would it not just be used for this particular class since its only within this scope...

  9. #8
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: FileDisplay Class

    This class will only remain within this scope of the class.
    sorry, am not good in english.
    do you mean that FileReader class will only remain within rectangle class?
    I think no.

    but to understand it why would it not just be used for this particular class since its only within this scope...
    ahaha, I can't understand what you mean. sorry.


    do know about getters and setters in java? something like:
    private String name;
     
    public void setName(String name) {
            this.name = name;
    }
     
    public String getName() {
            return name;
    }

  10. #9

    Default Re: FileDisplay Class

    Your English is fine. Yes that is exactly what I mean. I'm wrong in this part but this is what I'm thinking. In my main code
    public static void main(String...)
    suppose a variable like int number is declared. When that is declared this code can onky be used within the
    public static void main...
    area. In our case when we declare FileReader file in our class, then this variable can onky be used in this class. I'm aware I'm wrong but can you guide me to correct way?

  11. #10
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: FileDisplay Class

    In our case when we declare FileReader file in our class, then this variable can onky be used in this class.
    Yes it is.

Similar Threads

  1. Java, calling another public class from within the main class giving problems.
    By RandomGaisha in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 26th, 2012, 02:30 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  4. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  5. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM