The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Just have a look Indra - Keep in touch

Posted by Jeganathan S on October 20, 2000 at 4:36 AM

Hi! Indra,

import java.io.*;

//Indira i think u r aware of the fact that if u use DataOutputStream it will the
//in binary mode so even though there is a end of line indicator available u won't
//be able 2 see it in different line, to ensure that only i have provided this
//program which also has got code for reading from file which reads line by line
//and then it will print, it is possible only b'coz it is written line by line

//I like people who have thirst of Java

public class Indra
{
DataInputStream dis;
DataOutputStream dos;
String str;
FileInputStream fis;
FileOutputStream fos;
File f;
String line;
String l;

Indra()
{
str= new String();
line = new String();
try
{
f = new File("Indra.bin");
fos = new FileOutputStream(f);
dos = new DataOutputStream(fos);
}
catch(IOException e)
{
System.out.println("Some IOException occured Indra");
}
}

void writeToFile(String s)
{
s = s + "\n"; // next line
try
{
dos.writeBytes(s); // writing line by liine
}
catch(IOException e)
{
System.out.println("Some IOException occured Indra");
}
}

void readFromFile()
{
try
{
fis = new FileInputStream(f);
dis = new DataInputStream(fis);

while (dis.available() > 0)
{
l = new String(dis.readLine()); //reading line by line
System.out.println(l);
}
fis.close(); //closing FileInputStream
dis.close(); //closing DataInputStream
}
catch(IOException e)
{
System.out.println("Some IOException occured Indra");
}
}

void closeFile()
{
try
{
fos.close(); // close the FileInputStream
dos.close(); //thank u for closing me
}
catch(IOException e)
{
System.out.println("Some IOException occured Indra");
}
}

public static void main(String args[])
{
Indra obj = new Indra();

for(int i=1; i < 20; ++i)
{
obj.writeToFile("Line " + i); // Indra it will write 10 lines
}

obj.closeFile();

obj.readFromFile();
}
}





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us