Java文件读写

Java文件读写,第1张

实用的模糊(通配符)文件查找程序

1 import java.io.File

2 import java.util.regex.Matcher

3 import java.util.regex.Pattern

4 import java.util.ArrayList

5

6 /** *//**

7 * <p>Title: FileService </p>

8* <p>Description: 获取文件 </p>

9* <p>Copyright: Copyright (c) 2007</p>

10* <p>Company: </p>

11* @author not attributable

12* @version 1.0

13*/

14public class FileService {

15 public FileService() {

16 }

17

18 /** *//**

19* 在本文件夹下查找

20* @param s String 文件名

21* @return File[] 找到的文件

22*/

23 public static File[] getFiles(String s)

24 {

25 return getFiles("./",s)

26 }

27

28 /** *//**

29* 获取文件

30* 可以根据正则表达式查找

31* @param dir String 文件夹名称

32* @param s String 查找文件名,可带*.?进行模糊查询

33* @return File[] 找到的文件

34 */

35 public static File[] getFiles(String dir,String s) {

36 //开始的文件夹

37 File file = new File(dir)

38

39 s = s.replace('.', '#')

40 s = s.replaceAll("#", "\\\\.")

41 s = s.replace('*', '#')

42 s = s.replaceAll("#", ".*")

43 s = s.replace('?', '#')

44 s = s.replaceAll("#", ".?")

45 s = "^" + s + "$"

46

47 System.out.println(s)

48 Pattern p = Pattern.compile(s)

49 ArrayList list = filePattern(file, p)

50

51 File[] rtn = new File[list.size()]

52 list.toArray(rtn)

53 return rtn

54 }

55

56 /** *//**

57* @param file File 起始文件夹

58* @param p Pattern 匹配类型

59* @return ArrayList 其文件夹下的文件夹

60*/

61

62 private static ArrayList filePattern(File file, Pattern p) {

63 if (file == null) {

64 return null

65 }

66 else if (file.isFile()) {

67 Matcher fMatcher = p.matcher(file.getName())

68 if (fMatcher.matches()) {

69 ArrayList list = new ArrayList()

70 list.add(file)

71 return list

72 }

73 }

74 else if (file.isDirectory()) {

75 File[] files = file.listFiles()

76 if (files != null &&files.length >0) {

77 ArrayList list = new ArrayList()

78 for (int i = 0i <files.lengthi++) {

79 ArrayList rlist = filePattern(files[i], p)

80 if (rlist != null) {

81 list.addAll(rlist)

82 }

83 }

84 return list

85 }

86 }

87 return null

88 }

89

90 /** *//**

91* 测试

92* @param args String[]

93*/

94 public static void main(String[] args) {

95 }

96}

java读取文本文件的方法有很多 这个例子主要介绍最简单 最常用的BufferedReader类 完整例子如下 package net chinaunix blog hzm textimport java io BufferedReaderimport java io FileReaderimport java io IOExceptionpublic class ReadFile {private String pathpublic ReadFile(String filePath){path = filePath}public String[] openFile() throws IOException{FileReader fr = new FileReader(path) BufferedReader textReader = new BufferedReader(fr) String[] textData = new String[readLines()]int ifor(i= i <readLines() i++){textData[i] = textReader readLine() }textReader close() return textData}int readLines() throws IOException{FileReader fileToRead = new FileReader(path) BufferedReader bf = new BufferedReader(fileToRead) int numberOfLines = @SuppressWarnings( unused )String oneLinewhile((oneLine = bf readLine()) != null){numberOfLines++}bf close() return numberOfLines}}package net chinaunix blog hzm textimport java io IOExceptionpublic class FileData {public static void main(String[] args) throws IOException{String filePath = C:/text txt try{ReadFile reader = new ReadFile(filePath) String[] content = reader openFile() int ifor(i= i<content lengthi++){System out println(content[i]) }}catch(IOException e){System out println( 异常信息 + e getMessage()) }}}java io BufferedReaderThe buffer size may be specified or the default size may be used The default is large enough for most purposes In general each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly such as FileReaders and InputStreamReaders For example BufferedReader in = new BufferedReader(new FileReader( foo in )) will buffer the input from the specified file Without buffering each invocation of read() or readLine() could cause bytes to be read from the file converted into characters and then returned which can be very inefficient Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader java io FileReaderFileReader is meant for reading streams of characters For reading streams of raw bytes consider using a FileInputStream lishixinzhi/Article/program/Java/hx/201311/26249


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11774952.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存