Comments (1)
@slysnight 确实存在不同JDK有差异……被JDK坑了。
5.8.28这块重新自己实现,参考:https://stackoverflow.com/questions/453018/number-of-lines-in-a-file-in-java
/**
* 计算文件的总行数<br>
* 参考:https://stackoverflow.com/questions/453018/number-of-lines-in-a-file-in-java
*
* @param file 文件
* @param bufferSize 缓存大小,小于1则使用默认的1024
* @return 该文件总行数
* @since 5.8.28
*/
public static int getTotalLines(File file, int bufferSize) {
if (false == isFile(file)) {
throw new IORuntimeException("Input must be a File");
}
if (bufferSize < 1) {
bufferSize = 1024;
}
try (InputStream is = getInputStream(file)) {
byte[] c = new byte[bufferSize];
int readChars = is.read(c);
if (readChars == -1) {
// 空文件,返回0
return 0;
}
// 起始行为1
// 如果只有一行,无换行符,则读取结束后返回1
// 如果多行,最后一行无换行符,最后一行需要单独计数
// 如果多行,最后一行有换行符,则空行算作一行
int count = 1;
while (readChars == bufferSize) {
for (int i = 0; i < bufferSize; i++) {
if (c[i] == CharUtil.LF) {
++count;
}
}
readChars = is.read(c);
}
// count remaining characters
while (readChars != -1) {
for (int i = 0; i < readChars; i++) {
if (c[i] == CharUtil.LF) {
++count;
}
}
readChars = is.read(c);
}
return count;
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
from hutool.
Related Issues (20)
- 图片添加文字水印可以支持45度倾斜多行重复文字的效果么 HOT 1
- cn.hutool.http.HttpUtil#post(java.lang.String, java.lang.String) 这个方法按文档的标准参数调用报异常 HOT 5
- HttpRequest 设置body的时候能否修改isMultiPart的值。 HOT 4
- 需求请求:LocalDateTimeUtil.endOfDay重载一个接收LocalDate的方法 HOT 3
- get请求https时会出现ssl证书问题 HOT 3
- CVE-2023-51075 7.5 Loop with Unreachable Exit Condition ("Infinite Loop") vulnerability with High severity found HOT 1
- 贵团队有支持JPMS的计划吗? HOT 1
- NumberUtil.pow(3,-2) 指数不能为负数 HOT 3
- 能够提供Neo4jUtil HOT 2
- JDK17环境下,JarClassLoader.loadJar()方法异常
- 【question】请问一下hutool有直接读取并返回X509Certificate的方法吗? HOT 2
- ListUtil 需要一个【元素移动】的方法 HOT 1
- 请教下在HttpFilter里面有办法重写响应体吗? HOT 3
- Should avoid NPE when `coll1` is null HOT 1
- DateUtil.parse()无法解析日期: HOT 2
- [程序崩溃]JDK11 + HuTool5.7.5 偶发程序崩溃问题 HOT 1
- 希望给 ImgUtil.convert 图片类型转换添加 webp 转换支持 HOT 2
- 希望加个银行卡校验算法Luhn HOT 2
- 文档注释有误 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hutool.