字节流&&字节缓冲流

字节流&&字节缓冲流

JFileChooser jfc = new JFileChooser();
//设置文件选择模式
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.showDialog(null, “选择”);
File f = jfc.getSelectedFile();
if(f!=null){
String path = f.getPath();//获取选中文件的路径
jtfa.setText(path);//放到文本框中
}

String path = jtfa.getText();
File f = new File(path);
delete(f);

//重新刷新数据
int n = jta.getRowCount();
for (int i = 0; i < n; i++) {
dtm.removeRow(0);
}
printAll(f);

====================================================
Long time = file.lastModified();// 获得该文件的最后修改时间,返回的是long类型
Calendar cd = Calendar.getInstance();
cd.setTimeInMillis(time);
Date fileTime = cd.getTime();// 把long类型的最后修改时间转为Date类型
cd.setTime(new Date());// 获取当前日期
cd.add(Calendar.MONTH, -1);// 当前时间减去一个月,即一个月前的时间
Date lastMonth = cd.getTime();// 获取Date类型的一个月前的日期
if (fileTime.after(lastMonth)) {// 比较文件的最后修改日期如果在一个月前的日期之后,说明该文件是最近一个月的文件
file.delete();// 删除该文件
}

=========================================================================

input:
output:

IO流用来处理【设备】之间的【数据】传输

流是一组流动的数据的总称。类似于水流
流是有方向性的。我们应该以当前程序为参照物。(内存)
如果说是程序中要获得外面的数据,那么我们应该使用输入流
如果由程序向外面扔数据就应该是输出流

字节流:可以操作所有的文件类型 记事本 图片 媒体文件 包括视频

File file = new File("f:\\图片\\666.txt");
if(!file.exists()){
    file.createNewFile();
}

//1、读取记事本中的内容  前提是记事本里要有东西
FileInputStream fis = new FileInputStream(file);//创建一个文件读取的对象
int n = fis.read();//ASCII码
System.out.println((char)n);
fis.close();//关闭读取流

//2、写入记事本
Scanner mys = new Scanner(System.in);
File file = new File("f:\\图片\\666.txt");
FileOutputStream fos = new FileOutputStream(file);
System.out.println("请输入你要保存的内容:");
String str = mys.next();
//将字符串转换为字节数组
byte[] bs = str.getBytes();
fos.write(bs);
fos.close();
System.out.println("ok");


//解决覆盖问题 输入exit退出  先读再写
File file = new File("f:\\图片\\888.txt");
FileInputStream fis = new FileInputStream(file);
byte[] bs = new byte[(int)file.length()];
fis.read(bs);
String str = new String(bs);//获取原先的东西
while(true){
    System.out.println("请输入你要保存的字符串");
    String ss = mys.next();//用户写的东西
    if(ss.equals("exit")){
            break;
    }
    str+=ss;//拼接
}
 ());
fis.close();
fos.close();


包装类:缓冲流======效率高

File file = new File("f:\\图片\\666.txt");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] bs = new byte[(int)file.length()];
bis.read(bs);
String str = new String(bs);
//输出检测一下
System.out.println(str);
//记得关闭一下,以免缓存信息过多导致电脑运行速度变慢
bis.close();
fis.close();
    
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("哈哈哈哈".getBytes());
bos.close();
fos.close();

long start = System.currentTimeMillis();

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值