Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create new text file and add text to text file with JAVA

Status
Not open for further replies.

naninani123

Programmer
Aug 4, 2003
1
US
I am very new to Java, and have been searching the Internet to find what commands are needed to read a text file.I’m trying to do a program in Java that creates a new text file and write a text file using methods. Program should take a input text argument and code should open a text file( if text file not already exists need to create a new text file) , that input text what ever we are passing need to be add in a text file. I need a stand alone program to do above /Please elp me ...
 
go to java.sun.com and look at the Java tutorials

-pete
 
If you are new to Java I would advice you to download the sdk with netbeans

Download the java documentation, if you set the javadoc directory of netbeans to the folder where the you unpacked the java documentation then you can use netbeans to go to the correct api ref just by pressing ctrl + F1

To write to a file you need a file object (example in windows):
File fileToSave = new File("c:\\binuploaded.zip");
api ref:
since File is java.io.File you need to import java.io:
import java.io.*;
This example works in Windows if you want it platform independant you should use separatorChar

To create a new file:
fileToSave.createNewFile();
Check the url above what this means


To write to the file you need a fileOutputStream:
FileOutputStream fo = new FileOutputStream(fileToSave,true);
api ref:

If you have a string you want to write to the file:
fo.write(myString.getBytes());






Greetings, Harm Meijer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top