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();
}
}
}
温馨提示:答案为网友推荐,仅供参考