前几天做用java导入execl数据,程序是去年一个新员工写的,当时测试数据是可以导入的,再多也没研究里面的方法合理不、能否优化都没有再考虑这些。
导入的文件比较多,我就想给你加个把成功导入的execl文件转移到另外一个文件夹下,转移是没有问题,可是删除就出了问题,看删除方法是没有问题,就开始看他写的代码,发现读取execl文件的流没有关闭,导致文件无法。
我就在try catch finally的finally中把这个流关闭,然后在进行我文件复制和删除。
我的复制文件方法和删除文件方法如下
导入的文件比较多,我就想给你加个把成功导入的execl文件转移到另外一个文件夹下,转移是没有问题,可是删除就出了问题,看删除方法是没有问题,就开始看他写的代码,发现读取execl文件的流没有关闭,导致文件无法。
我就在try catch finally的finally中把这个流关闭,然后在进行我文件复制和删除。
我的复制文件方法和删除文件方法如下
1
public
static
boolean
copyFile(String oldPath, String newPath)
{
2
try
{
3
int bytesum = 0;
4
int byteread = 0;
5
File oldfile = new File(oldPath);
6
if (oldfile.exists())
{ // 文件存在时
7
InputStream inStream = new FileInputStream(oldPath); // 读入原文件
8
FileOutputStream fs = new FileOutputStream(newPath);
9
byte[] buffer = new byte[1444];
10
int length;
11
while ((byteread = inStream.read(buffer)) != -1)
{
12
bytesum += byteread; // 字节数 文件大小
13
// System.out.println(bytesum);
14
fs.write(buffer, 0, byteread);
15
}
16
inStream.close();
17
fs.close();
18
}
19
return true;
20
} catch (Exception e)
{
21
// System.out.println("复制单个文件操作出错");
22
e.printStackTrace();
23
return false;
24
}
25
}
26
27
/** */
/**
28
* 删除文件或者文件夹,对于文件夹遍历其子文件夹进行递归删除
29
*
30
* @param f -
31
* File对象
32
* @return 删除是否成功
33
*/
34
public
static
boolean
deleteFile(File f)
{
35
if (f.exists())
{
36
if (f.isFile())
37
return f.delete();
38
else if (f.isDirectory())
{
39
File[] files = f.listFiles();
40
for (int i = 0; i < files.length; i++)
{
41
if (!deleteFile(files[i]))
42
return false;
43
}
44
return f.delete();
45
} else
46
return false;
47
} else
48
return false;
49
}



2



3

4

5

6



7

8

9

10

11



12

13

14

15

16

17

18

19

20



21

22

23

24

25

26

27


28

29

30

31

32

33

34



35



36

37

38



39

40



41

42

43

44

45

46

47

48

49
