11 Replies - 1140 Views - Last Post: 05 October 2009 - 12:48 PM Rate Topic: -----

#1 rebbieboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-September 09

Saving and opening files

Post icon  Posted 05 October 2009 - 12:15 AM

Ive been reading a lot lately about this problem and still cant find a solution, i already encountered buferedreader and i cant seem to understand it, will tell me what shoul i use to save and open a file in java, and where does it get saved, and can i save a folder file? Since im gona do a adres book and decided to use Tree is that ok?,. Much thanks, an example would be great a simple one,
Is This A Good Question/Topic? 0
  • +

Replies To: Saving and opening files

#2 Ricendithas  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 12-July 08

Re: Saving and opening files

Posted 05 October 2009 - 06:06 AM

Here's an idea. Why don't you use the FileReader class and the PrintWriter class both from the java.io package?

I hope this helps. :)
Was This Post Helpful? 0
  • +
  • -

#3 rebbieboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-September 09

Re: Saving and opening files

Posted 05 October 2009 - 07:37 AM

View PostRicendithas, on 5 Oct, 2009 - 05:06 AM, said:

Here's an idea. Why don't you use the FileReader class and the PrintWriter class both from the java.io package?

I hope this helps. :)

example please i have this code but cant understand it
Was This Post Helpful? 0
  • +
  • -

#4 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2587
  • View blog
  • Posts: 15,619
  • Joined: 24-September 08

Re: Saving and opening files

Posted 05 October 2009 - 07:40 AM

Theres an open and save dialog box in swing already made that looks just like windows. Check out gabehabe's tutorial on making a notepad application. If it's just console, you would append to a file using file.println(String s); or something like that. Haven't fooled with this in a while. Could be wrong.
Was This Post Helpful? 0
  • +
  • -

#5 rebbieboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-September 09

Re: Saving and opening files

Posted 05 October 2009 - 07:51 AM

 public class TestNodeStreams{
public static void main(String[] args)
{
	  try
{
FileReader input =new FileReader(args[0]);
FileWriter output = new FileWriter(args[1]);
char[] charsRead;

charsRead = input.read(buffer);

while (charsRead != -1)
{
output.write(buffer, 0, charsRead);

charsRead = input.read(buffer);
}
input.close();
output.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}


/*sory for the typos im using my mobile phone,. When i run it it does nothing at all, doesnt teach me how to save a file, help please, i got that from our book SL-275*/

View PostNeoTifa, on 5 Oct, 2009 - 06:40 AM, said:

Theres an open and save dialog box in swing already made that looks just like windows. Check out gabehabe's tutorial on making a notepad application. If it's just console, you would append to a file using file.println(String s); or something like that. Haven't fooled with this in a while. Could be wrong.

link on that please and thank you, will you also help me with the code i posted

*Edited: fix the closing [ /code]

This post has been edited by pbl: 05 October 2009 - 04:45 PM

Was This Post Helpful? 0
  • +
  • -

#6 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2587
  • View blog
  • Posts: 15,619
  • Joined: 24-September 08

Re: Saving and opening files

Posted 05 October 2009 - 07:53 AM

:code: and try doing this on a computer. Running a step through debugger can show you exactly what your code is doing, unless its the Netbeans 6.7 or whatever, then it's shit. I like the 5 series myself ^___^
Was This Post Helpful? 0
  • +
  • -

#7 rebbieboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-September 09

Re: Saving and opening files

Posted 05 October 2009 - 08:00 AM

View PostNeoTifa, on 5 Oct, 2009 - 06:53 AM, said:

:code: and try doing this on a computer. Running a step through debugger can show you exactly what your code is doing, unless its the Netbeans 6.7 or whatever, then it's shit. I like the 5 series myself ^___^

was using jcreator the code compiles and runs the only problem is, it does nothing at all,. Link on your previous post please regarding the notepad thingy,
Was This Post Helpful? 0
  • +
  • -

#8 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2587
  • View blog
  • Posts: 15,619
  • Joined: 24-September 08

Re: Saving and opening files

Posted 05 October 2009 - 08:08 AM

Ugh, kids these days, so lazy. http://www.dreaminco...wtopic66176.htm
Was This Post Helpful? 0
  • +
  • -

#9 rebbieboi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-September 09

Re: Saving and opening files

Posted 05 October 2009 - 08:26 AM

View PostNeoTifa, on 5 Oct, 2009 - 07:08 AM, said:

Ugh, kids these days, so lazy. http://www.dreaminco...wtopic66176.htm



:) i got lazy searching every tutorial that would help me, i just ended up having a head ache cause everything mixed up,. -_-,. Thanks though,.
Was This Post Helpful? 0
  • +
  • -

#10 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2587
  • View blog
  • Posts: 15,619
  • Joined: 24-September 08

Re: Saving and opening files

Posted 05 October 2009 - 08:35 AM

Np. You should click the "this post was helpful" button under my post ^__^. Also, some tutorials have some abstract stuff in it, so you never know what you're gonna find...
Was This Post Helpful? 0
  • +
  • -

#11 kzhuravl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-September 09

Re: Saving and opening files

Posted 05 October 2009 - 12:24 PM

Your object(which you wanna save or load) can implement Serializable. Here is wiki link for Serialization:
http://en.wikipedia....i/Serialization
Was This Post Helpful? 0
  • +
  • -

#12 payamrastogi  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 30-July 08

Re: Saving and opening files

Posted 05 October 2009 - 12:48 PM

may be this is helpful to you ...

Program to copy contents of two files to the third file

import java.io.*;

class CopyFile
{
	public static void main(String args[])throws IOException
	{
		if(args.length<3)
		{
			System.out.print("Parameters missing");
			System.exit(0);
		}
		else if(args.length>3)
		{
			System.out.print("Too many parameters");
			System.exit(0);
		}
		else
		{
			File f1 = new File(args[0]);
			File f2 = new File(args[1]);
			if((!f1.exists())||(!f2.exists()))
			{
				System.out.println("One or more source file(s) does not exist");
			}
			else
			{
				FileInputStream fis1 = new FileInputStream(f1);
				FileInputStream fis2 = new FileInputStream(f2);
				BufferedInputStream bis = new BufferedInputStream(fis1);
				//BufferedInputStream bis2 = new BufferedInputStream(fis2);
				FileOutputStream fos = new FileOutputStream(args[args.length-1],true);
				BufferedOutputStream bos = new BufferedOutputStream(fos);
				for(int i=0;i<2;i++)
				{
					int ch;
					while((ch = bis.read())!=-1)
					{
						bos.write((char)ch);
					}
					bis = new BufferedInputStream(fis2);
				}
				bis.close();
				fis1.close();
				fis2.close();
				bos.close();
				fos.close();
			}
			
		}	
	}
}


This post has been edited by payamrastogi: 05 October 2009 - 12:49 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1