|
|
| help please |
Expert:allo
i wrote this program
but the function newLine dosnt work with me and i dont know where is exactly the error this is the code i wrote
import java.io.*; import java.util.*; import java.lang.Integer; import java.io.PrintWriter; import java.io.OutputStreamWriter; import java.io.FileWriter; import java.io.Writer; import java.io.BufferedWriter; import java.io.IOException;
public class DataToBinary{
public static void main(String[] args) throws IOException{
System.out.println("Data types conversion example!"); int num = 100; Writer output = null;
File file = new File("numbers.txt"); output = new BufferedWriter(new FileWriter(file));
for(int i =0 ; i<num; i++){
System.out.println("Integer: " + i); String byt = Integer.toBinaryString(i); String s = "0";
while(byt.length()<8){ byt= s+ byt;} System.out.println(" Byte: " +byt);
output.write(byt); output.newLine();
}
output.close(); System.out.println("Your file has been written");
} }
|
| Answers |
Hi friend,
We check the code having some changes to correct the code :
Changes in this lines code :
Writer output = null;
to
BufferedWriter output = null;
Correct Code :
import java.io.*; import java.util.*; import java.lang.Integer; import java.io.PrintWriter; import java.io.OutputStreamWriter; import java.io.FileWriter; import java.io.Writer; import java.io.BufferedWriter; import java.io.IOException;
public class DataToBinary{
public static void main(String[] args) throws IOException{
System.out.println("Data types conversion example!"); int num = 100; BufferedWriter output = null;
File file = new File("numbers.txt"); output = new BufferedWriter(new FileWriter(file));
for(int i =0 ; i<num; i++){
System.out.println("Integer: " + i); String byt = Integer.toBinaryString(i); String s = "0";
while(byt.length()<8){ byt= s+ byt;} System.out.println(" Byte: " +byt);
output.write(byt); output.newLine( ) ;
}
output.close(); System.out.println("Your file has been written");
} }
For more information on Java visit to :
http://www.roseindia.net/java
Thanks
|
thanx alot it works
|
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|