mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
FileUtil.getTotalLines()支持CR换行符
This commit is contained in:
parent
63b2f7c70d
commit
c459a0f613
@ -2,9 +2,10 @@
|
|||||||
# 🚀Changelog
|
# 🚀Changelog
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.32(2024-08-24)
|
# 5.8.32(2024-08-29)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
|
* 【core 】 FileUtil.getTotalLines()支持CR换行符(issue#IAMZYR@Gitee)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【http 】 修复getFileNameFromDisposition不符合规范问题(issue#IAKBPD@Gitee)
|
* 【http 】 修复getFileNameFromDisposition不符合规范问题(issue#IAKBPD@Gitee)
|
||||||
|
@ -572,8 +572,8 @@ public class FileUtil extends PathUtil {
|
|||||||
bufferSize = 1024;
|
bufferSize = 1024;
|
||||||
}
|
}
|
||||||
try (InputStream is = getInputStream(file)) {
|
try (InputStream is = getInputStream(file)) {
|
||||||
byte[] c = new byte[bufferSize];
|
byte[] chars = new byte[bufferSize];
|
||||||
int readChars = is.read(c);
|
int readChars = is.read(chars);
|
||||||
if (readChars == -1) {
|
if (readChars == -1) {
|
||||||
// 空文件,返回0
|
// 空文件,返回0
|
||||||
return 0;
|
return 0;
|
||||||
@ -584,23 +584,35 @@ public class FileUtil extends PathUtil {
|
|||||||
// 如果多行,最后一行无换行符,最后一行需要单独计数
|
// 如果多行,最后一行无换行符,最后一行需要单独计数
|
||||||
// 如果多行,最后一行有换行符,则空行算作一行
|
// 如果多行,最后一行有换行符,则空行算作一行
|
||||||
int count = 1;
|
int count = 1;
|
||||||
|
byte pre;
|
||||||
|
byte c = 0;
|
||||||
while (readChars == bufferSize) {
|
while (readChars == bufferSize) {
|
||||||
for (int i = 0; i < bufferSize; i++) {
|
for (int i = 0; i < bufferSize; i++) {
|
||||||
if (c[i] == CharUtil.LF) {
|
pre = c;
|
||||||
|
c = chars[i];
|
||||||
|
// 换行符兼容MAC
|
||||||
|
if (c == CharUtil.LF || pre == CharUtil.CR) {
|
||||||
++count;
|
++count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
readChars = is.read(c);
|
readChars = is.read(chars);
|
||||||
}
|
}
|
||||||
|
|
||||||
// count remaining characters
|
// count remaining characters
|
||||||
while (readChars != -1) {
|
while (readChars != -1) {
|
||||||
for (int i = 0; i < readChars; i++) {
|
for (int i = 0; i < readChars; i++) {
|
||||||
if (c[i] == CharUtil.LF) {
|
pre = c;
|
||||||
|
c = chars[i];
|
||||||
|
if (c == CharUtil.LF || pre == CharUtil.CR) {
|
||||||
++count;
|
++count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
readChars = is.read(c);
|
readChars = is.read(chars);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 最后一个字符为换行符,则单独计数行
|
||||||
|
if(c == CharUtil.CR){
|
||||||
|
++count;
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
|
@ -532,6 +532,20 @@ public class FileUtilTest {
|
|||||||
assertEquals(8, totalLines);
|
assertEquals(8, totalLines);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getTotalLinesCrTest() {
|
||||||
|
// 此文件最后一行有换行符,则最后的空行算作一行
|
||||||
|
final int totalLines = FileUtil.getTotalLines(FileUtil.file("test_lines_cr.csv"));
|
||||||
|
assertEquals(8, totalLines);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getTotalLinesCrlfTest() {
|
||||||
|
// 此文件最后一行有换行符,则最后的空行算作一行
|
||||||
|
final int totalLines = FileUtil.getTotalLines(FileUtil.file("test_lines_crlf.csv"));
|
||||||
|
assertEquals(8, totalLines);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void issue3591Test() {
|
public void issue3591Test() {
|
||||||
// 此文件最后一行末尾无换行符
|
// 此文件最后一行末尾无换行符
|
||||||
|
1
hutool-core/src/test/resources/test_lines_cr.csv
Normal file
1
hutool-core/src/test/resources/test_lines_cr.csv
Normal file
@ -0,0 +1 @@
|
|||||||
|
# 这是一行注释,读取时应忽略
a,b,c,d
1,2,3,4
# 这是一行注释,读取时应忽略
q,w,e,r,"我是一段
带换行的内容"
a,s,d,f
|
Can't render this file because it contains an unexpected character in line 1 and column 141.
|
7
hutool-core/src/test/resources/test_lines_crlf.csv
Normal file
7
hutool-core/src/test/resources/test_lines_crlf.csv
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# 这是一行注释,读取时应忽略
|
||||||
|
a,b,c,d
|
||||||
|
1,2,3,4
|
||||||
|
# 这是一行注释,读取时应忽略
|
||||||
|
q,w,e,r,"我是一段
|
||||||
|
带换行的内容"
|
||||||
|
a,s,d,f
|
Can't render this file because it has a wrong number of fields in line 2.
|
Loading…
x
Reference in New Issue
Block a user