Creating a Text file using Java



//Write a java program for creating a text file

import java.io.*;
import java.io.OutputStream;

class FileAccess
{
public static void main(String args[])throws IOException
{

//Attach keyboard to DataInputStream

DataInputStream dis=new DataInputStream(System.in);

//Attach myfile to fileoutputstream

FileOutputStream fos=new FileOutputStream(“xxxx.txt”);

//read data from datainputstream and write it into fileoutputstream

ObjectOutputStream p=new ObjectOutputStream(fos);
char ch;
System.out.println(“Enter date(@ to end the program);”);
while((ch=(char)dis.read())!=’@')
fos.write(ch);
p.writeInt(12345);
p.writeObject(“Today”);
p.putFields();

//p.writeStreamHeader();

p.writeFields();

p.flush();

//closefikle

fos.close();
}
}

Procedure:

First compile the program and run the program then given the inputs as a string then exit that and u can fine the file in the destination u had specified

Random Posts

  • What is Thread Safe Servlet
  • EJB INTERVIEW QUESTIONS
  • Servlet Important Questions
  • Hibernate Basics
  • Define Stored Procedures

Post a Comment