FileOutputStream is a class present in java.io package and child class of outputstream class. It Stores data in the form of bytes. It is used to create a text file.
In this program we are using BufferedReader class which is child class of Reader class is used to read data from keyboard.
BufferedReader file = new BufferedReader(new InputStreamReader(System.in));
Here file is the object of the FileOutputStream class that points to ” test txt file, if the file is not present it creates a new file.
FileoutputStream file=new FileoutputStream(“Hello.txt”);
ch=(char)br.read()
file.write(ch);
To close the file we have to close the associated streams.
file.close();
Here is the Java Example for Create a File and Write Data in Java:
import java.io.*;
class WriteDataFile
{
public static void main(String args[])
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
FileOutputStream file = new FileOutputStream("Hello.txt");
System.out.println("Enter text and Type 'y' to Terminate");
char c;
while((c=(char)br.read())!='y')
file.write(c);
file.close();
System.out.println("File is Created");
}
catch(IOException e){}
}
}