java中如何用使用输入输出流读写文件!
2个回答
展开全部
public static void main(String[] args) throws IOException {
FileOutputStream out1=new FileOutputStream("./src/test1/a.txt");
BufferedOutputStream out2=new BufferedOutputStream(out1);
DataOutputStream out=new DataOutputStream(out2);
out.write(-12);
out.write(12);
out.writeChar('1');
out.writeUTF("你好");
out.close();
InputStream in1=new FileInputStream("./src/test1/a.txt");
BufferedInputStream in2=new BufferedInputStream(in1);
DataInputStream in=new DataInputStream(in2);
System.out.println(in.readByte());
System.out.println(in.readLong());
System.out.println(in.readChar());
//System.out.println(in.readUTF(in));
in.close();
}
FileOutputStream out1=new FileOutputStream("./src/test1/a.txt");
BufferedOutputStream out2=new BufferedOutputStream(out1);
DataOutputStream out=new DataOutputStream(out2);
out.write(-12);
out.write(12);
out.writeChar('1');
out.writeUTF("你好");
out.close();
InputStream in1=new FileInputStream("./src/test1/a.txt");
BufferedInputStream in2=new BufferedInputStream(in1);
DataInputStream in=new DataInputStream(in2);
System.out.println(in.readByte());
System.out.println(in.readLong());
System.out.println(in.readChar());
//System.out.println(in.readUTF(in));
in.close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Reader r=new FileReader(file);
BufferedReader br=new BufferedReader(r);
String str=br.readLine();
while(str!=null){
txtContent.append(str+"\r\n");
str=br.readLine();
}
br.close();
r.close();
Writer w=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(w);
w.write(txtContent.getText());
bw.append(txtContent.getText());
wbw.close();
w.close();
BufferedReader br=new BufferedReader(r);
String str=br.readLine();
while(str!=null){
txtContent.append(str+"\r\n");
str=br.readLine();
}
br.close();
r.close();
Writer w=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(w);
w.write(txtContent.getText());
bw.append(txtContent.getText());
wbw.close();
w.close();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询