为什么这不行?import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Tester { public static void main(String[] args) {
FileReader fr;
try {
File file = new File("text.txt");
if(file.exists()==false){
file.createNewFile();
}
fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String s;
while((s=br.readLine())!=null){
byte[] b = s.getBytes("iso-8859-1");
s = new String(b);
System.out.println(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}}

解决方案 »

  1.   

    中文显示不出,
    我用eclipse
      

  2.   

    byte[] b = s.getBytes("iso-8859-1");
    改成byte[] b = s.getBytes();
      

  3.   

    那看看你txt文件的编码方式呢
      

  4.   

     byte[] b = s.getBytes("iso-8859-1");
     s = new String(b);这两句本没有多大的意义的,可以去掉。
      

  5.   

    byte[] b = s.getBytes("gb2312");
      

  6.   


    /**
         * 一行一行的读取数据
         * @param filePath
         * @param fileName
         * @throws IOException
         */
        public void readLineFile(String filePath,String fileName)throws IOException
        {
        
         FileReader fr=new FileReader(filePath+fileName);
         BufferedReader br=new BufferedReader(fr);
         String line=br.readLine();
         while(line!=null)
         {
         System.out.println(line);
         line=br.readLine();
         }
         br.close();
         fr.close();
        }我也不知道楼主是不显示呢,还是现实乱码呢!
      

  7.   


    public void readLineFile(String filePath,String fileName)throws IOException
        {
         File file=new File(filePath+fileName);
         InputStreamReader read = new InputStreamReader (new FileInputStream(file),"UTF-8");
         BufferedReader br=new BufferedReader(read);
         String line=br.readLine();
         while(line!=null)
         {
         System.out.println(line);
         line=br.readLine();
         }
         br.close();
         read.close();
        }继续试验下,先制定流的编码格式,在读取
      

  8.   

    package com.stream;import java.io.RandomAccessFile;class StringReaderTest {
      public static void main(String[] args) {
        try {
          RandomAccessFile raf = new RandomAccessFile("text.txt", "r");
          String str;
          while ((str = raf.readLine()) != null) {
            String strTemp = new String(str.getBytes("ISO-8859-1"), "UTF-16");
            System.out.println(strTemp);
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
      

  9.   

    package com.stream;import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;public class Tester {  public static void main(String[] args) throws Exception {
        File file = new File("text.txt");
        InputStreamReader read =
            new InputStreamReader(new FileInputStream(file), "UTF-16");
        BufferedReader br = new BufferedReader(read);
        String line = br.readLine();
        while (line != null) {
          System.out.println(line);
          line = br.readLine();
        }
        br.close();
        read.close();
      }
    }
    这个可以了。
      

  10.   

    7楼正解:
    把修改后的代码再给你发一下吧:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    public class Tester {    public static void main(String[] args) {
            FileReader fr;
            try {
                File file = new File("text.txt");
                if(file.exists()==false){
                    file.createNewFile();
                }
                fr = new FileReader(file);
                BufferedReader br = new BufferedReader(fr);
                String s;
                while((s=br.readLine())!=null){
                    //byte[] b = s.getBytes("iso-8859-1");这两句话完全多余,删除!
                    //s = new String(b);
                    System.out.println(s);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }}
      

  11.   

    你的中文乱码吗,是不是你的txt里存储的是对想,那你用readObject就可以读了
      

  12.   

    是window xp英文操作系统.有关系没有?
      

  13.   

    前面那些没问题的话,就是你Eclipse编码设置有问题了
    你看看是不是没设置好
      

  14.   

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;public class ReadFile
    { /**
     * @param args
     */
    public static void main(String[] args)
    {
    File file = new File("test1.txt");
    try
    {
    BufferedReader in = new BufferedReader(new FileReader(file));
    String s;
    s = in.readLine();
    while(s!=null)
    {
    System.out.println("Read:" + s);
    s= in.readLine();
    }
    in.close();
    }
    catch(FileNotFoundException e)
    {
    System.err.println("File not found:" + file);
    }
    catch(IOException e2)
    {
    e2.printStackTrace();
    }
    }
    }
      

  15.   

    text.txt 需要在指定位置,如果取不到该文件,他会新建一个新的txt,新的txt当然都是新的!
      

  16.   


    import java.io.*;public class FileReaderDemo {
    public static void main (String[] args) {

    FileReaderDemo t = new FileReaderDemo();
    t.readMyFile();
    }

    void readMyFile() {

    String record = null;
    int recCount = 0;

    try {
    FileReader fr = new FileReader("XX.txt");

    BufferedReader br = new BufferedReader(fr);

    record = new String();

    while ((record = br.readLine()) != null) {
    recCount++;
    System.out.println("Line" + recCount + ": " + record);
    }
    br.close();
    fr.close();
    } catch (IOException e) {
    e.printStackTrace();

    }
    }