📄 dealcomment.java
字号:
package dealcomment;
/*
*do by jjl2009 3.6
*
**/
import java.io.*;
import java.util.*;
import active.*;
class dealComment {
// File cHead;
File aCmmt;
/// RandomAccessFile rafH;
RandomAccessFile rafC;
public static final int READ=0;
public static final int WRITE=1;
int flag;
String cmtUrl;
Vector vCmtBuf=new Vector();
int size=0;
public dealComment(String fileCmt,int flag){
this.flag=flag;
cmtUrl=fileCmt;
try{
// cHead=new File(fileCmt+".chd");
aCmmt=new File(fileCmt);
if(flag==WRITE){
/* if(!cHead.exists()){
System.out.println("活动评论的头文件不存在!新创建中.....");
cHead.createNewFile();
}*/
if(!aCmmt.exists()){
System.out.println("活动评论不存在创建中.....");
aCmmt.createNewFile();
}
}else{
/* if(!cHead.exists()){
System.out.println("活动评论的头文件不存在!读取失败");
return;
}*/
if(!aCmmt.exists()){
System.out.println("活动评论不存在读取失败!");
return;
}
}
// rafH=new RandomAccessFile(cHead,"rw");
rafC=new RandomAccessFile(aCmmt,"rw");
rafC.seek(0);
rafC.writeLong(16);
rafC.writeLong(16);
}catch(Exception e){
System.out.println(e);
System.out.println("初始化失败!");
}
}
public void setComment(Comment comment){
try{
if(rafC==null){
rafC=new RandomAccessFile(aCmmt,"rw");
}
System.out.println("...........尝试添加评论在活评论文件"+cmtUrl+"...........");
rafC.seek(8);
Long hp=rafC.readLong();
rafC.seek(hp);
rafC.writeUTF(comment.getActiveName());
rafC.writeUTF(comment.getPerson());
rafC.writeUTF(comment.getTime());
rafC.writeUTF(comment.getContent());
Long p=rafC.getFilePointer();
rafC.seek(8);
rafC.writeLong(p);
}catch(Exception e){
System.out.println("添加活动评论出现故障!");
}
}
public void close(){
if(rafC!=null){
try{
rafC.close();
rafC.seek(0);
rafC.writeLong(16);
}catch(Exception e){
System.out.println("关闭文件时出错了!");
}
}
}
public Comment getNextComment(){
if(rafC==null)return null;
Comment comment=new Comment();
try{
rafC.seek(0);
Long p1=rafC.readLong();
// if(p1==rafC.readLong())return null;
System.out.println("正在读取位置为:"+p1+"的内容");
rafC.seek(p1);
comment.setActiveName(rafC.readUTF());
comment.setPerson(rafC.readUTF());
comment.setTime(rafC.readUTF());
comment.setContent(rafC.readUTF());
Long p=rafC.getFilePointer();
rafC.seek(0);
rafC.writeLong(p);
return comment;
}catch(Exception e){
System.out.println("获取下一评论时失败!");
return null;
}
}
public static void main(String args[]){
dealComment dealC=new dealComment("name.cmt",dealComment.WRITE);
Comment comment=new Comment();
comment.setActiveName("jjl");
comment.setTime("20092");
comment.setPerson("ff");
comment.setContent("fff");
dealC.setComment(comment);
comment.setActiveName("jjl11");
comment.setTime("20092");
comment.setPerson("ff");
comment.setContent("fff");
dealC.setComment(comment);
dealComment dealCm=new dealComment("name.cmt",dealComment.READ);
comment=dealCm.getNextComment();
if(comment!=null)
System.out.println(comment.getActiveName());
comment=dealCm.getNextComment();
if(comment!=null)
System.out.println(comment.getActiveName());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -