5 Replies - 4219 Views - Last Post: 13 February 2008 - 07:33 AM Rate Topic: -----

#1 Maverick519  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 01-December 07

Writing to a text file

Post icon  Posted 04 February 2008 - 12:10 PM

im clueless

this is what i have.

try 
		{
			name = jTextField1.getText(); 
			dob = jTextField2.getText();
			BufferedWriter out = new BufferedWriter(new FileWriter());
			out.write(name);
			out.write(dob);
			out.close();
		}
		catch (IOException e) 
		{
			
		}



Like in not sure if im doing this right. I want to take two strings from text field and when a button is clicked it will do this and send it to a text file and then i need to read from a file to display it on a text area.

Is This A Good Question/Topic? 0
  • +

Replies To: Writing to a text file

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,635
  • Joined: 16-October 07

Re: Writing to a text file

Posted 04 February 2008 - 01:56 PM

You're on you way. You want to, at the very least, print out the error that lands in the catch, so you know what's going on. Also, "out" is not the best name, since System.out is standard it could be confusing. I'd prefer something like writer or fwriter.

The biggest thing I see, though: files need names. You aren't going to be about to write to a file until you decide on a file name. This is honestly kind of a google question. Try here: http://www.roseindia...e-to-file.shtml

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 Maverick519  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 01-December 07

Re: Writing to a text file

Posted 04 February 2008 - 02:06 PM

Got it lol now i need to read from the file gosh.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Writing to a text file

Posted 04 February 2008 - 05:25 PM

View PostMaverick519, on 4 Feb, 2008 - 03:06 PM, said:

Got it lol now i need to read from the file gosh.


Here's a snippet of code to get you on the right path (this was written assuming a .data file was made, changelable, etc...):

String fileName = JOptionPane.showInputDialog(null, "What is the name of the file you wish to read in?\n" +
	 		"(.data is included, not necessary)");
	 		
	 	ArrayList dateList = new ArrayList();	
	 	//Read in the objects from the file
	File dateInFile;
	FileInputStream inFileStream; 
	ObjectInputStream inObjectStream;	//***
	try {
		dateInFile = new File( fileName + ".data" );
		inFileStream = new FileInputStream( dateInFile );
		inObjectStream = new ObjectInputStream( inFileStream );  //***
		dateList = (ArrayList) inObjectStream.readObject();
		inObjectStream.close();
	}
	catch (IOException e) {
		System.out.println( "threw an IOException...deal with it..." ); //files must be in the same directory
	}
	catch (ClassNotFoundException e) {	//readObject can throw this
		System.out.println( "threw an ClassNotFoundException...deal with it..." );
	}

//so on and so forht



Hope that helps.

--KYA
Was This Post Helpful? 0
  • +
  • -

#5 Maverick519  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 01-December 07

Re: Writing to a text file

Posted 05 February 2008 - 12:11 PM

ok i got it to read from the file some how. But it does not read all of the info.

		FileOutputStream out; // declare a file output object
		 PrintStream p; // declare a print stream object

		 try
		 {
			 name = jTextField1.getText(); 
			 dob = jTextField2.getText();
			 out = new FileOutputStream("asdf.txt");
			 p = new PrintStream( out );
			 p.println(name);
			 p.println(dob);
			 p.close();
			 
			 // Open the file that is the first 
			 // command line parameter
			 FileInputStream fstream = new FileInputStream("asdf.txt");
			 // Convert our input stream to a
			 // DataInputStream
			 DataInputStream in = new DataInputStream(fstream);

			 // Continue to read lines while 
			 // there are still some left to read
			 while (in.available() !=0)
			 {
				// Print file line to screen
				blah = in.readLine();
				jTextArea1.setText(blah);
			 }

			 in.close();
		 }
		 catch (Exception e)
		 {
			 System.err.println ("Error writing or reading to file");
		 }	 



i enter my name and date of birth into 2 text fields and then it should write both of them to a text file which it does write both. Then it needs to read both and write them in a text area. But it only writes the date of birth. Idk why

never mind i just got it like 5 secs after i posted this my bad
Was This Post Helpful? 0
  • +
  • -

#6 Guest_dato.java*


Reputation:

Re: Writing to a text file

Posted 13 February 2008 - 07:33 AM

View PostMaverick519, on 4 Feb, 2008 - 12:10 PM, said:

im clueless

this is what i have.

try 
		{
			name = jTextField1.getText(); 
			dob = jTextField2.getText();
	BufferedWriter out = new BufferedWriter(new FileWriter());
			out.write(name);
			out.write(dob);
			out.close();
		}
		catch (IOException e) 
		{
			
		}



Like in not sure if im doing this right. I want to take two strings from text field and when a button is clicked it will do this and send it to a text file and then i need to read from a file to display it on a text area.


you had to write: BufferedWriter out = new BufferedWriter(new FileWriter(<filename>));
you have to point an absolute or canonical path of file ;
Was This Post Helpful? 0

Page 1 of 1