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

Thread: How to open files when in classes instead of the main code.

  1. #1

    Default How to open files when in classes instead of the main code.

    Members, please help me understand these question. I have looked over other people code relating to files that are related to my actual studies in class. My teacher hasn't worked on a lot of file problems so I want to gain some understanding on it.

    First question:
    1.) I have this class with variables declared
    import java.io.*;
    public class FileDisplay
    {
    	private String filename;
    	FileWriter outputfile;
    	File Inputfile;

    I'm aware that having a private variable would not allow that variable to be accessed outside the code. Vice versa, public variabl would do that. The FileWriter and the File Inputfile is something I don't understand, especially have them in classes. What is the purpose of them?

    2.)
    public class FileDisplay
    {
    	private String filename;
    	FileWriter outputfile;
    	File Inputfile;
     
     
    	//Create a constructor
    	public FileDisplay(String Name_file) throws IOException
    	{
    		filename=Name_file;
     
    		//Open the output file
    			new FileWriter(filename);
     
    		//Open the input file
    			new File("Results.txt");
    	}
    }
    I have made this code here that opens the output and input file. If I make another method, how would I display the text onto that same file. Hard to clarify for me, but in each method do I just keep creating
     FileWriter outputfile=new Filewriter(filename)


  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: How to open files when in classes instead of the main code.

            public FileDisplay(String Name_file) throws IOException
    	{
    		filename=Name_file;
     
    		//Open the output file
    			new FileWriter(filename);
     
    		//Open the input file
    			new File("Results.txt");
    	}
    in your constructor, you created FileWriter and File,
    but you ignored them.
    it would be better if you stored them in variable.
    something like outputfile = new FileWriter(filename);

    how would I display the text onto that same file
    do mean to display the content of the file? if so, I think FileWriter cannot do that.

  3. #3

    Default Re: How to open files when in classes instead of the main code.

    Thank you for your kindly response. I have improved on my code a tad bit and have come up with this.
    public class FileDisplay
    {
    	private String filename;
    	FileWriter outputfile;
    	PrintWriter print;
    	File Inputfile;
    	Scanner inputfile;
     
     
    	//Create a constructor
    	public FileDisplay(String Name_file) throws IOException
    	{
    		filename=Name_file;
     
    		//Open the output file
    			outputfile=new FileWriter(filename);
    			print=new PrintWriter(outputfile);
     
    		//Open the input file
    			Inputfile=new File("Results.txt");
    			inputfile=new Scanner(Inputfile);
     
     
    	}
     
    	//Make a method that display the first 5 lines
    	public void displayHeader() throws IOException
    	{
    		int counter=1;
    		while(inputfile.hasNext()&&counter<6)
    		{
    			String Line=inputfile.nextLine();
    			print.println(Line);
    			counter++;
    		}

    I did fix what you told me to. I'm going to making more methods in this class. I want to know is this practice good, meaning declaring the objects of Filewriter, printwriter and file in the beginning, if I'm going to create lots of methods that involves using files?

    --- Update ---

    Also when you say "store them into a variable." Is outputfile not suppose to be an object of a class rather then variable?

  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: How to open files when in classes instead of the main code.

    Is outputfile not suppose to be an object of a class rather then variable?
    I think they are called Object Reference Variable, the variable that points to the object created as a way of accessing them.
    FileWriter, PrintWriter and other classes in java are Objects, and the variable that is created after them are the Object Reference variable.
    Do you just want to display the file content? Why do you use PrintWriter and FileWriter class?

    I want to know is this practice good, meaning declaring the objects of Filewriter, printwriter and file in the beginning,
    yes that is ok, that usually the way we create them(with private access modifier so that other class cannot easily change their values). they are the global variables, so i think they are the attributes of the class.
    Last edited by dicdic; March 18th, 2014 at 11:34 PM.

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

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

  6. #5

    Default Re: How to open files when in classes instead of the main code.

    Thank you! Did help me a lot and I did read your other post which seemed to help be a lot I appreciate it. I know how to write codes using files but couldn't understand how to use files in classes

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

    Default Re: How to open files when in classes instead of the main code.

    you better read the doc for the class your not sure of
    java.io.File Files (Java Platform SE 7 )

    To make it short, File class is not capable of reading and writing from and to files.
    I think it is just the handle of the file.

    In java.io.File we can:
    create file,
    delete file,
    create/delete directory
    create symbolic link etc etc..

    but we cannot write/read it.
    for some instances, we will need this class before reading since classes that are designed for reading and writing files accepts the File class for their construction of files.
    like for example:
    I'm going to write to a file,
    but before I write, I must create the file first named me.txt.
    for that instance I'm going to use File class and create a file,
    next I'm going to pass it to other class that can write on it.
    Well, that was just an example

  8. #7

    Default Re: How to open files when in classes instead of the main code.

    haha...I should clarified about how to use "files" in classes not how to use the file class Appreciate our response though!

  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: How to open files when in classes instead of the main code.

    haha...I should clarified about how to use "files" in classes not how to use the file class Appreciate our response though!
    ahahahah sorry man, I'm not good in english

    but what do you mean by this?:
    but couldn't understand how to use files in classes

Similar Threads

  1. Experiencing problems w/ Scanner & main, open to any assistance!
    By BlackStones in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 9th, 2014, 01:23 PM
  2. how to open one Jframe from main method call
    By ankushpruthi in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 1st, 2013, 05:23 PM
  3. Why am I getting a <No Main Classes Found> error message?? Help please!!
    By Rick Sebastian in forum What's Wrong With My Code?
    Replies: 13
    Last Post: February 9th, 2013, 07:04 PM
  4. How to open files that are represented in a JTree
    By reptar693 in forum AWT / Java Swing
    Replies: 2
    Last Post: March 30th, 2012, 12:11 PM
  5. could not open `C:\Program Files\Java\jre6\lib\amd64\jvm.cfg
    By miaaa00 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 29th, 2011, 12:45 AM