DataOutputStream myDataStream; FileOutputStream myFileStream; BufferedOutputStream myBufferStream; //get a file handle mhyFileStream = new FileOutputStream("/usr/db/stock.dbf"); //chain a buffered output stream (for efficiency); myBufferStream = new BufferedOutputStream(myFileStream); //chain a data output file myDataStream = new DataOutputStream(myBufferStream); //Now we can use both input streams to access our file //(iiIf we want to ...) myBufferStream.write(b); myDataStream.writeInt(i); //close the data file explicitly //Always colse the "topmost" file stream myDataStream.close(); myBuffersStream.close(); myFileStream.close();
用BufferedWriter可以写入,为什么非要用DataOutputStream???
FileOutputStream f = new FileOutputStream("C:/abc.txt",true); DataOutputStream filed = new DataOutputStream(f); filed.writeUTF("这是我的第一句话!"); filed.close();这段代码就能执行。文件abc里就会有中文的“这是我的第一句话!”,不过前面会有两个你看不懂的垃圾字符(那两个好像是UTF-8字符),不用管它。
FileOutputStream myFileStream;
BufferedOutputStream myBufferStream; //get a file handle
mhyFileStream = new FileOutputStream("/usr/db/stock.dbf");
//chain a buffered output stream (for efficiency);
myBufferStream = new BufferedOutputStream(myFileStream);
//chain a data output file myDataStream = new DataOutputStream(myBufferStream); //Now we can use both input streams to access our file
//(iiIf we want to ...)
myBufferStream.write(b);
myDataStream.writeInt(i); //close the data file explicitly
//Always colse the "topmost" file stream
myDataStream.close();
myBuffersStream.close();
myFileStream.close();
DataOutputStream filed = new DataOutputStream(f);
filed.writeUTF("这是我的第一句话!");
filed.close();这段代码就能执行。文件abc里就会有中文的“这是我的第一句话!”,不过前面会有两个你看不懂的垃圾字符(那两个好像是UTF-8字符),不用管它。
文件中前两个字符显示为空格或垃圾字符。子是因为writeUTF()再UTF字符的前面加上两个字节,表示数据占用的字节数。
try {
if (strvalue == null) {
return null;
}
else {
strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");
return strvalue;
}
}
catch (Exception e) {
return null;
}
}
有人发的贴子中的这段代码可以读中文,但读不了 noratong(诺拉) 提供代码写的中文,
难道这也得配套才行吗??
设定对了就可以。如设定为UTF-8
FileInputStream file = new FileInputStream("C:/abc.txt");
DataInputStream in = new DataInputStream(file);
String strtxt = "";
while (strtxt != null)
{
strtxt = in.readUTF();
System.out.println(strtxt);
}
in.close();
老大,你自己试试看。。
readUTF();异常
try ()
{
byte[] bt = new byte[1024];
in.read(bt); .......................
}
JBuilder 下可以通过