java io流总结
前言:java中io流主要分为两种:字节流和字符流。
字节流:以字节作为传输单位。
字符流:以字符作为传输单位,这里需要注意就是字符编码问题,不然会出现乱码问题,比如GBK编码处理英文不会出现乱码,但处理中文就出现乱码,所以如果碰到中文乱码一般都是编码问题。
那么如何选择使用字节流还是字符流:一个标准,如果是文本处理可以考虑用字符流或者字节流,其他一律用字节流。
怎么区分一个类是字节流还是字符流:一般来说,字节流以InputStream/OutputStream结尾,字符流以Reader/Writer结尾。
说到java io流就必须涉及文件,下面通过几个实用demo来说明:
这些demo可以直接应用到项目
一:对本地文件的操作,读取指定路径下文件内容
1.字节流实现,代码如下
1 /** 2 * @description: 读取文件的内容 3 * <br><br> 4 * @param filePath 文件路径 5 * @param charset 编码,默认为UTF-8 6 * @return 7 * @author zhonghong 9 */ 10 public static String readFileContent(String filePath, String charset){ 11 StringBuilder result = new StringBuilder(); 12 if(StringUtils.isBlank(charset)){ 13 charset = "UTF-8"; 14 } 15 File file = new File(filePath); 16 if(file.isFile()){ 17 FileInputStream fis = null; 18 try { 19 fis = new FileInputStream(file); 20 BufferedInputStream bis = new BufferedInputStream(fis); 21 byte[] b = new byte[1024]; 22 int num = 0; 23 while((num = bis.read(b)) != -1){ 24 String s = new String(b, 0, num, Charset.forName(charset)); //将字节数组转换为字符串 25 result.append(s); 26 } 27 bis.close(); 28 } catch (IOException e) { 29 logger.error("read file content is error!"); 30 e.printStackTrace(); 31 } finally{ 32 if(fis != null){ 33 try { 34 fis.close(); 35 } catch (IOException e) { 36 logger.error("exec fis.close() is error!"); 37 e.printStackTrace(); 38 } 39 } 40 } 41 }else{ 42 logger.info("the file path is "+filePath+", this is not file"); 43 } 44 logger.info("read file content is successful!"); 45 return result.toString(); 46 }
2.字符流实现
1 /** 2 * @description: 以字符流读取文件内容 3 * <br><br> 4 * @param filePath 5 * @return 6 * @author zhonghong 7 */ 8 public static String readFileContent1(String filePath) { 9 StringBuilder sb = new StringBuilder(); 10 File file = new File(filePath); 11 if(file.exists() && file.isFile()){ 12 FileReader fr = null; 13 try { 14 fr = new FileReader(file); 15 System.out.println(fr.getEncoding()); 16 BufferedReader br = new BufferedReader(fr); 17 char[] c = new char[1024]; 18 int num = 0; 19 if((num = br.read(c)) != -1){ 20 sb.append(c, 0, num); 21 } 22 br.close(); 23 } catch (IOException e) { 24 logger.error("read file content is error!"); 25 e.printStackTrace(); 26 } finally{ 27 if(fr != null){ 28 try { 29 fr.close(); 30 } catch (IOException e) { 31 logger.error("exec fr's close is error"); 32 e.printStackTrace(); 33 } 34 } 35 } 36 }else { 37 logger.info("this file is not exists"); 38 } 39 40 return sb.toString(); 41 }
二:读取远程文件内容
1.字节流实现
1 /** 2 * @description: 根据url,读取远程文件内容</br> 3 * 中文会出现乱码 4 * <br><br> 5 * @param url 文件url 6 * @return 7 * @author zhonghong 8 */ 9 public static String readURLFileContent(String url){ 10 StringBuilder sb = new StringBuilder(); 11 InputStream is = null; 12 try { 13 URL u = new URL(url); 14 URLConnection urlConnection = u.openConnection(); 15 is = urlConnection.getInputStream(); 16 BufferedInputStream bis = new BufferedInputStream(is); 17 byte[] b = new byte[1024]; 18 int num = 0; 19 while((num = bis.read(b)) != -1){ 20 String s = new String(b, 0, num, Charset.forName("UTF-8")); 21 sb.append(s); 22 } 23 bis.close(); 24 } catch (IOException e) { 25 logger.error("read remote file content is error!"); 26 e.printStackTrace(); 27 } finally{ 28 if(is !=null){ 29 try { 30 is.close(); 31 } catch (IOException e) { 32 logger.error("exec is.close() is error!"); 33 e.printStackTrace(); 34 } 35 } 36 } 37 logger.info("read remote file content is successful!"); 38 return sb.toString(); 39 }
2.字符流实现
1 /** 2 * @description: 根据url和编码方式,获取远程文件内容 3 * <br><br> 4 * @param url url 5 * @param charset 编码方式:eg UTF-8/GB2312/GBK 6 * @return 文件内容 7 */ 8 public static String readURLFileContent(String url, String charset){ 9 StringBuilder sb = new StringBuilder(); 10 InputStream is = null; 11 try { 12 URL u = new URL(url); 13 URLConnection urlConnection = u.openConnection(); 14 is = urlConnection.getInputStream(); 15 InputStreamReader isr = new InputStreamReader(is, Charset.forName(charset)); 16 BufferedReader br = new BufferedReader(isr); 17 char[] b = new char[1024]; 18 int num = 0; 19 while((num = br.read(b)) != -1){ 20 String s = new String(b, 0, num); 21 br.readLine(); 22 sb.append(s); 23 } 24 } catch (IOException e) { 25 logger.error("read remote file content is error!"); 26 e.printStackTrace(); 27 }finally{ 28 if(is !=null){ 29 try { 30 is.close(); 31 } catch (IOException e) { 32 logger.error("exec is.close() is error!"); 33 e.printStackTrace(); 34 } 35 } 36 } 37 logger.info("read remote file content is successful!"); 38 return sb.toString(); 39 }
三.往文件写内容
1.字节流
1 /** 2 * @description: 往文件写内容 3 * <br><br> 4 * @param filePath 存放文件路径,包含文件名。比如:E://filePath/readme.txt 5 * @param content 文件内容。 6 * @param charset 编码:UTF-8/GBK/GB2312 7 * @return 文件路径 8 * @author hong 9 */ 10 public static String writeFileContent(String filePath, String content, String charset){ 11 if(StringUtils.isNotBlank(filePath)){ 12 File file = new File(filePath); 13 if(!file.exists()){ 14 try { 15 file.createNewFile(); 16 } catch (IOException e) { 17 logger.error("create new file is error!"); 18 e.printStackTrace(); 19 return ""; 20 } 21 } 22 23 FileOutputStream fos = null; 24 BufferedOutputStream bos = null; 25 try { 26 fos = new FileOutputStream(file, false); 27 bos = new BufferedOutputStream(fos); 28 byte[] b = content.getBytes(Charset.forName(charset)); 29 bos.write(b); 30 bos.flush(); 31 } catch (IOException e) { 32 logger.error("write content to file is error!!"); 33 e.printStackTrace(); 34 return ""; 35 } finally{ 36 if(bos != null){ 37 try { 38 bos.close(); 39 } catch (IOException e) { 40 logger.error("exec bos's close is error!"); 41 e.printStackTrace(); 42 } 43 } 44 if(fos != null){ 45 try { 46 fos.close(); 47 } catch (IOException e) { 48 logger.error("exec fos's close is error!"); 49 e.printStackTrace(); 50 } 51 } 52 } 53 }else{ 54 logger.info("the file path is null!!"); 55 return ""; 56 } 57 58 return filePath; 59 }
2.字符流实现
1 /** 2 * @description: 往文件写内容 3 * <br><br> 4 * @param filePath 存放文件路径,包含文件名。比如:E://filePath/readme.txt 5 * @param content 文件内容。 6 * @param charset 编码:UTF-8/GBK/GB2312 7 * @return 文件路径 8 * @author hong 9 */ 10 public static String writeFileContent(String filePath, String content){ 11 if(StringUtils.isNotBlank(filePath)){ 12 File file = new File(filePath); 13 if(!file.exists()){ 14 try { 15 file.createNewFile(); 16 } catch (IOException e) { 17 logger.error("create new file is error!"); 18 e.printStackTrace(); 19 return ""; 20 } 21 } 22 23 FileWriter fw = null; 24 BufferedWriter bw = null; 25 try { 26 fw = new FileWriter(file, false); 27 bw = new BufferedWriter(fw); 28 bw.write(content); 29 bw.flush(); 30 } catch (IOException e) { 31 logger.error("write content to file is error!!"); 32 e.printStackTrace(); 33 return ""; 34 } finally{ 35 if(bw != null){ 36 try { 37 bw.close(); 38 } catch (IOException e) { 39 logger.error("exec bw's close is error!"); 40 e.printStackTrace(); 41 } 42 } 43 if(fw != null){ 44 try { 45 fw.close(); 46 } catch (IOException e) { 47 logger.error("exec fw's close is error!"); 48 e.printStackTrace(); 49 } 50 } 51 } 52 }else{ 53 logger.info("the file path is null!!"); 54 return ""; 55 } 56 57 return filePath; 58 }
以上是java io流对文件的操作,可以写成工具类使用,其他类型的文件操作,可以参考上。
其实,主要你掌握了java io工作原理,对于任何io流操作,你都可以自己写。

浙公网安备 33010602011771号