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. Springboot打包及远程调试

    如果您使用Spring Boot Maven或Gradle插件来创建可执行jar,则可以使用来运行您的应用程序java -jar,如以下示例所示: $ java -jar target / myapp ...

  2. 搭建使用PHPstorm环境

    本周学习内容: 1.学习PHP: 2.复习技能表: 3.学习正则表达式: 实验内容: 1.安装PHPstorm环境,破解PHPstorm: 2.PHPstorm运行PHP代码 3.PHPstorm安装 ...

  3. RookeyFrame 模块 线上创建的模块 迁移到 线下来

    1. 把线上创建的model,写在项目的model层里面. 把文件 Rookey.Frame.Web\Config\TempModel\Order_File.code 复制到model层, 用文本编辑 ...

  4. 开始编写Makefile(二)Makefile变量的使用

    Makefile可以使用变量代替 命令行:make -f Makefile2 说明开始make一个名为Makefile2的文件 ###############定义变量################# ...

  5. flutter 省市区选择器 city_pickers 的简单实用

    Github地址:https://github.com/hanxu317317/city_pickers packages地址: https://pub.flutter-io.cn/packages/ ...

  6. golang 斐波那契数

    golang 斐波那契数 package main import "fmt" /* 斐波那契数,亦称之为斐波那契数列(意大利语: Successione di Fibonacci) ...

  7. 【模板】最小割树(Gomory-Hu Tree)

    传送门 Description 给定一个\(n\)个点\(m\)条边的无向连通图,多次询问两点之间的最小割 两点间的最小割是这样定义的:原图的每条边有一个割断它的代价,你需要用最小的代价使得这两个点不 ...

  8. VS2019输出信息到调试控制台

    System.Diagnostics.Debug.WriteLine(format, args);

  9. elasticsearch_dsl.exceptions.ValidationException: You cannot write to a wildcard index.

    elasticsearch_dsl.exceptions.ValidationException: You cannot write to a wildcard index. 这里是因为版本不匹配的问 ...

  10. ubuntu系统五笔输入法安装

    转载:https://jingyan.baidu.com/article/454316ab67d702f7a7c03a1a.html Ubuntu 16.04 在安装时选择中文安装,安装过程中将自动安 ...