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)
上一篇 2022年5月27日 上午7:20
下一篇 2022年5月27日 上午7:20


相关推荐

  • 新手入门:websocket

    新手入门:websocketWebSocket详解1、什么是Socket?什么是WebSocket?2、WebSocket的通信原理和机制3、WebSocket技术出现之前,Web端实现即时通讯的方法有哪些?4、一个简单的WebSocket聊天小例子8、结语1、什么是Socket?什么是WebSocket?对于第1次听说WebSocket技术的人来说,两者有什么区别?websocket是仅仅将socket的概念移植到浏览器中的实现吗?我们知道,在网络中的两个应用程序(进程)需要全双工相互通信(全双工即双方可同时向对方发送消息),

    2022年7月11日
    18
  • c语言函数使用的三个步骤,sqrt函数怎么使用 C语言中的sqrt函数使用方法介绍

    c语言函数使用的三个步骤,sqrt函数怎么使用 C语言中的sqrt函数使用方法介绍sqrt函数是什么函数?sqrt函数怎么使用呢?对于这两个问题,相信这是很多第一次看到该函数人最先想到的问题,当然这两个问题也是了解该函数最主要的方面。因此今天小编就来说一说在c语言当中sqrt函数是如何使用的。sqrt函数怎么使用——C语言中sqrt函数的使用方法C语言中sqrt函数是指的开方运算函数,得到的结果是函数变量(可以是数值,也可以是变量名)的算术平方根。在VC6.0中的math.h头…

    2022年5月1日
    133
  • navicat15for激活码-激活码分享

    (navicat15for激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月31日
    238
  • Cursor 进阶课程:从入门到精通的AI编程助手完全指南

    Cursor 进阶课程:从入门到精通的AI编程助手完全指南

    2026年3月16日
    2
  • 利用MATLAB读取NetCDF文件

    利用MATLAB读取NetCDF文件网络通用数据格式 NetCDF networkCommo 是在气象科学领域用于存储数据的常见存储标准 该格式将数据存储成扩展名为 nc 的文件 MATLAB Python 和 Java 等语言都支持对该类型文件的读取 本文主要讲述利用 MATLAB 读取 NetCDF 文件

    2026年3月17日
    2
  • latex参考文献为网址「建议收藏」

    latex参考文献为网址「建议收藏」1、引入hyperref包:\usepackage{hyperref }2、举例:@misc{A:2005,  author = {AlexanderTotok},  title = {hello },  howpublished = {\url{http://cs.nyu.edu/totok/professional/software/tpcw/tpcw.html}}

    2025年10月16日
    6

发表回复

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

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