流。FileInputStream,DataOutputStream,ByteArrayInputStream,RandomAccessFile

IO流

流向分类:
输入流,输出流

来源分类:
文件流,内存流,网络流,控制台流(system.in,system.out)

操作单元分类:
字节流,字符流

设计模式分类: 基础流,包装流

FileInputStream
输入流,文件流,字节流

FileInputStream(“test.txt”);//字节流
BufferInputStreamReader(fis,10);//字节流
InputStreamReader();//字符流
BufferReader();//字符流

fis.skip();//表示跳过多少个字节

Integer.toBinaryString(int b)//转化为二进制

Integer.parseInt(int s1,int radix); radix :转为几进制
new BufferReader(new InputStreamReader(new FileInputStream(“test.txt”),”UTF-8”));
BufferInputStreamReader(fis,10); 缓存字节流, 10代表缓存的字节数。

FileOuputStream

流的声明写在try catch外边
catch结尾写finally{
try{
if(fos!=null){fos.close()}
if(bos!= null){bos.close()}
}catch(){}

}

// 1.7之后自己写的类实现AutoCloseable就可以在try(这里写入,后边就会自动调用close方法){}

DataOutputStream

包装类,写入基本类型以及String类型
dos.writeInt(); 写入整形数据
dos.writeUTF();存入字符串,此时如果只写字符串的话前边会有一点乱码,因为会先读取字符串的长度,然后写入开头
DateInputStream
dis.readInt();读取输入的整形数据,
读取时必须按顺序来读取相应的类型

ByteArrayInputStream
内存流

public class TestByteInput {
public static void main(String[] args) {
byte[] bytes = {0, 0, 1, 2};
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));
try {
System.out.println(dis.readInt());
} catch (IOException e) {
e.printStackTrace();
}
}
}

运行结果:258

public class TestByteInput {
public static void main(String[] args) {

try( DataInputStream  dis = new DataInputStream(new FileInputStream("res/data"));) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
 int length;
while ((length = dis.read(bytes)) != -1) {
baos.write(bytes,0 ,bytes.length - 1);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}

RandomAccessFile
写入数据。

public class TestRandom {
public static void main(String[] args) {

try (RandomAccessFile raf = new RandomAccessFile("res/data", "rw")) {
raf.writeChars("1234567890");
} catch (IOException e) {
e.printStackTrace();
}
}
}

文件内容 1 2 3 4 5 6 7 8 9 0

再次写入

public class TestRandom {
public static void main(String[] args) {

try (RandomAccessFile raf = new RandomAccessFile("res/data", "rw")) {
raf.writeChars("abc");
} catch (IOException e) {
e.printStackTrace();
}
}
}

文件内容:a b c 4 5 6 7 8 9 0

raf.seek(3);跳过几个字节

Serializable实现这个接口实现序列化。

private transient int i;//关键字transient不被序列化的意思

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值