?File file=new File("D:/demo.txt");??DataOutputStream? dos=new DataOutputStream(new FileOutputStream(file));????? dos.writeChars("世界");??DataInputStream dis=new DataInputStream(new FileInputStream(file));?? String s=null;???? while(dis.readChar()!=-1){????? s+=dis.readChar();????? ???? };?? System.out.println(s);
1 回答

qq_歲月靜好與君語_0
TA貢獻15條經(jīng)驗 獲得超2個贊
你的while后面不應該加分號,去掉,就可以了
有可能以后會出現(xiàn)的錯:
你的file有可能不存在,應該對其進行判斷,
if(!file2.exists()){
try{ ? ? ? ? ? ?//運行時異常
file2.createNewFile();
}catch(Exception e){
e.printStackTrace();
}
}
或者捕獲異常,像這樣,
try{
File file=new File("D:/demo.txt");
??DataOutputStream? dos=new DataOutputStream(new FileOutputStream(file));
????? dos.writeChars("世界");
??DataInputStream dis=new DataInputStream(new FileInputStream(file));
?? String s=null;
???? while(dis.readChar()!=-1){
????? s+=dis.readChar();
??????
???? }
}catch(Exception e){
}
添加回答
舉報
0/150
提交
取消