流读取导致StringBuilder.toString()乱码的问题及解决
作者:Andrew_Yuan 发布时间:2022-12-20 13:34:14
流读取导致StringBuilder.toString()乱码
乱码问题
StringBuilder sb = new StringBuilder();
String s = new String(sb.toString().getBytes("ios8859-1"),"UTF-8");
顺便回忆一下String, StringBuilder, StringBuffer直接的区别:
String对象每次追加字符串的时候都是创建了新的对象,消耗时间最长,性能最低,操作少量数据的时候可以用它;
StringBuilder和StringBuffer每次追加的都是同一个对象,消耗的时间短,性能优良;
StringBuilder比StringBuffer更快一点,因为StringBuffer是线程安全的,支持同步锁,而StringBuilder是线程不安全的,所以在单线程的时候最好使用StringBuilder,多线程的时候则使用线程安全的StringBuffer。
Java StringBuilder toString()方法与示例
StringBuilder类的toString()方法 (StringBuilder Class toString() method)
toString() method is available in java.lang package.
toString()方法在java.lang包中可用。
toString() method is used to represent string denoted by this object (when we create a new string object so first it is created and instantiated to contain the data[set of characters] denoted by this object currently).
toString()方法用于表示此对象表示的字符串(当我们创建一个新的字符串对象时,首先创建并实例化它包含当前由该对象表示的data [字符集])。
toString() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
toString()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
toString() method does not throw an exception at the time of string representation.
toString()方法在字符串表示时不会引发异常。
Syntax:
句法:
public String toString();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of this method is String, it returns the string denotation of this set of characters represented by this object.
此方法的返回类型为String ,它返回此对象表示的这组字符的字符串表示形式。
Example:
例:
// Java program to demonstrate the example
// of String toString() method of StringBuilder
public class ToString {
public static void main(String[] args) {
// Creating an StringBuilder object
StringBuilder st_b = new StringBuilder("Java ");
// By using append() method is to append the given string to
// st_b object
st_b.append("World");
// By using toString() method is to represent st_b
// object to String
System.out.println("st_b.toString() = " + st_b.toString());
}
}
Output
输出量
st_b.toString() = Java World
来源:https://blog.csdn.net/Andrew_Yuan/article/details/89349750


猜你喜欢
- 一、简介在上篇 ElasticSearch 文章中,我们详细的介绍了 ElasticSearch 的各种 api 使用。实际的项目开发过程中
- 1. 插入排序步骤:1.从第一个元素开始,该元素可以认为已经被排序2.取下一个元素tem,从已排序的元素序列从后往前扫描3.如果该元素大于t
- 关于Android的自定义控件,之前也写了两个,一个是简单地继承View,另一个通过继承Layout实现一个省市联动控件。这篇,将通过继承V
- 一, eclipse springboot打war包1. 配置pom.xml文件<packaging>war</packa
- 前言前段时间看到一道面试题:“main函数可以被重载么?”,当时就蒙圈了,怎么还会有这种面试题,现在
- 客户端的代码:package tcp.http;import java.io.*;import java.net.*;import java
- try就像一个网,把try{}里面的代码所抛出的异常都网住,然后把异常交给catch{}里面的代码去处理。最后执行finally之中的代码。
- 基本概念Spring Validation 验证框架对参数的验证机制提供了@Validated(Spring's JSR-303规范
- 老规矩,先上图看效果。说明TextView的跑马灯效果也就是指当你只想让TextView单行显示,可是文本内容却又超过一行时,自动从左往右慢
- 安卓系统本身可以很简便的实现分享功能,因为我们只需向startActivity传递一个ACTION_SEND的Intent,系统就为我们弹出
- 代码如果不进行格式化的处理,那么在查阅上会浪费不少的时间。今天我们要说的是字符串的格式化处理,作为基础编程内容,相信大家都字符串都不陌生。我
- 在线程间通信方式中,我们了解到可以使用Semaphore信号量来实现线程间通信,Semaphore支持公平锁和非公平锁,Semaphore底
- 本篇主要讲解如何使用Ideal 搭建Spring的源码环境,想必大家都会多多少少去看过Spring的部分源码,一般我们都是直接点进某个Spr
- Android客户端请求服务器端的详细解释1. Android客户端与服务器端通信方式: Android与服务器通信通常采用HTTP通信方式
- 1.概述数据库开发一直是JAVA开发的核心之一,作为现在JAVA EE的基石框架,Spring Boot自身携带了一个JDBCTemplat
- 本文实例讲述了Java编程实现向文本文件中读取数据之Scanner用法。分享给大家供大家参考,具体如下:使用Scanner类来读取文件我们使
- 前言Kotlin并没有想象中的那么牛逼哄哄,也并不难,我更喜欢把他看做一枚语法糖,所谓的语法糖就是:能够让代码变得更加简单易读的辅助工具。而
- 为什么要给图片添加水印为图片添加水印的主要作用是保护图片版权,防止图片被未经授权的人使用或传播。通常情况下,图片水印会包含图片作者的名字、版
- C# httpwebrequest访问HTTPS链接时遇到这个错误,但是如果我开抓包工具,比如filddler2,则POST返回正常错误提示
- 今天上班中午吃饱之后、逛博客溜达看到一道题:数组反转 晚上回家洗完澡没事情做,就自己练习一把。public static cla