java读取文件(不断更新)

 
 
/**
* 文件重命名<br>
* @param fromPath 文件所在目录
* @param suffix 目标文件的后缀格式
*/
public static void reName(String fromPath,String suffix) {
File file = new File(fromPath);
// 判断是不是文件目录
if (file.isDirectory()) {
//获取目录下所有文件
File[] files = file.listFiles();
int number = 0;
//遍历目录下的文件
for (File fileFrom : files) {
number++;
String fromFileName = fileFrom.getName();
String toFileName = fromPath + File.separator + number + "."+suffix;
File toFile = new File(toFileName);
// 开始更名
fileFrom.renameTo(toFile);
System.out.println(fromFileName+"---成功重命名为---"+number + "."+suffix);
}
}
}
   
   
/**
* 获得单个文件的大小(KB)
* @param path 文件的路径
*/
public static float getSingleFileSize(String path) throws IOException {
FileInputStream fis = null;
File f = new File(path);
float size = 0;
try {
if (f.exists()) {
if (f.isDirectory()) {
File flist[] = f.listFiles();
for (int i = 0; i < flist.length; i++) {
size = size + getSingleFileSize(flist[i].getPath());
}
} else {
fis = new FileInputStream(f);
size = fis.available();
size = (float) (size / 1024);
}
} else {
f.createNewFile();
System.out.println("文件不存在");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
fis.close();
}
return size;
}
   
   
/**
* 遍历目录下所有文件的大小(KB)
* @param path 目录路径
*/
public static void getPathFileSize(String filePath) throws IOException {
File file = new File(filePath);
if (file.isDirectory()) {
File[] files = file.listFiles();// 获取此目录下的文件列表
for (File fileFrom : files) {
String fileName = fileFrom.getName();
String srcImageFile = fileFrom.getAbsolutePath();
float fileSize = getSingleFileSize(srcImageFile);
System.out.println(fileName + "----size=" + fileSize + "kb");
}
}
}
   
   
/**
* 复制单个文件(原名复制)
* @param oldPathFile 准备复制的文件源
* @param newPathFile 拷贝到新绝对路径带文件名(注:目录路径需带文件名)
* @return
*/
public static void CopySingleFileTo(String oldPathFile, String targetPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPathFile);
String targetfile = targetPath + File.separator + oldfile.getName();
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPathFile); //读入原文件
FileOutputStream fs = new FileOutputStream(targetfile);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wsxlgg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值