4 Replies - 4409 Views - Last Post: 13 May 2009 - 07:52 AM Rate Topic: -----

#1 jazzer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 18-February 09

writing the output of a program to a text file

Post icon  Posted 13 May 2009 - 05:50 AM

I'm trying to write the output of a program to a text file.
Can i use BufferedWriter and specify the progam name as the source of the data to be written,
eg out.write(myProgram.java) or are there some other specific methods?
Is This A Good Question/Topic? 0
  • +

Replies To: writing the output of a program to a text file

#2 ralph_komer  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 71
  • Joined: 12-May 09

Re: writing the output of a program to a text file

Posted 13 May 2009 - 06:10 AM

If you want to write the output to a file, the best way I can think of doing it is using the PrintWriter class. There is a sample code below which will help you in getting started:


import java.io.*;

public class Test
{
	public static void main(String arg[])throws Exception
	{
		Test t = new Test();

		//Getting the output stream of the file for writing
		File f = new File("Output.txt");
		FileOutputStream fos = new FileOutputStream(f);
		PrintWriter pw = new PrintWriter(fos);


		//BufferedReader to take the input from the console
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter a text: ");
		String str = br.readLine();

		//Writing the user input to the file
		pw.write(str);
		pw.flush();
		fos.close();
		pw.close();
	}
}




Instead of taking the input from the user at the command prompt, you can directly print your output to the file.
Was This Post Helpful? 0
  • +
  • -

#3 jazzer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 18-February 09

Re: writing the output of a program to a text file

Posted 13 May 2009 - 07:08 AM

Okay so that allows me to input some text and output it to a text file.
What i'm trying to do is write the output of a program to a text file.
Maybe what i need is in your code but just to clarify:
For example i have a simple program that adds two integers, say x+y. You input x=2 and y=3. The answer is 5 so i want to write '5' to the text file.
So its not the actual user input i'm writing to the file but what the program outputs.

Sorry if you already get that but ican't see where the solution is from your code.

View Postralph_komer, on 13 May, 2009 - 05:10 AM, said:

If you want to write the output to a file, the best way I can think of doing it is using the PrintWriter class. There is a sample code below which will help you in getting started:


import java.io.*;

public class Test
{
	public static void main(String arg[])throws Exception
	{
		Test t = new Test();

		//Getting the output stream of the file for writing
		File f = new File("Output.txt");
		FileOutputStream fos = new FileOutputStream(f);
		PrintWriter pw = new PrintWriter(fos);


		//BufferedReader to take the input from the console
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter a text: ");
		String str = br.readLine();

		//Writing the user input to the file
		pw.write(str);
		pw.flush();
		fos.close();
		pw.close();
	}
}




Instead of taking the input from the user at the command prompt, you can directly print your output to the file.

Was This Post Helpful? 0
  • +
  • -

#4 ralph_komer  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 71
  • Joined: 12-May 09

Re: writing the output of a program to a text file

Posted 13 May 2009 - 07:17 AM

All you need to do is to calculate (2+3) in your program and write the results to the file using the PrintWriter object instead of taking any input from the user or giving any output on the console.

I hope this clears your confusion.
Was This Post Helpful? 0
  • +
  • -

#5 jazzer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 18-February 09

Re: writing the output of a program to a text file

Posted 13 May 2009 - 07:52 AM

View Postralph_komer, on 13 May, 2009 - 06:17 AM, said:

All you need to do is to calculate (2+3) in your program and write the results to the file using the PrintWriter object instead of taking any input from the user or giving any output on the console.

I hope this clears your confusion.



I get it now, cheers for that
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1