import java.io.File;
import java.io.RandomAccessFile;
/**
* 2016年8月31日下午7:00:37
*
* @author 3306 TODO 计算字节数
*
*/
public class FileUtil {
public static void main(String[] args) {
String filePath = "d:/test.txt";// d盘必须存在test.txt文件
readEachLine(filePath);
}
/**
* 打印文件每一行的字节数
*
* @param filePath
* 文件路径
*/
private static void readEachLine(String filePath) {
try {
File file = new File(filePath);
if (file.exists()) {// 文件存在
RandomAccessFile accessFile = new RandomAccessFile(file, "r");// 只赋予读的权限
String line = "";
long lineIndex = 1;
while (null != (line = accessFile.readLine())) {
System.out.println("line" + (lineIndex++) + ": " + line.getBytes().length);// 打印行号和字节数
}
accessFile.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
File file = new File("tcp.txt");
FileInputStream stream = new java.io.FileInputStream(file);
int pos = 10;//从第几个字节开始读
int len = 15;//读几个字节
stream.skip(pos); //跳过之前的字节数
byte[] b = new byte[len];
stream.read(b);
System.out.print(new String(b));
stream.close();
读取的每一行是string类型;string.length 就是字节长度了啊