62,614
社区成员
发帖
与我相关
我的任务
分享
import java.awt.HeadlessException;
import java.io.*;
public class TestA {
public static void main(String[] s) {
File filename = new File("d:\\test.txt");
String filein = "\r\n--你好;";
RandomAccessFile mm = null;
try {
mm = new RandomAccessFile(filename, "rw");
new FileOutputStream(filename,false);
mm.write(filein.getBytes("UTF-8"));
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
} finally {
if (mm != null) {
try {
mm.close();
} catch (IOException e2) {
// TODO 自动生成 catch 块
e2.printStackTrace();
}
}
}
}
}
public final void writeUTF(String str)
throws IOException
Writes a string to the file using UTF-8 encoding in a machine-independent manner.
First, two bytes are written to the file, starting at the current file pointer, as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the UTF-8 encoding for each character.
修改后片断如下: String filein = "\r\n--你好;";
byte[]a=filein.getBytes();
RandomAccessFile mm = null;
try {
mm = new RandomAccessFile(filename, "rw");
new FileOutputStream(filename, false);
// mm.writeUTF(filein);
mm.write(a);
public class HuanHangLuanMa3 {
public static void main(String[] args) throws Exception {
byte[] b = new byte[]{(byte)0x00, (byte)0x0B};
byte b1 = b[0];
byte b2 = b[1];
String s = new String(b);
System.out.println(s);
System.out.println(Byte.valueOf(b1).intValue());
System.out.println(Byte.valueOf(b2).intValue());
System.out.println("\r\n--你好;".getBytes().length);
}
}
String filein = "\r\n--你好;";
filein = new String(filein.getBytes(), "gb2312");