MCPcopy Create free account
hub / github.com/diyhi/bbs / readFileBomToString

Method readFileBomToString

src/main/java/cms/utils/FileUtil.java:120–171  ·  view source on GitHub ↗

读文件(读取的内容不包含UTF-8 BOM标志) @param path 路径 @param encoding 编码 UTF-8 @return 文本字符串

(String path,String encoding)

Source from the content-addressed store, hash-verified

118 * @return 文本字符串
119 */
120 public static String readFileBomToString(String path,String encoding){
121 File file = new File(PathUtil.defaultExternalDirectory()+File.separator+path);
122 StringBuffer result = new StringBuffer("");
123 if(file.exists()){
124 BufferedReader br = null;
125 String r;
126 try {
127 // 如果是UTF-8编码,需去掉BOM标志
128 if ("UTF-8".equalsIgnoreCase(encoding)) {
129
130 //处理windows自动添加的BOM
131 BOMInputStream bis = BOMInputStream.builder()
132 .setFile(file)
133 .setByteOrderMarks(ByteOrderMark.UTF_8)
134 .setInclude(false) // 设置为 false,将 BOM 从流中移除
135 .get();
136 //创建输入流
137 InputStreamReader isr = new InputStreamReader(bis, StandardCharsets.UTF_8 );
138
139
140 br = new BufferedReader(isr);
141
142 while ((r = br.readLine()) != null) {
143 if (!StringUtils.isEmpty(r)) {
144 result.append(r);
145 }
146 }
147
148 }else{
149 return FileUtils.readFileToString(file, encoding);
150 }
151 } catch (IOException e) {
152 // TODO Auto-generated catch block
153 // e.printStackTrace();
154 if (logger.isErrorEnabled()) {
155 logger.error("读文件(读取的内容不包含UTF-8 BOM标志)",e);
156 }
157 }finally {
158 if (br != null) {
159 try {
160 br.close();
161 } catch (IOException e) {
162 // e.printStackTrace();
163 if (logger.isErrorEnabled()) {
164 logger.error("读文件(读取的内容不包含UTF-8 BOM标志)",e);
165 }
166 }
167 }
168 }
169 }
170 return result.toString();
171 }
172 /**
173 * 读文件
174 * @param file 文件

Callers

nothing calls this directly

Calls 6

getMethod · 0.80
readLineMethod · 0.80
readFileToStringMethod · 0.80
isEmptyMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected