Jump to content

How to Load and Print to a TextFile in Java?

- - - - -

  • Please log in to reply
12 replies to this topic

#1
Laxman2809

Laxman2809

    Newbie

  • Members
  • PipPip
  • 12 posts
So I have finally completed my program, and now need to output the array to a text file and then have the ability to save it, and then load that file. So far I have started the on the saving, but I can seem to get it to create the file.
import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.PrintStream;

import java.util.Scanner;

public class Test {


	/**

	 * @author Alexander Chamerlaion

	 * @Version 1.0

	 * @Date 10/12/11

	 * This program creates and tests Social Events

	 */

	static String title, attire, dayweek,input,time;

	static int day,month,year;

	public static void main(String[] args) {

		SocialEventList sel = new SocialEventList();

		Scanner k = new Scanner(System.in);

		for(int i = 0; i<1000;++i){

			System.out.println("Please enter what you would like to add/execute/quit \n(Party, Club Meeting, Dance Class,Date Check,Display,Save,Load,Quit)");

			String input = k.nextLine();

			input=input.toUpperCase();

			if(input.equals("PARTY")){

				System.out.println("Choosen a Party");

				System.out.println("Please enter a description of your event: ");

				title=k.next();

				System.out.println("Please Enter an attire: ");

				attire = k.next();

				System.out.println("Please Enter an time: ");

				time=k.next();

				System.out.println("Please enter the date input the day first: ");

				day=k.nextInt();

				System.out.println("Month: ");

				month=k.nextInt();

				System.out.println("Year: ");

				year=k.nextInt();

				System.out.println("Please enter the day it occurs on(Monday,Tuesday,etc): ");

				dayweek=k.next();

				Party p = new Party(attire, dayweek, title, day, month, year,time);

				p.setPartyTime(time);

				p.setPartyTitle(title);

				p.setPartyAttire(attire);

				p.setPartyMonth(month);

				p.setPartyYear(year);

				p.setPartyDay(day);

				p.setPartyDayweek(dayweek);

				sel.add(p);

				sel.display();

			}

			else if (input.equals("DANCE CLASS")){

				System.out.println("Choosen a Dance Class");

				System.out.println("Please enter a description of your event: ");

				title=k.next();

				System.out.println("Please Enter an attire: ");

				attire = k.next();

				System.out.println("Please Enter an time: ");

				time=k.next();

				System.out.println("Please enter the date input the day first: ");

				day=k.nextInt();

				System.out.println("Month: ");

				month=k.nextInt();

				System.out.println("Year: ");

				year=k.nextInt();

				System.out.println("Please enter the day it occurs on(Monday,Tuesday,etc): ");

				dayweek=k.next();

				DanceClass d = new DanceClass(attire, title, time, day, month, year, dayweek);

				d.setDanceTitle(title);

				d.setDanceAttire(attire);

				d.setDanceTime(time);

				d.setDanceDay(day);

				d.setDanceMonth(month);

				d.setDanceYear(year);

				d.setDanceDayweek(dayweek);

				sel.add(d);

				sel.display();

			}

			else if(input.equals("CLUB MEETING")){

				System.out.println("You've Choosen A Club Meeting ");

				System.out.println("Please enter a description of your event: ");

				title=k.next();

				System.out.println("Please Enter an attire: ");

				attire = k.next();

				System.out.println("Please Enter an time: ");

				time=k.next();

				System.out.println("Please enter the date input the day first: ");

				day=k.nextInt();

				System.out.println("Month: ");

				month=k.nextInt();

				System.out.println("Year: ");

				year=k.nextInt();

				System.out.println("Please enter the day it occurs on(Monday,Tuesday,etc): ");

				dayweek=k.next();

				ClubMeeting c = new ClubMeeting(attire, title, time, day, month, year, dayweek);

				c.setClubTitle(title);

				c.setClubAttire(attire);

				c.setClubTime(time);

				c.setClubDay(day);

				c.setClubMonth(month);

				c.setClubYear(year);

				c.setClubDayweek(dayweek);

				sel.add(c);

				sel.display();


			}

			else if(input.equals("DISPLAY")){

				sel.display();

			}

			else if(input.equals("DATE CHECK")){

				System.out.println("Please enter a date to be checked");

				System.out.println("Please enter the day: ");

				day=k.nextInt();

				System.out.println("Please enter the month: ");

				month=k.nextInt();

				System.out.println("Please enter the year: ");

				year=k.nextInt();

				sel.occursOn(month,day,year);

			}

			else if(input.equals("SAVE")){

				sel.saveAppointmentData();

			}

			else if(input.equals("QUIT")){

				break;

			}

		}	

	}

}
import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.PrintStream;



public class SocialEventList {

	private SocialEvent[] thelist = new SocialEvent[10];

	private int i = 0;

	private int counter = 0;

	

	public void add(SocialEvent se){

		if(i<thelist.length){

			thelist[i]=se;

			++i;

			System.out.println(thelist[i]);

		}

	}

	public void display(){

		for(int j = 0; j < i; j++){                         

			System.out.println(thelist[j].toString(i));

		}

	}


	public void occursOn(int month, int day, int year) {

		for(int h = 0; h<i;h++)

		{

			if( month==thelist[h].getMonth()&&day==thelist[h].getDay() &&year==thelist[h].getYear() ) {

				System.out.println("You have event "+h+ " that occurs on the same day, check to make sure you have enough time.");

				display();

			}

			else{

				System.out.println("This event "+h+ " are no two events occur on this same day");

			}

		}

	}

		public void saveAppointmentData(){

			PrintStream out = null;

			try {

				out = new PrintStream(new FileOutputStream(toString()));

				out.println();

			}

			catch(FileNotFoundException e){

				System.out.println("Error opening the file ");

				System.exit(0);

			}

			

	}	

	

	

	}

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.PrintStream;



public class SocialEvent {

	private String title;

	private String attire;

	private String time;

	private int day;

	private int month;

	private int year;

	private String dayweek;

	private String save;

	

	public SocialEvent(String attire,String dayweek, String title,int day,int month,int year, String time){

		this.attire = attire;

		this.dayweek = dayweek;

		this.title = title;

		this.day = day;

		this.month = month;

		this.year = year;

		this.time = time;

	}

	


	public String getTitle() {

		return title;

	}



	public void setTitle(String title) {

		this.title = title;

	}



	public String getAttire() {

		return attire;

	}



	public void setAttire(String attire) {

		this.attire = attire;

	}



	public String getTime() {

		return time;

	}



	public void setTime(String time) {

		this.time = time;

	}



	public int getDay() {

		return day;

	}



	public void setDay(int day) {

		this.day = day;

	}



	public int getMonth() {

		return month;

	}



	public void setMonth(int month) {

		this.month = month;

	}



	public int getYear() {

		return year;

	}



	public void setYear(int year) {

		this.year = year;

	}



	public String getDayweek() {

		return dayweek;

	}



	public void setDayweek(String dayweek) {

		this.dayweek = dayweek;

	}

	

	public String toString(int i) {

		return "\nTitle: "+title+"\nAttire: "+attire+"\nDate: "+month+"/"+day+"/"+year+"\nDay of the Week: "+

				dayweek+"\nTime: "+time;

	}


}


other 3 classes set up like this

public class Party extends SocialEvent {

	


	public Party(String attire,String dayweek, String title,int day,int month,int year, String time){

		super(attire, dayweek, title, day, month, year,time);


	}


	public void setPartyTime(String partyTime){

		super.setTime(partyTime);

	}

	public String getPartyTime(){

		return super.getTime();

	}


	public String getPartyAttire() {

		return super.getAttire();

	}


	public void setPartyAttire(String partyAttire) {

		super.setAttire(partyAttire);

	}


	public String getPartyTitle() {

		return super.getTitle();

	}


	public void setPartyTitle(String partyTitle) {

		super.setTitle(partyTitle);

	}


