包含java读txt乱码的词条

博主:adminadmin 2022-12-19 11:36:07 63

本篇文章给大家谈谈java读txt乱码,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 写入txt文件的中文乱码是怎么回事?

原因是写入时使用的字符编码和期望的不一致导致的。

java工作区统一编码。统一为utf-8

这个就是输出流的编码问题吧?如果你在输出时指定好具体的编码,或者说指定跟原网页一直的编码。

如果你期望写到文件中的汉字是 GBK编码,可以写文件时,将字符串准换成 GBK编码的byte[]。

网页编码和输出流编码一致。

用java读取txt文件中的中文写入数据库出现中文乱码,怎么解决?

1,设置下你数据库的编码,一般设置成UTF-8或者GBK或者GB2312

2,java读取TXT数据时可以转换下编码,再存进数据库,从数据库读取出来后显示时也同样的转换下编码,好像可以用个过滤器来弄的,

java读取txt文件时候出现乱码

乱码是因为编解码不匹配造成的。

请重新打开并保存txt文件,保存时选择编码方式为:ANSI。如果使用别的文本编辑器,保存内容时请选择GBK或GB2312。

java读取txt中一行中文时出现的是乱码,怎么能显示出中文??代码如下:

又是你啊 ,上午已经回答了你一个问题了。你的编码格式设置下 就可以了。

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStreamReader;

import java.io.RandomAccessFile;

public class Random {

 public static void main(String[] args) throws Exception {

  int lineCount = 0;

  File f = new File("E:\\数学3班.txt");

  RandomAccessFile raf = new RandomAccessFile(f, "r");

  java.util.Random r1 = new java.util.Random();

  BufferedReader buffer = new BufferedReader(new InputStreamReader(

    new FileInputStream(f)));

  while (buffer.readLine() != null) {

   lineCount++;

  }

  int a = r1.nextInt(lineCount);

  int i = 0;

  while (raf.readLine() != null) {

   i++;

   if (i == a) {

    String temp = raf.readLine();

    System.out

      .println(new String(temp.getBytes("iso8859-1"), "GBK"));

   }

  }

 }

}

java字节流读txt文件出现乱码怎么解决?

用字节流读包含中文的文件出现乱码是不可避免的,简单的想想:单第一个字为英文,第二个子为中文,而一个英文占1一个字节,一个中文占两个字节,当你用两个字节的的数组读取,中文字就会被拆分,这样就肯定会出现乱码。

给你个解决方法,我用的JDK1.5,完全解决了你的需求,代码如下:

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

public class Start{

public static void main(String[] args){

File fileS = new File("E:/123.txt");

if(!fileS.exists()){

System.out.println ("找不到指定文件");

return;

}

FileInputStream fileIS = null;

try{

fileIS = new FileInputStream(fileS);

byte[] byt = new byte[2];

int data = fileIS.read(byt);

for (; data != -1 ; ){

int a=byt[0];

int b=byt[1];

if((a = 0 a = 127) (b 127 || b 0)){

System.out.print ((char)a);

data = fileIS.read();

byt[0] = (byte)b;

byt[1] = (byte)data;

}

String str = new String(byt);

System.out.print (str);

data = fileIS.read(byt);

}

System.out.println ();

}catch(FileNotFoundException ex){

ex.printStackTrace();

}catch(IOException ex){

ex.printStackTrace();

}finally{

if (fileIS != null) {

try{

fileIS.close();

}catch(IOException ex){

ex.printStackTrace();

}

}

}

}

}

java读txt乱码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、java读txt乱码的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-19,除非注明,否则均为首码项目网原创文章,转载请注明出处。