java bufferedwriter 编码,Java BufferedWriter对象与utf-8

java bufferedwriter 编码,Java BufferedWriter对象与utf-8IhavethefollowingcodeandIwanttomaketheoutputstreamuseutf-8.BasicallyIhavecharacterslikeéthatappearasésoitlookslikeanencodingissue.I’veseenlotsofexamplesthatuse…O…

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

java bufferedwriter 编码,Java BufferedWriter对象与utf-8

I have the following code and I want to make the outputstream use utf-8. Basically I have characters like é that appear as é so it looks like an encoding issue.

I’ve seen lots of examples that use…

OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(path),”UTF-8″);

My current code though is…

BufferedWriter out = new

BufferedWriter(new FileWriter(DatabaseProps.fileLocation + “Output.xml”));

Is it possible to define this object as UTF-8 without having to use the OutputStreamWriter?

Thanks,

解决方案

No. FileWriter doesn’t let you specify the encoding, which is extremely annoying. It always uses the system default encoding. Just suck it up and use OutputStreamWriter wrapping a FileOutputStream. You can still wrap the OutputStreamWriter in a BufferedWriter of course:

BufferedWriter out = new BufferedWriter

(new OutputStreamWriter(new FileOutputStream(path), StandardCharsets.UTF_8));

Or as of Java 8:

BufferedWriter out = Files.newBufferedWriter(Paths.of(path));

(Of course you could change your system default encoding to UTF-8, but that seems a bit of an extreme measure.)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • golang2020激活码【在线破解激活】

    golang2020激活码【在线破解激活】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日
    57
  • eclipse安装教程(win10版本,很全的)

    eclipse安装教程(win10版本,很全的)第一步:下载JDK。先给上下载链接:http://www.oracle.com/technetwork/java/javase/downloads/index.html之后根据自己的系统选择,x86代表32位,x64代表64位。点击相应的jdk下载。同意之后下载。(记住下载到哪,打开之后一路同意安装即可)记住你把Jdk安装到哪里,文件路径不要有中文,有时会无法识别,我是将jdk安装到D盘java文件夹下第二步:java环境变量配置。(这是第一种方法,还有第二种设置JAVA_HOME,个人推

    2022年6月13日
    66
  • 经纬度与距离的换算关系图_经纬度对应距离

    经纬度与距离的换算关系图_经纬度对应距离一、经纬度距离换算a)在纬度相等的情况下:经度每隔0.00001度,距离相差约1米;每隔0.0001度,距离相差约10米;每隔0.001度,距离相差约100米;每隔0.01度,距离相差约1000米;每隔0.1度,距离相差约10000米。b)在经度相等的情况下:纬度每隔0.00001度,距离相差约1.1米;每隔0.0001度,距离相差约11米;…

    2025年11月18日
    6
  • ifconfig详解_linux不能ifconfig

    ifconfig详解_linux不能ifconfig功能:ifconfig命令被用于配置和显示Linux内核中网络接口的网络参数。用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在。要想将上述的配置信息永远的存的电脑里,那就要修改网卡的配置文件了。语法ifconfig(参数)情况一:来源:https://man.linuxde.net/ifconfigadd<地址>:设置网络设备IPv6的ip地址;d…

    2025年7月11日
    3
  • PhpSpreadsheet_php标准输入流stdin

    PhpSpreadsheet_php标准输入流stdin实例化对象composer导包”phpoffice/phpspreadsheet”:”^1.11″,$spreadsheet=newSpreadsheet();$sheet=$spreadsheet->getActiveSheet();合并单元格$sheet->mergeCells(‘A1:J1’);设置单元格的内容$spreadsheet->getActiveSheet()->setCellValue(‘A2′,’序号’)-&

    2025年12月13日
    2
  • Web前端开发中的MCRV模式(转)

    Web前端开发中的MCRV模式(转)

    2021年7月9日
    92

发表回复

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

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