简介:如题
import java.io.FileInputStream; import java.io.FileOutputStream; /** * @author czchina * */ public class TestStream { public static void main(String[] args) { FileInputStream fls = null; FileOutputStream fos =null; try{ fls = new FileInputStream("E:/Android/AndroidStudioProjects/text.txt"); fos = new FileOutputStream("E:/Android/AndroidStudioProjects/store.txt"); //生成一个字节数组 byte [] buffer= new byte [500]; int num = 0; int tmp = 0; int tmp1 = 0; for(int i=0;i<3;i++){ tmp = fls.read(buffer,0,buffer.length); if(-1 == tmp){ System.out.print("Read complete ! \n"); break; } //打印buffer String bufferToString = new String(buffer); bufferToString = bufferToString.trim(); System.out.println("Print bufferToString:\n"+bufferToString+"\n"); tmp1 = tmp; if(500 == tmp1){ num++; tmp1 = 0; } fos.write(buffer,0,tmp); } //流的字节数 num = num*500+tmp1; System.out.println("num: "+num); } catch(Exception e){ System.out.println(e.toString()); } } }
输入流文本:
输出流文本:
可见:流的输入与输出都没有问题。
以下是console打印:
如上图:
1、问题:红圈内文本打印了两次,原因不明‘
2、num计数正确。
时间: 2024-10-23 00:04:51