先给出代码:
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方法读取不到呢?
请高手帮忙~!小弟谢过了
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方法读取不到呢?
请高手帮忙~!小弟谢过了
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(); }