package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter; public class FileHelper { public static void readFile(File file) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} public static void writeFile(File file, String content) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
osw.write(content);
osw.flush();
} public static void appendFile(File file, String content) throws IOException {
OutputStreamWriter out = new OutputStreamWriter(
new FileOutputStream(file, true), // true to append
"UTF-8"
);
out.write(content);
out.close();
} // main for test
public static void main(String[] args) throws IOException {
File file = new File("D:\\test.txt");
writeFile(file, "");
appendFile(file, "你好");
appendFile(file, "!!!");
readFile(file);
}
}

Java以UTF-8格式读写及追加写文件示例的更多相关文章

  1. shell脚本实现覆盖写文件和追加写文件

    1.覆盖写文件 ">" date  > not_append_file.txt

  2. python 用类方法和静态方法实现是追加写文件内容,和读指定行号的内容

    用类方法和静态方法实现:一个是追加写文件一行内容,一个是读指定行号的内容   #coding=utf-8   class handle_file(object):     def __init__(s ...

  3. java调c# exe 程序,exe里写文件问题

    应用场景描述: java web程序,触发 调用c#写的后台exe程序,发现exe里写的文件找不到.单独在cmd命令行下执行exe没问题: 问题查找: 由于exe里获取文件路径错误导致: 解决方法: ...

  4. 总结java中创建并写文件的5种方式

    在java中有很多的方法可以创建文件写文件,你是否真的认真的总结过?下面笔者就帮大家总结一下java中创建文件的五种方法. Files.newBufferedWriter(Java 8) Files. ...

  5. 写文件的工具类,输出有格式的文件(txt、json/csv)

    import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io. ...

  6. Java开发笔记(八十七)随机访问文件的读写

    前面介绍了字符流读写文件的两种方式,包括文件字符流和缓存字符流,但是它们的写操作都存在一个问题:不管是write方法还是append方法,都只能从文件开头写入,而不能追加到文件末尾或者在文件中间某个位 ...

  7. 如何使用java代码进行视频格式的转换(FLV)

    如何使用java代码进行视频格式的转换(FLV) 一,前言 在给网页添加视频播放功能后,发现上传的视频有各种格式,那么就需要将他么转换成FLV,以很好的支持在线视频播放. 公司一直在使用中,配合使用, ...

  8. java写文件时往末尾追加文件(而不是覆盖原文件),的两种方法总结

    代码如下: import java.io.FileWriter; import java.io.IOException; import java.io.RandomAccessFile; public ...

  9. Java 读写文件示例

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class T ...

随机推荐

  1. NISP二级笔记(二) 信息安全管理体系

  2. 【CSS】div

    一.div內容溢出 .remark-div { overflow: auto; height: auto; max-height: 100px; } 1.溢出 overflow :auto时,内容超过 ...

  3. matlab 万能实用的非线性曲线拟合方法

    ——转载网络 在科学计算和工程应用中,经常会遇到需要拟合一系列的离散数据,最近找了很多相关的文章方法,在这里进行总结一下其中最完整.几乎能解决所有离散参数非线性拟合的方法 第一步:得到散点数据 根据你 ...

  4. 模板 - 数学 - 数论 - 扩展Euler定理

    费马(Fermat)小定理 当 \(p\) 为质数,则 \(a^{p-1}\equiv 1 \mod p\) 反之,费马小定理的逆定理不成立,这样的数叫做伪质数,最小的伪质数是341. 欧拉(Eule ...

  5. UMD、CommonJS、ES Module、AMD、CMD模块的写法

    AMD异步模块规范 RequireJS就是AMD的一个典型的实现. 以下是一个只依赖与jQuery的模块代码: // foo.js define(['jquery'], function($){ // ...

  6. thymeleaf错误解决办法

    Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "username&q ...

  7. ubuntu之路——day10.3 train/dev/test的划分、大小和指标更新

     train/dev/test的划分 我们在前面的博文中已经提到了train/dev/test的相关做法.比如不能将dev和test混为一谈.同时要保证数据集的同分布等. 现在在train/dev/t ...

  8. strace命令 二

    让我们看一台高负载服务器的 top 结果: top 技巧:运行 top 时,按「1」打开 CPU 列表,按「shift+p」以 CPU 排序. 在本例中大家很容易发现 CPU 主要是被若干个 PHP ...

  9. graph embedding 之 struc2vec

    在现实的网络中,构成网络的每个节点可能在网络中担任着某种角色.比如社交网络中,经常可以看见一些关注量很高的大V.两个大V在网络中的角色可能相同,因为他们都有很高的关注量:而大V与普通人(仅有几个关注) ...

  10. you are not allowed to push code to protected branches on this project(转)

    .. 图 1-1 报错:failed to push some refs to 'http://*******.git'. 一痛瞎踅摸之后,远程控制电脑,在H电脑上,重新建立了一个test项目,之后走 ...