免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2495 | 回复: 1
打印 上一主题 下一主题

文件读取为字符串,字符串存储为文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-15 15:46 |只看该作者 |倒序浏览
文件读取为字符串,字符串存储为文件










将外部文件读取成为字符串

Java代码
  1. public String doPageToStr(String filename) {   
  2.         StringBuffer buf = new StringBuffer();   
  3.         BufferedReader breader = null;   
  4.         try {   
  5.             breader = new BufferedReader(   
  6.                     new FileReader(filename));   
  7.             while (breader.ready()) {   
  8.                 buf.append((char) breader.read());   
  9.             }   
  10.         } catch (Exception e) {   
  11.             e.printStackTrace();   
  12.         } finally {   
  13.             if (breader != null) {   
  14.                 try {   
  15.                     breader.close();   
  16.                 } catch (IOException e) {   
  17.                     e.printStackTrace();   
  18.                 }   
  19.             }   
  20.         }   
  21.         return buf.toString();   
  22.     }  

  23. public String doPageToStr(String filename) {
  24.                 StringBuffer buf = new StringBuffer();
  25.                 BufferedReader breader = null;
  26.                 try {
  27.                         breader = new BufferedReader(
  28.                                         new FileReader(filename));
  29.                         while (breader.ready()) {
  30.                                 buf.append((char) breader.read());
  31.                         }
  32.                 } catch (Exception e) {
  33.                         e.printStackTrace();
  34.                 } finally {
  35.                         if (breader != null) {
  36.                                 try {
  37.                                         breader.close();
  38.                                 } catch (IOException e) {
  39.                                         e.printStackTrace();
  40.                                 }
  41.                         }
  42.                 }
  43.                 return buf.toString();
  44.         } 将字符串存储为文件
复制代码
Java代码
  1. public void doTask(String strPage, String basePath, ProductDownload product) {   
  2.         synchronized (lock) {   
  3.             String text = strPage;   
  4.             String path = basePath;   
  5.             String filePath = path + File.separator + "product"  
  6.                     + product.getProductId() + File.separator + "product.html";   
  7.             FileOutputStream fos = null;   
  8.             try {   
  9.                 fos = new FileOutputStream(filePath);   
  10.                 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(   
  11.                         fos));   
  12.                 bw.write(text);   
  13.                 bw.flush();   
  14.             } catch (Exception e) {   
  15.                 e.printStackTrace();   
  16.             } finally {   
  17.                 if (fos != null) {   
  18.                     try {   
  19.                         fos.close();   
  20.                     } catch (Exception e) {   
  21.                     }   
  22.                 }   
  23.             }   
  24.         }   
  25.     }  

  26. public void doTask(String strPage, String basePath, ProductDownload product) {
  27.                 synchronized (lock) {
  28.                         String text = strPage;
  29.                         String path = basePath;
  30.                         String filePath = path + File.separator + "product"
  31.                                         + product.getProductId() + File.separator + "product.html";
  32.                         FileOutputStream fos = null;
  33.                         try {
  34.                                 fos = new FileOutputStream(filePath);
  35.                                 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
  36.                                                 fos));
  37.                                 bw.write(text);
  38.                                 bw.flush();
  39.                         } catch (Exception e) {
  40.                                 e.printStackTrace();
  41.                         } finally {
  42.                                 if (fos != null) {
  43.                                         try {
  44.                                                 fos.close();
  45.                                         } catch (Exception e) {
  46.                                         }
  47.                                 }
  48.                         }
  49.                 }
  50.         }
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-03-15 15:46 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP