mirror of
https://github.com/Dichgrem/Java.git
synced 2025-12-17 04:51:58 -05:00
update:demo5
This commit is contained in:
25
com/study/demo5/FileStat.java
Normal file
25
com/study/demo5/FileStat.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package com.study.demo5;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class FileStat {
|
||||
public long countBytes(String filePath) {
|
||||
long totalBytes = 0;
|
||||
|
||||
try (FileInputStream fis = new FileInputStream(filePath)) {
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
|
||||
while ((bytesRead = fis.read(buffer)) != -1) {
|
||||
totalBytes += bytesRead;
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("文件读取失败: " + e.getMessage());
|
||||
return -1;
|
||||
}
|
||||
|
||||
return totalBytes;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user