从具有意外输出的文件中读取
Reading from a file with unexpected output
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
class Myclass
{
public static void main(String args[])
{
try{
//writing data into file from byte stream i.e FileOutputStream and reading from DataInputStream
int data[]={99,76,65,55,99};
FileOutputStream fout=new FileOutputStream("C:\Users\dell\Desktop\testingfile.txt");
for(int i:data)
{
fout.write(i);
}
Myclass m=new Myclass();
FileInputStream fin=new FileInputStream("C:\Users\dell\Desktop\testingfile.txt");
m.readMethod(fin);
}catch(Exception e)
{
System.out.println("Exception caught");
}
}
void readMethod(InputStream obj)
{
try{
DataInputStream din=new DataInputStream(obj);//noe datainputstream has only one constructor with parameter of Inputstream
int d;
while((d=din.readInt())!=-1)
{
System.out.println(d);
}
}catch(Exception E){}
}
I am writing set of integers to the file testingfile.txt with the write(int) method available in FileOutPutStream.I have created a method readMethod which reads Integer from file with method available in DataInputStream.But reading from an file gives me different output.It differs from data i have written.Why is my Output different?Can anyone correct my code or tell me why is that so?
Output is :1665941815.
void write(int b)
Writes the specified byte to this file output stream.
在JavaSE文档(http://docs.oracle.com/javase/8/docs/api/java/io/FileOutputStream.html)中,write方法用于写入一个byte,而不是int,即使它接收一个int值作为参数。
也许您需要改用 DataOutputStream。
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
class Myclass
{
public static void main(String args[])
{
try{
//writing data into file from byte stream i.e FileOutputStream and reading from DataInputStream
int data[]={99,76,65,55,99};
FileOutputStream fout=new FileOutputStream("C:\Users\dell\Desktop\testingfile.txt");
for(int i:data)
{
fout.write(i);
}
Myclass m=new Myclass();
FileInputStream fin=new FileInputStream("C:\Users\dell\Desktop\testingfile.txt");
m.readMethod(fin);
}catch(Exception e)
{
System.out.println("Exception caught");
}
}
void readMethod(InputStream obj)
{
try{
DataInputStream din=new DataInputStream(obj);//noe datainputstream has only one constructor with parameter of Inputstream
int d;
while((d=din.readInt())!=-1)
{
System.out.println(d);
}
}catch(Exception E){}
}
I am writing set of integers to the file testingfile.txt with the write(int) method available in FileOutPutStream.I have created a method readMethod which reads Integer from file with method available in DataInputStream.But reading from an file gives me different output.It differs from data i have written.Why is my Output different?Can anyone correct my code or tell me why is that so?
Output is :1665941815.
void write(int b)
Writes the specified byte to this file output stream.
在JavaSE文档(http://docs.oracle.com/javase/8/docs/api/java/io/FileOutputStream.html)中,write方法用于写入一个byte,而不是int,即使它接收一个int值作为参数。
也许您需要改用 DataOutputStream。