就是一个很简单的小程序,要读入txt中的文本。程序如下:package lesson;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class c {
static String str;static char[] charnum=new char[1024];
static int i=0;
 
public static void main(String[] args) {
    File aFile = new File("D:/Java/example.txt");
    FileInputStream inFile = null;
        
    try {
      inFile = new FileInputStream(aFile);      } catch(FileNotFoundException e) {
      e.printStackTrace(System.err);
      System.exit(1);
    }
        
    FileChannel inChannel = inFile.getChannel();    
    ByteBuffer buf = ByteBuffer.allocate(1024);    
    try {
    
      while(inChannel.read(buf) != -1) {        
      str= ((ByteBuffer)(buf.flip())).asCharBuffer().toString();
               System.out.println(str);             
               System.out.println(new String(str.getBytes(),"UTF-8"));                             
      buf.clear();                    
      }      
      inFile.close();                 
    } catch(IOException e) {
      e.printStackTrace(System.err);
      System.exit(1);
    }
     }
}现在的状况就是txt读取全部是乱码。。
“???????????獯潯潯潯潯潯潯潯潯漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????漪?????????潯漪??????????漪??????????漪??????????漪??????????漪??????????漪??????????攪???????????”
我想问下大家怎么修改这个程序能让它正确读出文本?
谢谢!!!

解决方案 »

  1.   

    编码问题。
    不知道行不行。用文本编译器打开。之后改一下编码方式。改成utf-8再读试试。
    我没试验。不知道行不行哈
      

  2.   


    System.out.println(new String(str.getBytes(),"UTF-8"));
    //字符集编码问题,换成系统默认的试试
      

  3.   

    FileInputStream是按字节来读的  但是汉字一次占两个字节,所以会有乱码,用FileReader来读,它是按字符读的。
      

  4.   


    package lesson;import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    public class c {
    static String str;static char[] charnum=new char[1024];
    static int i=0;
      
    public static void main(String[] args) {
    File aFile = new File("D:/Java/example.txt");
    FileInputStream inFile = null;
      
    try {
    inFile = new FileInputStream(aFile);  } catch(FileNotFoundException e) {
    e.printStackTrace(System.err);
    System.exit(1);
    }
      
    FileChannel inChannel = inFile.getChannel();   
    ByteBuffer buf = ByteBuffer.allocate(1024);   
    try {
      
      while(inChannel.read(buf) != -1) { 
    str= new String(buf.array(), "gbk"); 
      System.out.println(str);   
    buf.clear(); 
    }   
    inFile.close();   
    } catch(IOException e) {
    e.printStackTrace(System.err);
    System.exit(1);
    }
      } 
    }
      

  5.   

    UTF-8的吗?读的时候需要注意文件存的时候有没有BOM编码,如果有要跳过。
    另外下面的转码有点乱,转成CharBuffer时会有字符补齐的情况,导致再转成
    byte[]时会和文件中的编码不一样。是否可以试着用ByteBuffer.array()直接转成byte[] while(inChannel.read(buf) != -1) {
    str= ((ByteBuffer)(buf.flip())).asCharBuffer().toString();
      System.out.println(str);   
      System.out.println(new String(str.getBytes(),"UTF-8")); 
      

  6.   

    先看一下你的txt是什么编码的
      

  7.   

    windows的默认编码是GBK,你转成了UTF-8肯定是乱码了!
      

  8.   

    用BufferedReader类的对象去读
    这个是按字符去读
      

  9.   

    JAVA和Window有一个编码.是什么忘了你最好找找