java读写文件

java读写文件1、一次性读取整个文件内容/***一次性读取全部文件数据*@paramstrFile*/publicstaticvoidreadFile(StringstrFile){try{InputStreamis=newFileInputStream(strFile);…

大家好,又见面了,我是你们的朋友全栈君。

1、一次性读取整个文件内容

/** * 一次性读取全部文件数据 * @param strFile */
    public static void readFile(String strFile){ 
   
        try{ 
   
            InputStream is = new FileInputStream(strFile);
            int iAvail = is.available();
            byte[] bytes = new byte[iAvail];
            is.read(bytes);
            logger.info("文件内容:\n" + new String(bytes));
            is.close();
        }catch(Exception e){ 
   
            e.printStackTrace();
        }
    }

2、字符流按行读取文件

/** * 按行读取文件 * @param strFile */
    public static void readFileByLine(String strFile){ 
   
        try { 
   
            File file = new File(strFile);
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            String strLine = null;
            int lineCount = 1;
            while(null != (strLine = bufferedReader.readLine())){ 
   
                logger.info("第[" + lineCount + "]行数据:[" + strLine + "]");
                lineCount++;
            }
        }catch(Exception e){ 
   
            e.printStackTrace();
        }
    }

3、字节流按行读取文件

 /**
     * 按行读取全部文件数据
     *
     * @param strFile
     */
    public static StringBuffer readFile(String strFile) throws IOException { 
   
        StringBuffer strSb = new StringBuffer();
        InputStreamReader inStrR = new InputStreamReader(new FileInputStream(strFile), "UTF-8");
        // character streams
        BufferedReader br = new BufferedReader(inStrR);
        String line = br.readLine();
        while (line != null) { 
   
            strSb.append(line).append("\r\n");
            line = br.readLine();
        }
        return strSb;
    }

4、写文件

  /**
     * 写入文件
     * @param fileName
     * @param s
     * @throws IOException
     */
    public static void writeToFile(String fileName,String s) throws IOException { 
   
        File f1 = new File(fileName);
        OutputStream out = null;
        BufferedWriter bw = null;
        if (f1.exists()) { 
   
            out = new FileOutputStream(f1);
            bw = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
            bw.write(s);
            bw.flush();
            bw.close();
        } else { 
   
            System.out.println("文件不存在");
        }
    }

5、写文件,追加内容

/**
     * 追加文件
     */
    public static void writeToFileAppend(String fileName, String text) { 
   
        FileWriter fw = null;
        try { 
   
            //如果文件存在,则追加内容;如果文件不存在,则创建文件
            File f = new File(fileName);
            fw = new FileWriter(f, true);
        } catch (IOException e) { 
   
            e.printStackTrace();
        }
        PrintWriter pw = new PrintWriter(fw);
        pw.println(text);
        pw.flush();
        try { 
   
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) { 
   
            e.printStackTrace();
        }
    }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150422.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月18日 上午9:36
下一篇 2022年6月18日 上午9:36


相关推荐

  • DSP效果器调音教程_DSP控制器

    DSP效果器调音教程_DSP控制器1)DSP输出给5V的电路(如D/A),无需加任何缓冲电路,可以直接连接。3)的JTAG口的信号也必须为3.3V,否则有可能损坏DSP。dsp教程24。为什么要片内RAM大的DSP效率高?目前DSP发展的片内存储器RAM越来越大,要设计高效的DSP系统,就应该选择片内RAM较大的DSP。片内RAM同片外存储器相比,有以下优点:1)片内RAM的速度较快,可以保证DSP无等待运行。2)对于C2000…

    2025年8月27日
    12
  • Spring Boot 集成MyBatis[通俗易懂]

    Spring Boot 集成MyBatis[通俗易懂]SpringBoot集成MyBatis在配置MyBatis前,我们先配置一个druid数据源。SpringBoot集成druiddruid有很多个配置选项,使用SpringBoot的ConfigurationProperties我们可以很方便的配置druid。创建DataSourceConfig如下:/***数据源**@authorliuzh*@since201

    2022年5月25日
    37
  • 懂车帝多模态大模型算法通过备案

    懂车帝多模态大模型算法通过备案

    2026年3月14日
    1
  • 快速入门(完整):Python实例100个(基于最新Python3.7版本)

    快速入门(完整):Python实例100个(基于最新Python3.7版本)Python3100 例原题地址 http www runoob com python python 100 examples htmlgit 地址 https github com RichardFu123 Python100Cas 原例为 Python2 7 版本重写过程中有不少是随意发挥的重写运行版本 Python3 7 总

    2026年3月19日
    2
  • 如何用 Stata 做调节中介效应检验?

    如何用 Stata 做调节中介效应检验?作者 崔颖 中央财经大学 Source HOWCANIDOMOD STATAFAQ 2019 暑期 实证研究方法与经典论文 专题班 连玉君 江艇主讲文章目录 2019 暑期 实证研究方法与经典论文 专题班 连玉君 江艇主讲 1 中介效应和调节效应的定义 2 调节中介效应的检验方法 2 1 调用数据并定义变量 2 2

    2026年3月26日
    2
  • xshell安装步骤_Xshell怎么使用

    xshell安装步骤_Xshell怎么使用XShell可以在Windows界面下来访问远端不同系统下的服务器,从而比较好的达到远程控制终端的目的。它支持RLOGIN、SFTP、SERIAL、TELNET、SSH2和SSH1,可以非常方便的对Linux主机进行远程管理。除此之外,其还有丰富的外观配色方案以及样式选择。Xshell免费版官网下载地址https//www.xshell.com/zh/free-for-home-school/链接https//pan.baidu.com/s/1NJGWZHkByakOkQpKfkc7Yg。…

    2025年9月5日
    56

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号