⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 appendfile.java

📁 实例精华
💻 JAVA
字号:

import java.io.*;
public class AppendFile
{
  public static void main(String[] args) {

    String toAppend = args[0];

    try{

      int i = 0;

      String record = new String();

      String toCn = null;
      //处理中文问题
      toCn = new String(toAppend.getBytes("GBK"),"ISO8859_1");

      RandomAccessFile rf1
		= new RandomAccessFile("e:\\toAppend.txt","rw");
      //length()方法得到的字符长度,包括每一行中的回车符,回车符为一个字节
      rf1.seek(rf1.length());
      //此处为了显示一下读取到的文件的长度,用字节来表示。一个汉字为2个字节
      System.out.println(rf1.length());
      rf1.writeBytes(toCn+"\n");
      rf1.close();

      RandomAccessFile rf2 = new RandomAccessFile("e:\\toAppend.txt","r");
      String outCn = null;
      while ((record = rf2.readLine()) != null)
      {
        i ++;
        //处理中文问题
        outCn = new String(record.getBytes("ISO8859_1"),"GBK");

        System.out.println("Line "+i+":"+outCn);
      }
      rf2.close();
   }catch(Exception e)
   {
    e.printStackTrace();
   }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -