943,831 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 17244
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Mar 11th, 2009
0

Re: how to write to txt file

hi,
I think the problem could not be solved since we cannot implement user interaction using java runtime(). so i just used buffered file writer if you have any suggestion plz inform me.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thijo is offline Offline
12 posts
since Feb 2009
Mar 11th, 2009
0

Re: how to write to txt file

See the example that I mentioned in post # 2
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Mar 12th, 2009
0

Re: how to write to txt file

you need to preappend

cmd /c

my example I tested:

java Syntax (Toggle Plain Text)
  1. package redirectconsoleoutput;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. /**
  8.  *
  9.  * @author Michael
  10.  */
  11. public class Main {
  12.  
  13. /**
  14.   * @param args the command line arguments
  15.   */
  16. public static void main( String[] args ) {
  17. // TODO code application logic here
  18. System.out.println("Attempting to redirect the output of one java app to another");
  19. String s = null;
  20.  
  21. try {
  22. Process p = Runtime.getRuntime().exec("cmd /c java -jar \"D:\\Users\\Michael\\Documents\\NetBeansProjects\\SortedSetReWrite\\dist\\SortedSetReWrite.jar\" > \"D:\\SortedSetReWrite.txt\"");
  23.  
  24. BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
  25.  
  26. BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  27.  
  28. // read the output from the command
  29.  
  30. System.out.println("Here is the standard output of the command:\n");
  31. while ( (s = stdInput.readLine()) != null ) {
  32. System.out.println(s);
  33. }
  34.  
  35. // read any errors from the attempted command
  36.  
  37. System.out.println("Here is the standard error of the command (if any):\n");
  38. while ( (s = stdError.readLine()) != null ) {
  39. System.out.println(s);
  40. }
  41.  
  42. System.exit(0);
  43.  
  44. } catch ( IOException e ) {
  45. System.out.println(e.getMessage());
  46. System.exit(-1);
  47. }
  48.  
  49. }
  50. }
Last edited by Killer_Typo; Mar 12th, 2009 at 4:46 am.
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Mar 12th, 2009
0

Re: how to write to txt file

hey guys...i have one question.i have file Sample.data, in that file i write "Hello world". how to write code to separate word between "Hello" and "world" and print out it as output seperately.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lelakijadian is offline Offline
1 posts
since Mar 2009
Mar 12th, 2009
0

Re: how to write to txt file

hey guys...i have one question.i have file Sample.data, in that file i write "Hello world". how to write code to separate word between "Hello" and "world" and print out it as output seperately.
Start a new thread.

Or better search this forum. Your question has been answered a million times
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Mar 12th, 2009
0

Re: how to write to txt file

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.

Lisa11

<snip fake signature>
Last edited by Ancient Dragon; Mar 12th, 2009 at 8:06 am. Reason: snip fake signature
Reputation Points: 8
Solved Threads: 0
Newbie Poster
Lisa1110 is offline Offline
2 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Compiling error
Next Thread in Java Forum Timeline: Terminal Window in a GUI





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC