java输出为什么有乱码?用java为什么输出了之后不是文字而是乱码
很多朋友对于java输出为什么有乱码和用java为什么输出了之后不是文字而是乱码不太懂,今天就由小编来为大家分享,希望可以帮助到大家,下面一起来看看吧!
用java为什么输出了之后不是文字而是乱码
出现问题的原因是字节流中每个汉字是两个字节,所以肯定会出现乱码的,要将字节流通过BufferedReader变成按行读取的就可以了。
BufferedReader bre= null;
try{
String file="D:/test/test.txt";
bre= new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流
while((str= bre.readLine())!= null)//判断最后一行不存在,为空结束循环
{
System.out.println(str);//原样输出读到的内容
};
备注:流用完之后必须close掉,如上面的就应该是:bre.close(),否则bre流会一直存在,直到程序运行结束。
java中的输出string字符串,是乱码
同学,这个不是乱码。
数组本身是没有toString()方法的。
你这里有个默认的调用 Object.toString()
Object中的toString()方法,是将传入的参数的类型名和摘要(字符串的hashcode的十六进制编码)返回。
也就是说,你直接对数组使用了toString()方法,就会得到一个Ljava.lang.String;@175d6ab
其中,Ljava.lang.String是指数据是String类型的,175d6ab是摘要了
你要想得到你要的类型,需要使用循环的方法来输出数组中的每一个值。
下面是jdk中关于toString()方法的注释:
/**
* Returns a string representation of the object. In general, the
*{@code toString} method returns a string that
*"textually represents" this object. The result should
* be a concise but informative representation that is easy for a
* person to read.
* It is recommended that all subclasses override this method.
*<p>
* The{@code toString} method for class{@code Object}
* returns a string consisting of the name of the class of which the
* object is an instance, the at-sign character `{@code@}', and
* the unsigned hexadecimal representation of the hash code of the
* object. In other words, this method returns a string equal to the
* value of:
*<blockquote>
*<pre>
* getClass().getName()+'@'+ Integer.toHexString(hashCode())
*</pre></blockquote>
*
*@return a string representation of the object.
*/
Object中的toString()方法实现:
public String toString(){
return getClass().getName()+"@"+ Integer.toHexString(hashCode());
}
JAVA输出是乱码
windows控制台cmd乱码的解决办法
我本机的系统环境:
OS Name: Microsoft Windows 10企业版
OS Version: 10.0.14393 N/A Build 14393
有时在cmd中输出的中文乱码
解决办法如下:
在cmd中输入 CHCP 65001
按Enter键
然后查看不再乱码
注:CHCP是一个计算机指令,能够显示或设置活动代码页编号。
代码页描述
65001 UTF-8代码页
950繁体中文
936简体中文默认的GBK
437 MS-DOS美国英语
但是通过CHCP设置编码是治标不治本的
想永久的更改cmd编码值需要修改注册表
方法一:
在运行中通过regedit进入注册表
找到HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe
新建一个 DWORD(32位值),命名为CodePage,值设为65001
方法二:
我更喜欢这样:
新建一个cmd.reg
内容输入如下:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe]
"CodePage"=dword:0000fde9
"FontFamily"=dword:00000036
"FontWeight"=dword:00000190
"FaceName"="Consolas"
"ScreenBufferSize"=dword:232900d2
"WindowSize"=dword:002b00d2
如图:
保存之后,双击cmd.reg即可。
cmd.reg我在csdn也放了一份:http://download.csdn.net/detail/taoshujian/9770251
java控制台输出乱码
产生原因:因为这个开源项目的默认字符编码不对,所以控制台的字符编码也自动变成了UTF-8,而键盘的输入流的默认格式是GBK格式,这样就造成了在GBK转UTF-8的过程中产生的奇数乱码错误(这个问题的解释可以在搜索引擎找到)。
解决办法:
1.在代码区域右键-> run as-> run configurations-> common(右侧)-> console encoding
出现此错误,此时的编码格式应该是UTF-8,选择Other,这时可能没有GBK选项,没有,则执行之后操作。
2.更改该项目的文本文件编码,项目右键-> properties-> resource->先将 text file encoding调整回GBK,然后再回去重新设置console encoding编码为GBK。
java输出为什么有乱码和用java为什么输出了之后不是文字而是乱码的问题分享结束啦,以上的文章解决了您的问题吗?欢迎您下次再来哦!