数组)输入输出流"/>
Java(数据,字节数组)输入输出流
Java的数据输入输出流是非常强大的IO,使用它可以使我们的程序更加高效。它封装了很多的函数供我们使用。
import java.io.DataInputStream;
import java.io.DataOutputStream;
readInt();//读取一个整型
readChar();//读取一个字符
readFloat();//读取一个浮点数
readLine();//读取一行
readBoolean();//读取布尔型
readShort();//读取短整型
例如,我们可以直接通过以下方式读取一个整型数据
File file=new File("D:/io.txt");
FileInputStream fis=new FileInputStream(file);int ch1 = fis.read();//读取一个字节int ch2 = fis.read();int ch3 = fis.read();int ch4 = fis.read();int x=((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
也可以使用封装好的数据流
File file=new File("D:/io.txt");
FileInputStream fis=new FileInputStream(file);
DataInputStream dis=new DataInputStream(fis);
int x=dis.readInt();//直接读取一个整型数据
可以看出,使用了数据输入输出流的代码更加简洁,让人一目了然。事实上,DataInputStream.readInt()内部的实现方法就是像第一段代码写的那样,你可能会问,这两者之间的效率可能并没有什么差别,但是在我们读取更多不同类型数据的时候,两者之间的差距就显而易见了
使用这种方式,我们可以轻松的实现数据的读写
public class Objects {public int integer;public byte bytes;public String string;}
public class WriteFile {public static void main(String [] args) throws Exception {int total_num=10;List<Objects> oblist= new ArrayList<Objects>();for(int i=0;i<total_num;i++) {Objects e=new Objects();e.integer=i+1;e.bytes=(byte)(i+1);e.string="数据等于="+(char)(i+48);oblist.add(e); }//向文件中写入数据FileOutputStream fos=new FileOutputStream("C:\\Users\\14005\\Desktop\\test.txt");DataOutputStream dos=new DataOutputStream(fos);dos.writeInt(total_num);for(int i=0;i<total_num;i++) { dos.writeInt(oblist.get(i).integer);dos.write(oblist.get(i).bytes);dos.write(oblist.get(i).string.getBytes());dos.flush();}fos.close();dos.close();//从文件中读取数据File file=new File("C:\\Users\\14005\\Desktop\\test.txt");FileInputStream fis=new FileInputStream(file);DataInputStream dis=new DataInputStream(fis);int length=dis.readInt();for(int i=0;i<length;i++) {int integer=dis.readInt()-1;byte bytes=(byte)(dis.readByte()-1);byte[] b=new byte[10];dis.read(b);System.out.println(integer+" "+bytes+" "+new String(b));}fis.close();dis.close();}}
ByteArrayInputStream ByteArrayOutputStream
基本用法:
int LEN = 5;
byte[] ArrayLetters = {0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A};
private static void tesByteArrayInputStream() {// 创建ByteArrayInputStream字节流,内容是ArrayLetters数组ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters);// 从字节流中读取5个字节for (int i=0; i<LEN; i++) {// 若能继续读取下一个字节,则读取下一个字节if (bais.available() >= 0) {// 读取“字节流的下一个字节”int tmp = bais.read();System.out.printf("%d : 0x%s\n", i, Integer.toHexString(tmp));}}
参考链接 .htm
ByteArrayInputStream,ByteArrayOutputStream在转化BufferedImage与[]byte时非常方便
public byte[] send_imagebyte(BufferedImage i) {ByteArrayOutputStream bos = new ByteArrayOutputStream();bos.write(1);try {ImageIO.write(i, "jpg",bos);bos.close();} catch (IOException e) {e.printStackTrace();}byte[] bytes = bos.toByteArray();return bytes;}
public BufferedImage To_Image(byte[] buffer) {ByteArrayInputStream bis = new ByteArrayInputStream(buffer);BufferedImage recvImg = null;try {recvImg = ImageIO.read(bis);} catch (IOException e) {e.printStackTrace();}return recvImg;}
更多推荐
Java(数据,字节数组)输入输出流
发布评论