how to write to txt file

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2009
Posts: 12
Reputation: thijo is an unknown quantity at this point 
Solved Threads: 0
thijo thijo is offline Offline
Newbie Poster

how to write to txt file

 
0
  #1
Mar 7th, 2009
hi,
I have to write my output of a java program to a txt file.
how to do this in java
thanks
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: how to write to txt file

 
0
  #2
Mar 7th, 2009
Take a look at the Java I/O package. I am sure it's a monster containing hundreads of classes but it present as many ways as there can be to write to a file in Java.

OR

You can use a PrintWriter over an OutputStream like the FileOutputStream. The FileOutputStream can be opened on a File object, or a string that specifies the filepath.

OR

For a quick example check here
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: how to write to txt file

 
0
  #3
Mar 7th, 2009
You can use BufferedWriter to write to a file:

  1. FileWriter fWriter = null;
  2. BufferedWriter writer = null;
  3. try {
  4. fWriter = new FileWriter("fileName");
  5. writer = new BufferedWriter(fWriter);
  6.  
  7. //WRITES 1 LINE TO FILE AND CHANGES LINE
  8. writer.write("This line is written");
  9. writer.newLine();
  10.  
  11.  
  12. writer.close();
  13. } catch (Exception e) {
  14.  
  15. }
Also check these APIs
BufferedWriter
The BufferedWriter's super class: Writer
FileWriter
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: how to write to txt file

 
0
  #4
Mar 7th, 2009
Originally Posted by verruckt24 View Post
Take a look at the Java I/O package. I am sure it's a monster containing hundreads of classes but it present as many ways as there can be to write to a file in Java.

OR

You can use a PrintWriter over an OutputStream like the FileOutputStream. The FileOutputStream can be opened on a File object, or a string that specifies the filepath.

OR

For a quick example check here

Together almost the same post
Last edited by javaAddict; Mar 7th, 2009 at 4:01 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 12
Reputation: thijo is an unknown quantity at this point 
Solved Threads: 0
thijo thijo is offline Offline
Newbie Poster

Re: how to write to txt file

 
0
  #5
Mar 9th, 2009
sir,
i'm trying to add 2 nos.i have the code
  1. import java.io.*;
  2. /*
  3.  * Adds 2 integers given by the user, then prints out the sum.
  4.  */
  5.  
  6.  
  7. public class Add2number {
  8.  
  9. public static void main(String[] args) throws IOException {
  10. BufferedReader stdin = new BufferedReader(
  11. new InputStreamReader(System.in));
  12.  
  13. //asks user for 1st number, then converts it to an integer
  14. System.out.print("Enter first integer: ");
  15. int x = Integer.parseInt(stdin.readLine());
  16. //asks user for 2nd number, than converts it to an integer
  17. System.out.print("Enter second integer: ");
  18. int y = Integer.parseInt(stdin.readLine());
  19.  
  20. //add x,y
  21. int sum = x + y;
  22.  
  23. //Display sum of x,y
  24. System.out.println("The sum is: " + sum);
  25. }//ends main
  26.  
  27. }//ends addReadLine

[output]
Enter first integer:12
Enter second integer:13
The sum is :25
[/output]

fine
if i want to print this output to a text file
i use another class Using.java
  1. public class Using{
  2. public static void main(String args[]) {
  3.  
  4.  
  5. try {
  6. Process p = Runtime.getRuntime().exec("java Add2number>C:\\Program Files\\Java\\jdk1.6.0_03\\bin \\out.txt");
  7. } catch(Exception e) {
  8. System.err.println(e);
  9. System.exit(-1);
  10. }
  11. } }
the output is not redirected in out.txt .i don't know where i'm going wrong.can you help in this code to print my output in a text file
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,654
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: how to write to txt file

 
0
  #6
Mar 9th, 2009
If you want to use that code, I'd suggest you simply copy and paste it into another file, and edit it so that it sends its output to a text file rather than prints it on the screen. You can use PrintWriter to do this.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: how to write to txt file

 
0
  #7
Mar 9th, 2009
I haven't written this kind of java code before but I am familiar with the redirect command: '>'

Maybe this is your problem:
  1. Process p = Runtime.getRuntime().exec("java Add2number>C:\\Program Files\\Java\\jdk1.6.0_03\\bin \\out.txt");

Have you noticed a space between \\bin and \\out
...\\bin \\out
Can you try it with this:
  1. Process p = Runtime.getRuntime().exec("java Add2number>C:\\Program Files\\Java\\jdk1.6.0_03\\bin\\out.txt");
Last edited by javaAddict; Mar 9th, 2009 at 4:47 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,654
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: how to write to txt file

 
0
  #8
Mar 10th, 2009
Good call.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: how to write to txt file

 
0
  #9
Mar 10th, 2009
Originally Posted by BestJewSinceJC View Post
Good call.
I don't know if this solves the problem. We will need thijo to reply with an update of his problem
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 229
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: how to write to txt file

 
0
  #10
Mar 10th, 2009
I see that you marked the thread solved.
What was the problem?
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3903 | Replies: 15
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC