java输入流显示进度_Java输入输出流

File类的使用

三种文件构造方式

//File file1 = new File("C:\\Users\\Kong\\Desktop\\kong\\xing.txt");

//File file1 = new File("C:\\Users","Kong\\Desktop\\kong\\xing.txt");

File file =new File("C:\\Users");

File file1 = new File(file,"Kong\\Desktop\\kong\\xing.txt");

常用API

bollean es = file.exists();

file.createNewFile();

file.mkdir();

file.mkdirs();

String na = file.getName()

String pa = file.getParent()

boolean cd = file.canRead()

boolean we = file.canWrite()

String gh = file.getPath()

String ah = file.getAbsolutePath()

字节流

二进制文件传输(图片)

注意flush()和close()

cd3d65730c4e3f8852af82b989933373.png

477f0a6ffd4d1467e114aaed6fc2f2fb.png

方式一

e18239d8b08243d79dbb7b3a018e3278.png

方式二

fbe5267c56a503165bb46650d74a1c8b.png

字符流

字符文件传输及字节字符转换

Reader和Writer

注意构造器包装类型

方式一

01b5a6d33b8d4766e180f74f4122e0ed.png

方式二

1735c3667f549693b1a6416c533b1f92.png

序列化(Serializable)

public class GoodsTest {

public static void main(String[] args) {

//Goods类要实现Serializable

Goods goods1 = new Goods("gd001","电脑",3000);

try {

FileOutputStream fos = new FileOutputStream("kong.txt");

ObjectOutputStream oos = new ObjectOutputStream(fos);

FileInputStream fis = new FileInputStream("kong.txt");

ObjectInputStream ois = new ObjectInputStream(fis);

//将对象信息写入文件

oos.writeObject(goods1);

oos.writeBoolean(true);

oos.flush();

//读对象信息

Goods goods = (Goods)ois.readObject();

System.out.println(goods);

System.out.println(ois.readBoolean());

fis.close();

ois.close();

fos.close();

oos.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e)

{

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值