	public String getPartyDayweek() {

		return super.getDayweek();

	}


	public void setPartyDayweek(String partyDayweek) {

		super.setDayweek(partyDayweek);

	}


	public int getDay() {

		return super.getDay();

		

	}


	public void setPartyDay(int partyDay) {

		super.setDay(partyDay);

	}


	public int getMonth() {

		return super.getMonth();

	}


	public void setPartyMonth(int partyMonth) {

		super.setMonth(partyMonth);

	}



	public int getYear() {

		return super.getYear();

	}


	public void setPartyYear(int partyYear) {

		super.setYear(partyYear);

	}

	public String toString(int i){

		return "Party" + super.toString(i);

	}


}




#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Errors?
This works fine for me:

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.PrintStream;


public class OutputTest {


	/**

	 * @param args

	 */

	public static void main(String[] args) {

		// TODO Auto-generated method stub

		PrintStream out = null;

		OutputTest ot = new OutputTest();

		try {

			out = new PrintStream( new FileOutputStream(ot.toString()));

			out.println();

		} catch (FileNotFoundException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		} finally {

			out.close();

		}

	}


}


BTW, don't forget to .close() your outputstream when you're finished with it.

#3
Laxman2809

Laxman2809

    Newbie

  • Members
  • PipPip
  • 12 posts
It outputs it to something very weird and does not create a text file it gives me random lettering. Also it does not print out me array.
With finally added, and i put it to String save
public void saveAppointmentData(){

			PrintStream out = null;

			try {

				out = new PrintStream(new FileOutputStream(toString()));

				out.println(save);

			}

			catch(FileNotFoundException e){

				System.out.println("Error opening the file ");

				System.exit(0);

			}

			finally {

				out.close();

			}

			

	}	


#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Well the toString() is your problem. You don't have a toString() method defined for any of your classes.
You do however have a toString(int i) method defined, but it's not the same.

Try this:

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.PrintStream;


public class OutputTest {


	/**

	 * @param args

	 */

	public static void main(String[] args) {

		// TODO Auto-generated method stub

		PrintStream out = null;

		OutputTest ot = new OutputTest();

		

		Object someObject = new Object();

		

		try {

			out = new PrintStream( new FileOutputStream("SomeFakeFile.txt"));

		} catch (FileNotFoundException e) {

			// TODO Auto-generated catch block

			e.printStackTrace();

		} finally {

			out.close();

		}

		

		System.out.println(out);

		System.out.println(out.toString());

		

		System.out.println(ot);

		System.out.println(ot.toString());

		

		System.out.println(someObject);

		System.out.println(someObject.toString());

	}


}

The output is:

java.io.PrintStream@cac268

java.io.PrintStream@cac268

outputStreamHelp.OutputTest@1a16869

outputStreamHelp.OutputTest@1a16869

java.lang.Object@1cde100

java.lang.Object@1cde100


Can you figure out why you have a random file with weird letters/numbers now?

#5
Laxman2809

Laxman2809

    Newbie

  • Members
  • PipPip
  • 12 posts
I thought
public String toString(){

		return "Party" + super.toString();

	}
which is^ from the party class
public String toString(int i) {

		return "\nTitle: "+title+"\nAttire: "+attire+"\nDate: "+month+"/"+day+"/"+year+"\nDay of the Week: "+

				dayweek+"\nTime: "+time;

	}

From SocialEvent^
isn't the the toString() which is in both my super/sub classes

Also you are calling the object in your example
		Object someObject = new Object();

