Java to read and delete files in folders

Source: Internet
Author: User

Java to read and delete files in folders

This article will share with you how to read and delete files in folders in java. File. delete () is used to delete "a File or an empty directory "! Therefore, to delete a directory and all its files and subdirectories, and to perform recursive deletion, you can refer to it as needed.

Java to read and delete files in folders

?

1

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

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

Package test.com;

 

Import java. io. File;

Import java. io. FileNotFoundException;

Import java. io. IOException;

 

Public class ReadFile {

Public ReadFile (){

}

/**

* Read all files in a folder

*/

Public static boolean readfile (String filepath) throws FileNotFoundException, IOException {

Try {

 

File file = new File (filepath );

If (! File. isDirectory ()){

// System. out. println ("file ");

// System. out. println ("path =" + file. getPath ());

// System. out. println ("absolutepath =" + file. getAbsolutePath ());

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

 

} Else if (file. isDirectory ()){

String [] filelist = file. list ();

For (int I = 0; I <filelist. length; I ++ ){

File readfile = new File (filepath + "\" + filelist [I]);

If (! Readfile. isDirectory ()){

// System. out. println ("path =" + readfile. getPath ());

// System. out. println ("absolutepath ="

// + Readfile. getAbsolutePath ());

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

 

} Else if (readfile. isDirectory ()){

Readfile (filepath + "\" + filelist [I]);

}

}

 

}

 

} Catch (FileNotFoundException e ){

System. out. println ("readfile () Exception:" + e. getMessage ());

}

Return true;

}

 

/**

* Delete all folders and files in a folder

*/

 

 

/* Public static boolean deletefile (String delpath)

Throws FileNotFoundException, IOException {

Try {

 

File file = new File (delpath );

If (! File. isDirectory ()){

System. out. println ("1 ");

File. delete ();

} Else if (file. isDirectory ()){

System. out. println ("2 ");

String [] filelist = file. list ();

For (int I = 0; I <filelist. length; I ++ ){

File delfile = new File (delpath + "\" + filelist [I]);

If (! Delfile. isDirectory ()){

System. out. println ("path =" + delfile. getPath ());

System. out. println ("absolutepath ="

+ Delfile. getAbsolutePath ());

System. out. println ("name =" + delfile. getName ());

Delfile. delete ();

System. out. println ("File deleted successfully ");

} Else if (delfile. isDirectory ()){

Deletefile (delpath + "\" + filelist [I]);

}

}

File. delete ();

 

}

 

} Catch (FileNotFoundException e ){

System. out. println ("deletefile () Exception:" + e. getMessage ());

}

Return true;

}*/

 

Public static void main (String [] args ){

Try {

Readfile ("C :\\ Users \ SW \ Desktop \ SKJ_H25 Zheng \ 004_RCAG \ 003_SKJ ");

// Deletefile ("D:/file ");

} Catch (FileNotFoundException ex ){

} Catch (IOException ex ){

}

System. out. println ("OK ");

}

 

}

Method 2:

?

1

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

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

Package otherstudy;

 

Import java. io. File;

Import java. io. FileNotFoundException;

Import java. io. IOException;

 

/**

* @ ClassName: TestReadFile

* @ CreateTime: Aug 1, 2014 11:42:44 AM

* @ Author: mayi

* @ Description: Reads and deletes all files in the folder.

*

*/

Public class TestReadFile {

/**

* Obtain the absolute WebRoot path of the project.

* @ Return

*/

String getProjectPath (){

// Obtain the path such as "/d:/$ {workspace}/$ {projectName}/WebRoot/WEB-INF/classes /"

String path = this. getClass (). getResource ("/"). getPath ();

// Extract the project path from the path string

Path = path. substring (1, path. indexOf ("WEB-INF/classes "));

System. out. println ("project path:" + path );

Return path;

}

/**

* @ Param args

*/

Public static void main (String [] args ){

TestReadFile trf = new TestReadFile ();

String xmlPath = trf. getProjectPath () + "testDocs ";

Try {

ReadAllFile (xmlPath );

} Catch (FileNotFoundException e ){

E. printStackTrace ();

} Catch (IOException e ){

E. printStackTrace ();

}

}

/**

* Read all files in the specified path folder

* @ Param filepath

* @ Return

* @ Throws FileNotFoundException

* @ Throws IOException

*/

Public static boolean readAllFile (String filepath)

Throws FileNotFoundException, IOException {

Try {

File file = new File (filepath );

If (! File. isDirectory ()){

System. out. println ("\ n file information :");

System. out. println ("\ t Relative Path =" + file. getPath ());

System. out. println ("\ t absolute path =" + file. getAbsolutePath ());

System. out. println ("\ t file full name =" + file. getName ());

 

} Else if (file. isDirectory ()){

System. out. println ("\ n folder file list information :");

File [] fileList = file. listFiles ();

For (int I = 0; I <fileList. length; I ++ ){

File readfile = fileList [I];

If (! Readfile. isDirectory ()){

System. out. println ("\ n \ t Relative Path =" + readfile. getPath ());

System. out. println ("\ t absolute path =" + readfile. getAbsolutePath ());

System. out. println ("\ t file full name =" + readfile. getName ());

} Else if (readfile. isDirectory ()){

ReadAllFile (fileList [I]. getPath ());

}

}

}

} Catch (FileNotFoundException e ){

System. out. println ("readfile () Exception:" + e. getMessage ());

}

Return true;

}

 

/**

* Delete all folders and files in a folder

* @ Param delpath

* @ Return

* @ Throws FileNotFoundException

* @ Throws IOException

*/

Public static boolean deleteFile (String delpath)

Throws FileNotFoundException, IOException {

Try {

File file = new File (delpath );

If (! File. isDirectory ()){

System. out. println ("1 ");

File. delete ();

} Else if (file. isDirectory ()){

System. out. println ("2 ");

File [] fileList = file. listFiles ();

For (int I = 0; I <fileList. length; I ++ ){

File delfile = fileList [I];

If (! Delfile. isDirectory ()){

System. out. println ("Relative Path =" + delfile. getPath ());

System. out. println ("absolute path =" + delfile. getAbsolutePath ());

System. out. println ("file full name =" + delfile. getName ());

Delfile. delete ();

System. out. println ("File deleted successfully ");

} Else if (delfile. isDirectory ()){

DeleteFile (fileList [I]. getPath ());

}

}

File. delete ();

}

} Catch (FileNotFoundException e ){

System. out. println ("deletefile () Exception:" + e. getMessage ());

}

Return true;

}

}

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.