先给出代码:
import java.io.*;
class BufferedReaderExample{
public static void main(String[] args) throws IOException{
BufferedReader d=new BufferedReader(new InputStreamReader(new FileInputStream("c:/write.txt")));
DataOutputStream o=new DataOutputStream(new FileOutputStream("c:/write.txt"));
String line;
while((line=d.readLine())!=null){
String a=(line.toUpperCase());
System.out.println(a);
o.writeBytes(a+"\r\n");
}
//System.out.println(line);
d.close();
o.close();
}

}
在C盘的write.txt文件中有一行数据,但是通过运行代码并没有把里面的这行数据变成大写,而是把文件里的数据清掉了,最后我发现是没有进入(while)循环,也就是说line一直是空的,但是文件中明明有一行数据,为什么用readLine方法读取不到呢?
请高手帮忙~!小弟谢过了

解决方案 »

  1.   

    我认为你的“c:/write.txt”应改为C:\\write.txt.因为在java中标识文件存在的位置不是"/"而是"\\".其余的我没看
      

  2.   

    BufferedReader d=new BufferedReader(new InputStreamReader(new FileInputStream("c:/write.txt")));
    DataOutputStream o=new DataOutputStream(new FileOutputStream("c:/write.txt"));
    因为是同一个文件所以出问题了,必须将原来的文件改名:    public static void main(String[] args) throws Exception {
            File file = new File("c:/write.txt");
            File filebak = new File("c:/write.txt.bak");
            file.renameTo(filebak);
            System.out.println(filebak);
            System.out.println(file);
            BufferedReader d = new BufferedReader(new InputStreamReader(
                    new FileInputStream(filebak)));
            DataOutputStream o = new DataOutputStream(new FileOutputStream(
                    file));
            String line;
            while ((line = d.readLine()) != null) {
                String a = (line.toUpperCase());
                System.out.println(a);
                o.writeBytes(a + "\r\n");
            }
            //System.out.println(line);
            d.close();
            o.close();    }
      

  3.   

    你的输入流和输出流是同一个文件导致的问题。可以换一个文件名,或者把读取的数据保存到list中,关闭输入流后在利用输出流写入list中的数据