PDA

View Full Version : Why output is not 121212 in following java code but the output is |||?


dhaval2711
Apr 22, 2013, 10:33 PM
please help in following java code, I have to write 121212 ten times in poll.txt file but when I open poll.txt file it shows me |||||||||| output

import java.io.*;
class Byte{
public static void main(String args[]) throws IOException {
try{FileOutputStream f=new FileOutputStream("poll.txt");
for(int I=0;i<10;i++)
f.write(121212);
f.close();
}catch(Exception e)
{
}
}
}

rameshspry
Jul 4, 2013, 11:40 PM
To write the content into the file
we need to convert the content into the byte.

Like this...



import java.io.*;
class Byte
{
public static void main(String args[]) throws IOException
{
String content="121212";
try
{
FileOutputStream f=new FileOutputStream("poll.txt");
for(int I=0;i<10;i++)
{
byte[] contentInBytes = content.getBytes();
f.write(contentInBytes);

}
f.close();

}
catch(Exception e)
{
System.out.println(e.toString());
}
}
}