程序代码如下:
import java.io.*;
public class examFile7 {
public static void main(String[] args) {
String tempstr;
String tempstr1;
int fileline =0;
long pointerlast = 0 ;
try{
RandomAccessFile inobj = new RandomAccessFile("e:\\mydir\\secondfile.txt","rw");
while (inobj.readLine()!=null)
fileline++;
for(int i = 0 ; i<fileline;i++)
{inobj.seek(i);
tempstr=inobj.readLine();
System.out.println(tempstr);
}
pointerlast=inobj.getFilePointer();
System.out.println(pointerlast);
}
catch(IOException e){
}
}
}在e:\mydir\secondfile.txt 的文件中里边的内容是
youaregoodperson
youaregoodperson
youaregoodperson
youaregoodperson
youaregoodperson
youaregoodperson
youaregoodperson
youaregoodperson程序运行的结果是
youaregoodperson
ouaregoodperson
uaregoodperson
aregoodperson
regoodperson
egoodperson
goodperson
oodperson
18
我感到疑惑的问题是:为什么System.out.println(pointerlast);打印的结果是18,我查阅了api文档资料如下
public long getFilePointer()
throws IOException返回此文件中的当前偏移量。 返回:
到此文件开头的偏移量(以字节为单位),在该位置发生下一个读取或写入操作我以为在文件e:\mydir\secondfile.txt中,每行是youaregoodperson只有16个字符,为什么打印结果是18呢?这是什么原因呢?请大虾帮忙解决!!谢谢。
import java.io.*;
public class examFile7 {
public static void main(String[] args) {
String tempstr;
String tempstr1;
int fileline =0;
long pointerlast = 0 ;
try{
RandomAccessFile inobj = new RandomAccessFile("e:\\mydir\\secondfile.txt","rw");
while (inobj.readLine()!=null)
fileline++;
for(int i = 0 ; i<fileline;i++)
{inobj.seek(i);
tempstr=inobj.readLine();
System.out.println(tempstr);
}
pointerlast=inobj.getFilePointer();
System.out.println(pointerlast);
}
catch(IOException e){
}
}
}在e:\mydir\secondfile.txt 的文件中里边的内容是
youaregoodperson
youaregoodperson
youaregoodperson
youaregoodperson
youaregoodperson
youaregoodperson
youaregoodperson
youaregoodperson程序运行的结果是
youaregoodperson
ouaregoodperson
uaregoodperson
aregoodperson
regoodperson
egoodperson
goodperson
oodperson
18
我感到疑惑的问题是:为什么System.out.println(pointerlast);打印的结果是18,我查阅了api文档资料如下
public long getFilePointer()
throws IOException返回此文件中的当前偏移量。 返回:
到此文件开头的偏移量(以字节为单位),在该位置发生下一个读取或写入操作我以为在文件e:\mydir\secondfile.txt中,每行是youaregoodperson只有16个字符,为什么打印结果是18呢?这是什么原因呢?请大虾帮忙解决!!谢谢。
解决方案 »
- JSplitPane设置成纵向之后,如何固定下面的大小?
- JTable中一行有多个JCheckBox设置只能选一个 请各位大哥帮帮忙 ....
- 将字符串转为日期时间类型,要精确到毫秒
- '2009-03-18T10:15:53.086Z' 谁知道这个时间格式
- 急!急!!关于写文件控制文件大小!谢谢!!急!急!!
- 类似于这种网站是怎么做出来的,有没有源程序下载。能不能告诉我,谢谢!!!
- 如何实现文件的拷贝与剪切?
- 大家好,我初次来到JAVA版,想结识一些用JAVA的朋友
- 关于点对点连接问题!
- 如何在点击某个按钮后往JPanel上再添加一个panel?
- 命令行启动eclipse
- 求解一道java程序题,估计不是很难。。。不过对我就。。。。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/package src;/**
*
* @author Administrator
*/
import java.io.*;
public class examFile7 {
public static void main(String[] args) {
String tempstr;
String tempstr1;
int fileline =0;
long pointerlast = 0 ;
try{
RandomAccessFile inobj = new RandomAccessFile("e:\\secondfile.txt","rw");
while (inobj.readLine()!=null)
fileline++;
for(int i = 0 ; i <fileline;i++)
{
if(i==0)
inobj.seek(i);
else
inobj.seek(i*19);
tempstr=inobj.readLine();
System.out.println(tempstr);
}
pointerlast=inobj.getFilePointer();
System.out.println(pointerlast);
}
catch(IOException e){
}
}
}
每行结束时都有个\n换行符
同时你的偏移量不对