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();
    
}
}

解决方案 »

  1.   

      windows的换行并不是直接的"n",是"\r\n"。所以out.write("\n")只能得到一个黑框,因为windos不认为这是个“换行”。直接从记事本输入的话,windows自动输入了"\r\n",所以从从文本文件中读出来的也是"\r\n",可以正常显示。      那么这是为什么呢?稍微学过正则表达式的朋友都知道:\r是回车符,而\n是换行符。Windows默认\n在文档中显示的是一个空格或者小黑框。所以,要先回车,再换行。
      

  2.   

    同上在java里输出可以用\n,在文件里要用\r\n
      

  3.   

    你看一下java doc就明白为啥了。writeUTF函数,会先往文件里面写两个byte,用来表示实际写出去的字节数
    例如写出去  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]
      

  4.   

    windows里换行时\r\n  linux中是\n
      

  5.   

    3楼正解!
    首先,你用readline来读,读进来的s里面是不含换行符的
    你又用writeUTF来写s入文件,本身又不写换行符
    所以这个问题,跟换行符根本没关系
    就是上面那个两个byte的问题
      

  6.   

    各位楼主好!那我怎样去解决那个空格和黑框或者去掉他们,使得文件Test2的内容连续的追加到文件Test1的末尾呢?
      

  7.   

    直接write就是了如果你要指定写成UTF-8的文件,那就用OutputStreamWriter,指定charset就行了
      

  8.   

    package csdn;import java.io.*; public class ReadText 

    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(); 
        

    }
      

  9.   

    你的问题我又试了下
    发现 Text1和Text2
    必须都是utf-8编码时才会出现lz说的情况!!
    如果是3#说的情况的话  不应该这样吧!!!