灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2091回复:0

将String保存成文件

楼主#
更多 发布于:2012-09-06 13:52

[html] 如题,将String保存成文件。
如题,将String保存成文件。[html] /**
  * 将String数据存为文件
  */
public static File getFileFromBytes(String name,String path) {
    byte[] b=name.getBytes();
     BufferedOutputStream stream = null;
     File file = null;
     try {
         file = new File(path);
         FileOutputStream fstream = new FileOutputStream(file);
         stream = new BufferedOutputStream(fstream);
         stream.write(b);
     } catch (Exception e) {
         e.printStackTrace();
     } finally {
         if (stream != null) {
             try {
                 stream.close();
             } catch (IOException e1) {
                 e1.printStackTrace();
             }
         }
     }
     return file;
}
   /**
     * 将String数据存为文件
     */
    public static File getFileFromBytes(String name,String path) {
     byte[] b=name.getBytes();
        BufferedOutputStream stream = null;
        File file = null;
        try {
            file = new File(path);
            FileOutputStream fstream = new FileOutputStream(file);
            stream = new BufferedOutputStream(fstream);
            stream.write(b);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return file;
    }
byte数据保存为文件:
[html] /**  
  * 将byte数据存为文件 www.atcpu.com/bbs
  */
public static File getFileFromBytes(byte[] b,String path) {
     BufferedOutputStream stream = null;
     File file = null;
     try {
         file = new File(path);
         FileOutputStream fstream = new FileOutputStream(file);
         stream = new BufferedOutputStream(fstream);
         stream.write(b);
     } catch (Exception e) {
         e.printStackTrace();
     } finally {
         if (stream != null) {
             try {
                 stream.close();
             } catch (IOException e1) {
                 e1.printStackTrace();
             }
         }
     }
     return file;
}


喜欢0 评分0
游客

返回顶部