字节流读数据
一次读一个字节的数据
FileInputStream:从文件系统中的文件获取输入字节
public class FileInputStreamDemo{
public static void main(String[] args) throws IOException{
FileInputStream fis=new FileInputStream("myByteStream\\fos.txt");
int by=fis.read();
Syatem.out.println(by);//ASCII
System.out.println((char)by);
fis.close();
}
}
注意文件末尾的返回值是**-1**
一次读一个字节数组的数据
public class FileInputStreamDemo{
public static void main(String[] args) throws IOException{
FileInputStream fis=new FileInputStream("myByteStream\\fos.txt");
byte[] bys=new byte[5];
int len=fis.read(bys);
System.out.println(new String(bys,0,len));
fis.close();
}
}
字节流复制图片
public class CopyJpgDemo{
public static void main(String[] args) throws IOException{
FileInputStream fis=new FileInputStream("myByteStream\\in.jpg");
FileOutputStream fis=new FileInputStream("myByteStream\\out.jpg");
byte[] bys=new byte[1024];
int len;
while((len=fis.read(bys))!=-1){
fos.write(bys,0,len);
}
fis.close();
fos.close();
}
}