当前位置:七道奇文章资讯编程技术Java编程
日期:2011-01-26 02:54:00  来源:本站整理

Java字符串与文件的互转[Java编程]

赞助商链接



  本文“Java字符串与文件的互转[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

    Java中有时刻需求读取一个文本类的文件,将其转换为字符串,然后做进一步处理.Java中没有现成的API办法,自己手动实现一个,大家来分享.

    1、字符串转换为文件

        /**
         * 将字符串写入指定文件(当指定的父途径中文件夹不存在时,会最大限度去成立,以保证保存成功!)
         *
         * @param res            原字符串
         * @param filePath 文件途径
         * @return 成功标志
         */

        public static boolean string2File(String res, String filePath) {
                boolean flag = true;
                BufferedReader bufferedReader = null;
                BufferedWriter bufferedWriter = null;
                try {
                        File distFile = new File(filePath);
                        if (!distFile.getParentFile().exists()) distFile.getParentFile().mkdirs();
                        bufferedReader = new BufferedReader(new StringReader(res));
                        bufferedWriter = new BufferedWriter(new FileWriter(distFile));
                        char buf[] = new char[1024];         //字符缓冲区
                        int len;
                        while ((len = bufferedReader.read(buf)) != -1) {
                                bufferedWriter.write(buf, 0, len);
                        }
                        bufferedWriter.flush();
                        bufferedReader.close();
                        bufferedWriter.close();
                } catch (IOException e) {
                        e.printStackTrace();
                        flag = false;
                        return flag;
                } finally {
                        if (bufferedReader != null) {
                                try {
                                        bufferedReader.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                }
                return flag;
        }

    2、文件转换为字符串

        /**
         * 文本文件转换为指定编码的字符串
         *
         * @param file         文本文件
         * @param encoding 编码范例
         * @return 转换后的字符串
         * @throws IOException
         */

        public static String file2String(File file, String encoding) {
                InputStreamReader reader = null;
                StringWriter writer = new StringWriter();
                try {
                        if (encoding == null || "".equals(encoding.trim())) {
                                reader = new InputStreamReader(new FileInputStream(file), encoding);
                        } else {
                                reader = new InputStreamReader(new FileInputStream(file));
                        }
                        //将输入流写入输出流
                        char[] buffer = new char[DEFAULT_BUFFER_SIZE];
                        int n = 0;
                        while (-1 != (n = reader.read(buffer))) {
                                writer.write(buffer, 0, n);
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                        return null;
                } finally {
                        if (reader != null)
                                try {
                                        reader.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                }
                //返反转展转换后果
                if (writer != null)
                        return writer.toString();
                else return null;
        }

    不想抛出太多的查抄非常,假如你需求捕捉,就抛出吧.

    注意:关于文件转换为字符串,存在一个编码问题.上面的file2String办法中,假如你不知道编码,调用时刻办法设为null便可,此时利用系统默许的编码范例.


  以上是“Java字符串与文件的互转[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 利用Javascript实现网页水印(非图片水印)
  • Java开辟环境的搭建
  • Ubuntu java安装与配置
  • 办理Ubuntu 10.04 Firefox3.6 Java浏览器插件不工作的问
  • Ubuntu重装后Java环境的设置
  • Sun Java进入Ubuntu 10.10软件中央
  • Ubuntu 10.10配置Java开辟环境
  • 在Ubuntu 10.10中配置Java环境变量的办法
  • Ubuntu下Java环境的搭建
  • Ubuntu 10.04 下安装 Java, JRE
  • Ubuntu 10.04下的搭建SUN JAVA开辟环境
  • Ubuntu 12.04安装java7
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .