请问高手:这样的文件操作如何实现? 試著尋找最後一個"\r\n"或0d0a(非文件尾部),下一個位置就是文件的最後一行了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 RandomAccessFile asdf=new RandomAccessFile("d:\\test.zip","rw"); byte b[]=new byte[10]; long ll_length,index=1; ll_length=asdf.length()-10;asdf.seek(ll_length-1000);RandomAccessFile可以把定位文件的任何位置, to signboy(横):你怎么知道最后一行的byte数. BufferedReader in = new BufferedReader(new FileReader(file));String s;s = in.readLine();Vector fileBuffer = new Vector();while( null != s){ fileBuffer.addElement(s); s = in.readLine();}这样,通过fileBuffer的size()可以找到该文件一共有多少行,不管你是想直接在这个文件上进行操作,或者是把最后一行去掉然后生成一个新文件都可以,这种东西只要自己用不同的方法试试不就可以了吗? 还是用RandomAccessFile 打开文件,然后从后往前查找行结束符,取出结束符之后的就行了.(如果最后两个字节为行结束符,就找前面一个了) 如果是个文本文件的话 要定位的话我到是有一个笨办法 当然还是要读完整个文件//准备File fd=new File(filepath);RandomAccessFile raf=new RandomAccessFile(fd,"rw");String temp=new String();int point=0;//准备工作做完以后,要用到RandomAccessFile类中的三个方法(或者4个)//readLine() seek() length() getfilepointer()while(raf.getfilepointer()<raf.length()){ point=raf.getfilepointer(); //这里就是获得了当前随 //机读取文件的指针 temp=raf.readLine(); //读一行,同时指针下移一行 } raf.seek(point); //这个时候指针已经指向了最后一行 //用seek定位就可以了 然后就可以任意进行你想要对这一行的读写操作了 java.io.RandomAccessFile in=new java.io.RandomAccessFile("c:\\kkk.txt","r"); String str=null; int row=0; while( (str=in.readLine())!=null ){ row++; } while (row-- >= 0 ) {in.readLine();} //the file pointer pointes to the last line. javamail发邮件正文的中文乱码 关于java开发OPC的客户端接口 String类型的定义几个对象 outputStream.write()抛出异常 关于数据库连接的问题。 repaint() 函数怎么老是不响应呢?(急!!!!) 怎么对另一个文件的对象进行操作。 国内有没有网站用过Jive论坛呵? Java 和 Access数据库连接问题。谢谢!! 如何正确安装java plug-in? !!!!!高分急问!用Java编写FTP Client&Server的问题!!!!! 一旦小代码!送分!
byte b[]=new byte[10];
long ll_length,index=1;
ll_length=asdf.length()-10;
asdf.seek(ll_length-1000);
RandomAccessFile可以把定位文件的任何位置,
你怎么知道最后一行的byte数.
String s;
s = in.readLine();
Vector fileBuffer = new Vector();
while( null != s)
{
fileBuffer.addElement(s);
s = in.readLine();
}
这样,通过fileBuffer的size()可以找到该文件一共有多少行,不管你是想直接在这个文件上进行操作,或者是把最后一行去掉然后生成一个新文件都可以,这种东西只要自己用不同的方法试试不就可以了吗?
我到是有一个笨办法 当然还是要读完整个文件
//准备
File fd=new File(filepath);
RandomAccessFile raf=new RandomAccessFile(fd,"rw");
String temp=new String();
int point=0;
//准备工作做完以后,要用到RandomAccessFile类中的三个方法(或者4个)
//readLine() seek() length() getfilepointer()
while(raf.getfilepointer()<raf.length())
{
point=raf.getfilepointer(); //这里就是获得了当前随
//机读取文件的指针
temp=raf.readLine(); //读一行,同时指针下移一行
}
raf.seek(point); //这个时候指针已经指向了最后一行
//用seek定位就可以了
然后就可以任意进行你想要对这一行的读写操作了
String str=null;
int row=0;
while( (str=in.readLine())!=null ){
row++;
}
while (row-- >= 0 ) {in.readLine();}
//the file pointer pointes to the last line.