将String保存成文件

来源:互联网 发布:消防联动编程 编辑:程序博客网 时间:2024/05/27 20:58
如题,将String保存成文件。
   /**     * 将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数据保存为文件:

   /**      * 将byte数据存为文件     */    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;    }


原创粉丝点击