java读取文件软件_java 文件读写工具 FileUtil

1 packagecom.wiscom.utils;2

3 importjava.io.BufferedReader;4 importjava.io.File;5 importjava.io.FileInputStream;6 importjava.io.FileNotFoundException;7 importjava.io.FileWriter;8 importjava.io.IOException;9 importjava.io.InputStream;10 importjava.io.InputStreamReader;11 importjava.io.Reader;12 importjava.io.UnsupportedEncodingException;13

14 public classFileUtil {15

16 /**

17 * 以字节为单位读取文件,通常用于读取二进制文件,如图片18 *@parampath19 *@return

20 */

21 public staticString readByBytes(String path) {22 String content = null;23

24 try{25 InputStream inputStream = newFileInputStream(path);26 StringBuffer sb = newStringBuffer();27 int c = 0;28 byte[] bytes = new byte[1024];29 /*

30 * InputStream.read(byte[] b)31 *32 * Reads some number of bytes from the input stream and stores them into the buffer array b. 从输入流中读取一些字节存入缓冲数组b中33 * The number of bytes actually read is returned as an integer. 返回实际读到的字节数34 * This method blocks until input data is available, end of file is detected, or an exception is thrown.35 * 该方法会一直阻塞,直到输入数据可以得到、或检测到文件结束、或抛出异常 — 意思是得到数据就返回36 */

37 while ((c = inputStream.read(bytes)) != -1) {38 sb.append(new String(bytes, 0, c, “utf-8”));39 }40

41 content =sb.toString();42 inputStream.close();43 } catch(FileNotFoundException e) {44 e.printStackTrace();45 } catch(IOException e) {46 e.printStackTrace();47 }48

49 returncontent;50 }51

52 /**

53 * 以行为单位读取文件,常用于读取面向行的格式化文件54 *@parampath55 *@return

56 */

57 public staticString readByLines(String path) {58 String content = null;59

60

61 try{62 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(path), “utf-8”));63

64 StringBuffer sb = newStringBuffer();65 String temp = null;66 while ((temp = bufferedReader.readLine()) != null) {67 sb.append(temp);68 }69

70 content =sb.toString();71 bufferedReader.close();72 } catch(UnsupportedEncodingException e) {73 e.printStackTrace();74 } catch(IOException e) {75 e.printStackTrace();76 }77

78 returncontent;79 }80

81 /**

85 */

86 public staticString readByChars(String path) {87 String content = null;88

89 try{90

91 Reader reader = new InputStreamReader(new FileInputStream(path), “utf-8”);92 StringBuffer sb = newStringBuffer();93

94

95 char[] tempchars = new char[1024];96 while (reader.read(tempchars) != -1) {97 sb.append(tempchars);98 }99

100 content =sb.toString();101 reader.close();102 } catch(Exception e) {103 e.printStackTrace();104 }105 returncontent;106 }107

108 /**

109 * 把内容content写的path文件中110 *@paramcontent111 *@parampath112 *@return

113 */

114 public static booleansaveAs(String content, String path) {115

116 FileWriter fw = null;117

118 //System.out.println(“把内容:” + content + “, 写入文件:” + path);

119

120 try{121 /**

122 * Constructs a FileWriter object given a File object.123 * If the second argument is true, then bytes will be written to the end of the file rather than the beginning.124 * 根据给定的File对象构造一个FileWriter对象。 如果append参数为true, 则字节将被写入到文件的末尾(向文件中追加内容)125 *126 * Parameters:127 * file, a File object to write to 带写入的文件对象128 * append, if true, then bytes will be written to the end of the file rather than the beginning129 * Throws:130 * IOException -131 * if the file exists but is a directory rather than a regular file,132 * does not exist but cannot be created,133 * or cannot be opened for any other reason134 * 异常的3种情况:135 * file对象是一个存在的目录(不是一个常规文件)136 * file对象是一个不存在的常规文件,但不能被创建137 * file对象是一个存在的常规文件,但不能被打开138 *139 */

140 fw = new FileWriter(new File(path), false);141 if (content != null) {142 fw.write(content);143 }144 } catch(IOException e) {145 e.printStackTrace();146 return false;147 } finally{148 if (fw != null) {149 try{150 fw.flush();151 fw.close();152 } catch(IOException e) {153 e.printStackTrace();154 }155 }156 }157 return true;158 }159 }

文章知识点与官方知识档案匹配,可进一步学习相关知识Java技能树首页概览92868 人正在系统学习中 相关资源:c#编写的鸡兔同笼程序

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年1月21日
下一篇 2021年1月21日

相关推荐