FileOutputStream out; // declare a file output object
PrintStream p;
try {
out = new FileOutputStream(_strFilePathAndName);
p = new PrintStream(out);
p.println (_strLog+"\n");
p.close();
}
catch (FileNotFoundException ex) {
}
到是能把汉字写进去,但不能追加
用RandomAccessFile的writeBytes到可以追加,可是写汉字就不能了,全是乱码
哪位大哥还有别的办法。
用FileWriter(path,true(还是false忘记了,写一下试试))
不行呀
writeBytes写进去的汉字读出来不处理肯定是乱码啊
先看下编码的基础啊
String strFile="1.txt";
File f=new File(fDir,strFile);
RandomAccessFile bw=new RandomAccessFile(strFile,"rw");
bw.seek(bw.length());//将指针移动到文件末尾
String strGBK="中国";
bw.writeBytes(strGBK);
bw.close();以上代码写进文本的是乱码,那位帮改一下,谢了.
这也是写字节呀,
上面我用了,写不了,不能写进呀
public class Test{
public static void main(String[] args){
try {
File fDir=new File(File.separator);
String strFile="1.txt";
File f=new File(fDir,strFile);
RandomAccessFile bw=new RandomAccessFile(strFile,"rw");
bw.seek(bw.length());//将指针移动到文件末尾
String strGBK="中国";
bw.write(strGBK.getBytes()) ;
bw.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}这样写就没有问题了。
throws IOException按字节序列将该字符串写入该文件。该字符串中的每个字符均按顺序写出,并丢弃其高八位。写入从文件指针的当前位置开始。 指定者:
接口 DataOutput 中的 writeBytes
参数:
s - 要写入的字节的字符串。
抛出:
IOException - 如果发生 I/O 错误。丢弃了高8位,所以就写汉字就不对了。
File f =null;
RandomAccessFile bw = null;
try
{
f=new File("./"+strFile);
bw=new RandomAccessFile(f,"rw");
bw.seek(bw.length());//将指针移动到文件末尾
String strGBK="中国";
byte [] strBytes = strGBK.getBytes("GB2312");
bw.write(strBytes);
}
catch(IOException es)
{
System.out.println("IO读写异常!");
}
finally
{
try
{
bw.close();
}
catch (IOException e) {
System.out.println("资源回收失败!");
}
}楼主,编码规范些:)