zoukankan      html  css  js  c++  java
  • 读写文件

    package com.pro;

    import java.io.*;

    public class Pro {

    public static void main(String[] args) throws IOException {

    File f = new File("a.txt");
    FileOutputStream fop = new FileOutputStream(f);
    // 构建FileOutputStream对象,文件不存在会自动新建

    OutputStreamWriter writer = new OutputStreamWriter(fop, "UTF-8");
    // 构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk

    writer.append("中文输入");
    // 写入到缓冲区

    writer.append(" ");
    // 换行

    writer.append("English");
    // 刷新缓存冲,写入到文件,如果下面已经没有写入的内容了,直接close也会写入

    writer.close();
    // 关闭写入流,同时会把缓冲区内容写入文件,所以上面的注释掉

    fop.close();
    // 关闭输出流,释放系统资源

    FileInputStream fip = new FileInputStream(f);
    // 构建FileInputStream对象

    InputStreamReader reader = new InputStreamReader(fip, "UTF-8");
    // 构建InputStreamReader对象,编码与写入相同

    StringBuffer sb = new StringBuffer();
    while (reader.ready()) {
    sb.append((char) reader.read());
    // 转成char加到StringBuffer对象中
    }
    System.out.println(sb.toString());
    reader.close();
    // 关闭读取流

    fip.close();
    // 关闭输入流,释放系统资源

    }
    }

  • 相关阅读:
    Centos7,PHP7安装swoole
    安装最新LAMP环境(CentOS7+PHP7.1.5+Mysql5.7)
    PHP7性能提升原因
    Git 图文教程
    centos下安装mongodb和php的mongo扩展
    linux如何把普通用户添加到sudo组
    Linux常用的三种软件安装方式
    PHP几个常用的概率算法
    java面向对象知识(上)
    linux中tar命令用法
  • 原文地址:https://www.cnblogs.com/sunny-sl/p/7504266.html
Copyright © 2011-2022 走看看