import java.io.*;public class task_99_2 {
public static void main(String args[]) throws IOException{
String s;
RandomAccessFile rf=new RandomAccessFile("e:/Test1.txt","rw");
RandomAccessFile raf=new RandomAccessFile("e:/Test2.txt","rw");
rf.seek(rf.length());
raf.seek(0);
while((s=raf.readLine())!=null){
rf.writeUTF(s);
System.out.println(s); }
rf.close();
raf.close();
}
}
public static void main(String args[]) throws IOException{
String s;
RandomAccessFile rf=new RandomAccessFile("e:/Test1.txt","rw");
RandomAccessFile raf=new RandomAccessFile("e:/Test2.txt","rw");
rf.seek(rf.length());
raf.seek(0);
while((s=raf.readLine())!=null){
rf.writeUTF(s);
System.out.println(s); }
rf.close();
raf.close();
}
}
例如写出去 9个, 就是 [0, 9]
写出去 300个, 就是 [1, 44]
就因为有了这两个byte,所以才有了你看到的方块啊,空白啊之类的PS:writeUTF并不输出回车换行之类的[code=HTML]writeUTF
public final void writeUTF(String str)
throws IOExceptionWrites a string to the file using modified 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 modified UTF-8 encoding for each character.
Specified by:
writeUTF in interface DataOutput
Parameters:
str - a string to be written.
Throws:
IOException - if an I/O error occurs.[code]
首先,你用readline来读,读进来的s里面是不含换行符的
你又用writeUTF来写s入文件,本身又不写换行符
所以这个问题,跟换行符根本没关系
就是上面那个两个byte的问题
{
public static void main(String args[]) throws IOException
{ String s;
//RandomAccessFile rf=new RandomAccessFile("Test2.txt","rw");
//RandomAccessFile raf=new RandomAccessFile("Test1.txt","rw");
BufferedReader raf=new BufferedReader(new FileReader("Test1.txt"));
OutputStreamWriter rf=new OutputStreamWriter(new FileOutputStream("Test2.txt"),"gb2312");
//rf.seek(rf.length());
//raf.seek(0);
while((s=raf.readLine())!=null)
{
//s=new String (s.getBytes("UTF-8"),"UTF-8");
//rf.writeChars(s);
rf.write(s);
System.out.println(s); }
rf.close();
raf.close();
}
}
发现 Text1和Text2
必须都是utf-8编码时才会出现lz说的情况!!
如果是3#说的情况的话 不应该这样吧!!!