读写txt的方式

来源:互联网 发布:formtalk 阿里云 编辑:程序博客网 时间:2024/05/02 06:12

//字符的方式读取txt 

BufferedReader reader=new BufferedReader(new FileReader("d://t.txt")); 

String line=""; 

while((line=reader.readLine())!=null){ 

    System.out.println(str); 

//字节的方式读取txt 

BufferedInputStream in=new BufferedInputStream(new FileInputStream("d://t.txt")); 

BufferedOutputStream out=new BufferedOutStream(new FileOutInputStream("d://tt.txt")); 

byte[] buff=new byte[1024]; int hasRead=0; 

while((hasRead=in.read(buff,0,1024))!=-1){ 

      out.write(buff,0,hasRead); 

      out.flush(); 

//写txt 

String str="welcome to china"; 

byte[] buff=new byte[255]; 

buff=str.getBytes(); 

OutputStream out=new OutputStream(new File("d://t3.txt")); 

out.write(buff,0,buff.length); 

out.flush();

0 0
原创粉丝点击