java删除指定大小文件_java中对文件的一些操作(删除,复制,计算大小..)和一些数学问题代码详解...

本文介绍了Java中对文件的操作,包括遍历文件路径、删除、复制以及计算文件大小等方法。同时,还提供了斐波那契数列和约瑟夫环问题的解决方案。对于文件操作,代码详细展示了如何根据路径获取文件、遍历文件夹、复制文件和删除文件夹。在数学问题部分,通过递归和非递归方式计算斐波那契数列,以及实现约瑟夫环问题的幸运数字算法。
摘要由CSDN通过智能技术生成

以下是我在空闲时间整理出来对文件操作的一些基本方法,涵盖增删改查  主要是根据文件路径来对文件进行操作,另外有一些解决数学问题的一些小方法,有兴趣的可以看看>>>>>>>>>

package com.dzkj.Action;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.math.BigInteger;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

import com.sun.org.apache.xml.internal.utils.SuballocatedByteVector;

public class FileTest {

public static void main(String[] args) {

FileTest f1 = new FileTest();

// File path = f1.getAllFileByPath();

// f1.getAllFileName(path);

File src = f1.getAllFileByPath();

// File destin=f1.getAllFileByPath();

// f1.copyFile2(src, destin);

System.out.println(f1.getFileLength(src));

}

/**

* 定义一个方法用来判断用户给定路径是否是系统文件夹路径 用来返回一个文件对象

*/

public File getAllFileByPath() {

Scanner scann = new Scanner(System.in);

System.out.println("请输入要遍历的文件系统路径");

String path = scann.next();

File file = new File(path);

// 该文件路径在系统中不存在时的情况给予提示

if (!file.exists()) {

System.out.println("您输入的路径不存在 请重新输入!");

getAllFileByPath();

}

// 该路径在系统下存在的情况

else {

// 判断文件是否是一个文件 如果给定的是一个路径是一个文件就提示是文件

if (file.isFile()) {

System.out.println("sorry!你给的路径是一个文件路径 请重新输入!!");

getAllFileByPath();

} else {

return file;

}

}

return null;

}

/**

* 定义一个方法用来遍历文件夹下的所有文件名称

*通俗来讲就是把指定一个文件夹下的文件按照层级进行遍历出来

*/

public void getAllFileName(File filePath) {

// 获得该文件夹下所有的文件 得到的是一个数组

File[] file = filePath.listFiles();

System.out.println(file.length);

// 根据得打的数组遍历文件

for (File sunFile : file) {

// 判断文件是不是文件夹 如果是一个文件夹就递归调用本方法

if (sunFile.isDirectory()) {

getAllFileName(sunFile);

}

// 在控制台打印出文件名称

System.out.println(sunFile.getName());

}

}

/**

* 实现给定两个系统路径 将 A路径下的所有文件复制到B路径中去

*

* @param src

* 源文件

* @param destin

* 需要复制的目标文件夹路径

*/

public void copyFile(File src, File destin) {

// 声明一个目标文件 路径是destin 文件名称是src.getName();

File destFile = new File(destin, src.getName());

// 判断声明的目标文件是否存在

if (!destFile.exists()) {

destFile.mkdir();

}

// 获得源文件的文件数组

File[] srcFile = src.listFiles();

// 遍历这个数组

for (File subFile : srcFile) {

// 判断目标文件是不是一个文件夹 如果不是一个文件夹就通过Io进行文件复制

if (subFile.isFile()) {

try {

BufferedInputStream bio = new BufferedInputStream(new FileInputStream(subFile));

BufferedOutputStream out = new BufferedOutputStream(

new FileOutputStream(new File(destFile, subFile.getName())));

// 下面进行文件复制Io操作循环

int b;

while ((b = bio.read()) != -1) {

out.write(b);

}

// 关闭流操作

bio.close();

out.close();

} catch (Exception e) {

e.printStackTrace();

}

}

// 如果file是一个文件夹就递归调用

else {

copyFile(subFile, destFile);

}

}

}

/**

* 删除该文件夹 1,返回值类型 void 2,参数列表File dir

**/

public static void deleteFile(File dir) {

// 1,获取该文件夹下的所有的文件和文件夹

File[] subFiles = dir.listFiles();

// 2,遍历数组

for (File subFile : subFiles) {

// 3,判断是文件直接删除

if (subFile.isFile()) {

subFile.delete();

// 4,如果是文件夹,递归调用

} else {

deleteFile(subFile);

}

}

// 5,循环结束后,把空文件夹删掉

dir.delete();

}

/**

* 实现文件复制的第二种方法 利用字节流进行复制 此方法效率比较慢

*/

public void copyFile2(File src, File destin) {

// 声明一个目标文件 路径是destin 文件名称是src.getName();

File destFile = new File(destin, src.getName());

// 判断声明的目标文件是否存在

if (!destFile.exists()) {

destFile.mkdir();

}

// 获得源文件的文件数组

File[] srcFile = src.listFiles();

// 遍历这个数组

for (File subFile : srcFile) {

// 判断目标文件是不是一个文件夹 如果不是一个文件夹就通过Io进行文件复制

if (subFile.isFile()) {

try {

FileOutputStream out = new FileOutputStream(destFile);

FileInputStream in = new FileInputStream(src);

int b;

while ((b = in.read()) != -1) {

out.write(b);

}

out.close();

in.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

// 如果file是一个文件夹就递归调用

else {

copyFile(subFile, destFile);

}

}

}

/**

* 不死神兔 故事得从西元1202年说起,话说有一位意大利青年,名叫斐波那契。

* 在他的一部著作中提出了一个有趣的问题:假设一对刚出生的小兔一个月后就能长成大兔,

* 再过一个月就能生下一对小兔,并且此后每个月都生一对小兔,一年内没有发生死亡, 问:一对刚出生的兔子,一年内繁殖成多少对兔子? 1 1 2 3 5

* 8 13 21 1 = fun(1) 1 = fun(2) 2 = fun(1) + fun(2) 3 = fun(2) + fun(3)

*/

public static void feiBolaQie1() {

// 用数组做不死神兔

int[] arr = new int[8];

// 数组中第一个元素和第二个元素都为1

arr[0] = 1;

arr[1] = 1;

// 遍历数组对其他元素赋值

for (int i = 2; i < arr.length; i++) {

arr[i] = arr[i - 2] + arr[i - 1];

}

// 如何获取最后一个数

System.out.println(arr[arr.length - 1]);

}

/**

* 利用递归求斐波拉契数

*/

public static int feiBoLaQie2(int num) {

if (num == 1 || num == 2) {

return 1;

} else {

return feiBoLaQie2(num - 2) + feiBoLaQie2(num - 1);

}

}

/**

* 统计指定文件夹大小 1,返回值类型long 2,参数列表File dir

**/

public static long getFileLength(File dir) {

// 1,定义一个求和变量

long len = 0;

// 2,获取该文件夹下所有的文件和文件夹listFiles();

File[] subFiles = dir.listFiles();

// 3,遍历数组

for (File subFile : subFiles) {

// 4,判断是文件就计算大小并累加

if (subFile.isFile()) {

len = len + subFile.length();

// 5,判断是文件夹,递归调用

} else {

len = len + getFileLength(subFile);

}

}

return len;

}

/**

* @param args

* 约瑟夫环 幸运数字

*/

public static int getLucklyNum(int num) {

ArrayList list = new ArrayList<>(); // 创建集合存储1到num的对象

for (int i = 1; i <= num; i++) {

list.add(i); // 将1到num存储在集合中

}

int count = 1;// 用来数数的,只要是3的倍数就杀人

for (int i = 0; list.size() != 1; i++) { // 只要集合中人数超过1,就要不断的杀

if (i == list.size()) {// 如果i增长到集合最大的索引+1时

i = 0;// 重新归零

}

if (count % 3 == 0) { // 如果是3的倍数

list.remove(i--); // 就杀人

}

count++;

}

return list.get(0);

}

/**

* @param args

* 需求:求出1000的阶乘尾部零的个数,用递归做

* 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100...1000 1000 / 5 = 200

* 5 * 55 * 5 * 2 5 * 5 * 35 * 5 * 45 * 5 * 55 * 5 * 6200 / 5 = 40

* 5 * 5 * 5 * 15 * 5 * 5 * 25 * 5 * 5 * 35 * 5 * 5 * 45 * 5 * 5 * 55 * 5 * 5 * 65 * 5 * 5 * 75 * 5 * 5 * 8

40 / 5 = 8

5 * 5 * 5 * 58 / 5 = 1

*/

public static int fun(int num) {

if(num > 0 && num < 5) {

return 0;

}else {

return num / 5 + fun(num / 5);

}

}

/**

* @param args

* 需求:求出1000的阶乘所有零和尾部零的个数,不用递归做

*/

//方法1-------------------------

public static void demo1() {//获取1000的阶乘尾部有多少个零

BigInteger bi1 = new BigInteger("1");

for(int i = 1; i <= 1000; i++) {

BigInteger bi2 = new BigInteger(i+"");

bi1 = bi1.multiply(bi2);//将bi1与bi2相乘的结果赋值给bi1

}

String str = bi1.toString();//获取字符串表现形式

StringBuilder sb = new StringBuilder(str);

str = sb.reverse().toString();//链式编程

int count = 0;//定义计数器

for(int i = 0; i < str.length(); i++) {

if('0' != str.charAt(i)) {

break;

}else {

count++;

}

}

System.out.println(count);

}

//方法2----------------------------》

public static void demo2() {//求1000的阶乘中所有的零

BigInteger bi1 = new BigInteger("1");

for(int i = 1; i <= 1000; i++) {

BigInteger bi2 = new BigInteger(i+"");

bi1 = bi1.multiply(bi2);//将bi1与bi2相乘的结果赋值给bi1

}

String str = bi1.toString();//获取字符串表现形式

int count = 0;

for(int i = 0; i < str.length(); i++) {

if('0' == str.charAt(i)) {//如果字符串中出现了0字符

count++;//计数器加1

}

}

System.out.println(count);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值