ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

字节流&&字节缓冲流

2019-07-03 10:28:16  阅读:153  来源: 互联网

标签:&& fis File 缓冲 String file close new 字节


字节流&&字节缓冲流

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();

标签:&&,fis,File,缓冲,String,file,close,new,字节
来源: https://blog.csdn.net/AGoodBoy_lj/article/details/94551714

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有