-
File input output
I have the following code which should read binary data from a file and write it to the console as character data. It is not working because the data is written as binary data, it is not converted to char as supposed to. Can anyone help me please?
Code:
package Tutorial;
/**
*
* @author Admin
*/
import java.io.*;
public class fileStreamTest{
public static void main(String args[]){
try{
byte bWrite [] = {11,21,3,40,5};
OutputStream os = new FileOutputStream("C:\\Temp\\test.txt");
for(int x=0; x < bWrite.length ; x++){
os.write( bWrite[x] ); // writes the bytes
}
os.close();
InputStream is = new FileInputStream("C:\\Temp\\test.txt");
int size = is.available();
for(int i=0; i< size; i++){
System.out.print((char)is.read()+ " ");
}
is.close();
}catch(IOException e){
System.out.print("Exception");
}
}
}
-
a freebee
The code is working perfectly.
You have to learn more about how to present data.
(which is probably what this assignment is about)
I hope you do not come to rely on this forum
to solve such simple problems for you.
Code:
/**
*
* @author Admin
*/
import java.io.*;
public class fileStreamTest{
public static void main(String args[]){
try{
byte bWrite [] = {11,21,3,40,5};
OutputStream os = new FileOutputStream("C:\\Documents and Settings\\Paul\\Desktop\\test.txt");
for(int x=0; x < bWrite.length ; x++){
os.write( bWrite[x] ); // writes the bytes
}
os.close();
InputStream is = new FileInputStream("C:\\Documents and Settings\\Paul\\Desktop\\test.txt");
int size = is.available();
for(int i=0; i< size; i++){
//System.out.print((char)is.read()+ " ");
System.out.print(Integer.toString(is.read())+" ");
}
is.close();
}catch(IOException e){
System.out.print("Exception");
}
}
}
And please use code tags next time you post a problem on the forum.
-
I have reconsidered my brief remarks on your
posting.
I see that this assignment offers an oppertunity
to explore many possibles solutions to a simple
problem.
The following is an alternate solution to the
problem you posted.
I am almost certain that there is a third possibility..
Code:
/**
*
* @author Admin
*/
import java.io.*;
public class fileStreamTest{
public static void main(String args[]){
try{
byte bWrite [] = {11,21,3,40,5};
OutputStream os = new FileOutputStream("C:\\Documents and Settings\\Paul\\Desktop\\test.txt");
for(int x=0; x < bWrite.length ; x++){
os.write( bWrite[x] ); // writes the bytes
}
os.close();
InputStream is = new FileInputStream("C:\\Documents and Settings\\Paul\\Desktop\\test.txt");
int size = is.available();
for(int i=0; i< size; i++){
//System.out.print((char)is.read()+ " ");
//System.out.print(Integer.toString(is.read())+" ");
System.out.print((is.read() & 0xf)+" ");
}
is.close();
}catch(IOException e){
System.out.print("Exception");
}
}
}
-
@OP, please use code tags when you are posting code segments in the forum. Unformated codes are really hard to read.
-
convert the binary to decimal format after reading, chars are essentially decimal numbers...
-
File io
Thank you everybody for your immediate help. Paul, excuse me for posting simple problems but I am trying to teach myself and have no other sources.
My difficulty is that I am not understanding why the progrum returns byte instead of character data.
Code:
System.out.print((char)is.read()+ " ");
the line above should cast the result of as a char but it is not working in my case. The commented line underneath works but is not the original solution.
Code:
//System.out.print(new String(is.read()+ " "));
I am running this code in NetBeans and the output i am getting is symbols, which I opened with a hex editor and found that it is the binary representation of the four numbers 11,21,3,40,5 which I included in the byte array.
Code:
byte bWrite [] = {11,21,3,40,5};
I would appreciate if someone will clarify this for me.
Thank you.