which I can't do because i am getting errors on the passing variables
	public void saveAppointmentData(){

			SocialEvent se = new SocialEvent(attire, dayweek, title, day, month, year,time);

			PrintStream out = null; 

			try {

				out = new PrintStream(new FileOutputStream(se.toString(i)));

				out.println(save);

			}

			catch(FileNotFoundException e){

				System.out.println("Error opening the file ");

				System.exit(0);

			}

			finally {

				out.close();

			}

			


#6
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
toString(int i) isn't the same as toString()

If you haven't defined a toString() method for the class, then a default toString() method is generated AND it returns an object's address in memory.

So for example, when you have
FileOutputStream( toString() ); 

the contents of toString are equal to some address in memory.
Example:

Quote

outputStreamHelp.OutputTest@1a16869


#7
Laxman2809

Laxman2809

    Newbie

  • Members
  • PipPip
  • 12 posts
ok so then if i change it to
public String toString() {

		return "\nTitle: "+title+"\nAttire: "+attire+"\nDate: "+month+"/"+day+"/"+year+"\nDay of the Week: "+

				dayweek+"\nTime: "+time;

	}


}

doesnt that make it a the toString method that allows for it to be called?

#8
Laxman2809

Laxman2809

    Newbie

  • Members
  • PipPip
  • 12 posts
Little update I want to switch to an ObjectOutputStream which i have done here
Ok i have now switched from just an output stream to an ObjectOutput except I have an error here 


[CODE]				ObjectOutputStream ois = ObjectOutputStream(save);


Can anyone tell me why?

public void saveAppointmentData(){

		PrintStream out = null;

			try {

				out = new PrintStream(new FileOutputStream(save));

				ObjectOutputStream ois = ObjectOutputStream(save);

				ois.writeObject(thelist);

				System.out.println("You have just saved your file ");

			}

		catch(FileNotFoundException e){

			System.out.println("Error opening the file ");

			System.exit(0);

		}		

	}
Except I have an error here
				ObjectOutputStream ois = ObjectOutputStream(save);



#9
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
It's hard to say anything without the error details.

#10
Laxman2809

Laxman2809

    Newbie

  • Members
  • PipPip
  • 12 posts
OK that works, but now why won't my toString work?
import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.PrintStream;



public class SocialEvent {

	private String title;

	private String attire;

	private String time;

	private int day;

	private int month;

	private int year;

	private String dayweek;

	private String save;

	

	public SocialEvent(String attire,String dayweek, String title,int day,int month,int year, String time){

		this.attire = attire;

		this.dayweek = dayweek;

		this.title = title;

		this.day = day;

		this.month = month;

		this.year = year;

		this.time = time;

	}

	

	public String getTitle() {

		return title;

	}



	public void setTitle(String title) {

		this.title = title;

	}



	public String getAttire() {

		return attire;

	}



	public void setAttire(String attire) {

		this.attire = attire;

	}



	public String getTime() {

		return time;

	}



	public void setTime(String time) {

		this.time = time;

	}



	public int getDay() {

		return day;

	}



	public void setDay(int day) {

		this.day = day;

	}



	public int getMonth() {

		return month;

	}



	public void setMonth(int month) {

		this.month = month;

	}



	public int getYear() {

		return year;

	}



	public void setYear(int year) {

		this.year = year;

	}



	public String getDayweek() {

		return dayweek;

	}



	public void setDayweek(String dayweek) {

		this.dayweek = dayweek;

	}


	@Override

	public String toString() {

		return "\nTitle: "+title+"\nAttire: "+attire+"\nDate: "+month+"/"+day+"/"+year+"\nDay of the Week: "+

				dayweek+"\nTime: "+time;

	}


}


	



	public void saveAppointmentData(){

		try {

			BufferedWriter out = new BufferedWriter (new FileWriter (save));

			for (int i=0; i<thelist.length; i++){

				out.write(toString());

				System.out.println("File Saved");

			}

			out.close ();

		} catch (FileNotFoundException e) {

			System.out.println("Error!");

		} catch (IOException e) {

			System.out.println("Error!");

		}

	}


#11
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
You haven't supplied enough information? How is it not working? Compile error, runtime error, human error?
Is the file being created at all? If so, with what file name?

#12
Laxman2809

Laxman2809

    Newbie

  • Members
  • PipPip
  • 12 posts
File name is save.txt and it prints out the memory position in the file




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users