| | |
how to write to txt file
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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
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 !!!
You can use BufferedWriter to write to a file:
Also check these APIs
BufferedWriter
The BufferedWriter's super class: Writer
FileWriter
Java Syntax (Toggle Plain Text)
FileWriter fWriter = null; BufferedWriter writer = null; try { fWriter = new FileWriter("fileName"); writer = new BufferedWriter(fWriter); //WRITES 1 LINE TO FILE AND CHANGES LINE writer.write("This line is written"); writer.newLine(); writer.close(); } catch (Exception e) { }
BufferedWriter
The BufferedWriter's super class: Writer
FileWriter
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
•
•
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
•
•
Join Date: Feb 2009
Posts: 12
Reputation:
Solved Threads: 0
sir,
i'm trying to add 2 nos.i have the code
[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 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
i'm trying to add 2 nos.i have the code
java Syntax (Toggle Plain Text)
import java.io.*; /* * Adds 2 integers given by the user, then prints out the sum. */ public class Add2number { public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); //asks user for 1st number, then converts it to an integer System.out.print("Enter first integer: "); int x = Integer.parseInt(stdin.readLine()); //asks user for 2nd number, than converts it to an integer System.out.print("Enter second integer: "); int y = Integer.parseInt(stdin.readLine()); //add x,y int sum = x + y; //Display sum of x,y System.out.println("The sum is: " + sum); }//ends main }//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
java Syntax (Toggle Plain Text)
public class Using{ public static void main(String args[]) { try { Process p = Runtime.getRuntime().exec("java Add2number>C:\\Program Files\\Java\\jdk1.6.0_03\\bin \\out.txt"); } catch(Exception e) { System.err.println(e); System.exit(-1); } } }
I haven't written this kind of java code before but I am familiar with the redirect command: '>'
Maybe this is your problem:
Have you noticed a space between \\bin and \\out
...\\bin \\out
Can you try it with this:
Maybe this is your problem:
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
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
I see that you marked the thread solved.
What was the problem?
What was the problem?
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- Why my executable jar file cant write a text file (Java)
- Need to write postfix to a file (Java)
- The Fastest way to read a .txt File (C++)
- how to write a .txt file (C++)
- Write determinate controls to txt file (C#)
- getting words from .txt file (C++)
- .txt file formatting assignment due TODAY help!! (C)
Other Threads in the Java Forum
- Previous Thread: Compiling error
- Next Thread: Terminal Window in a GUI
Views: 3903 | Replies: 15
| Thread Tools | Search this Thread |
Tag cloud for Java
add android api apple applet application arguments array arrays automation bank binary bluetooth chat chooser class classes client code component converter database digit draw eclipse equation error event exception file fractal functiontesting game givemetehcodez graphics gui health helpwithhomework html hyper ide idea image infinite input int integer j2me java javame javaprojects jmf jni jpanel julia linux list loop main map method methods mobile myregfun netbeans newbie nonstatic number object oracle pattern pearl print problem program programming project recursion scanner screen scrollbar server set size sms socket sort spamblocker sql sqlserver string superclass swing test thread threads time transfer tree windows






