Java的IO流

九十六、文件类
(1)、当前日期自定义输出格式

	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	
	//2019-1-17 9:36:55
	String time=sdf.format(date);
	System.out.println(time);

	Date date=new Date(f.lastModified());
	System.out.println(date);

(2)、创建文件
f.mkdir();//如果没有指定的目录,就创建该目录(单个)
f.mkdirs();//创建多个

(3)、指定条件选择子目录或者子文件

		File list []=f.listFiles(new FileFilter() {
			@Override
			public boolean accept(File pathname) {
				//返回true的过滤出来
				if(pathname.getName().endsWith(".txt")) {
					return true;
				}
				return false;
			}
		}); 

九十七、字节流

注意:
        字节流:以字节的形式传输;
        是抽象类,不能创建对象。

(1)、缓冲流:给传输的过程添加缓冲区,将数据放入缓冲区中,再传输和取出。
            输出流:写入

	BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(file));
	String s="sdsdsds";
	bos.write(s.getBytes());
	bos.flush();//强制刷新缓冲区域
	bos.close();

            输入流:读取

	BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
	byte b[] =new byte[512];
	while(true) {
		int len=bis.read(b);
		if(len==-1) {
			break;
		}
		System.out.println(new String(b,0,len));
	}
	bis.close();

(2)、数据操作流
            1、数据转换流:当向文件中写数据并传入数字时,写的是ascii码中对应的字符;
            2、使用数据转换流,可以向文件中写入基本数据类型;
            3、代码实现:
File f=new File(“src/b.txt”);
输出流:写入

		FileOutputStream fps=new FileOutputStream(f);
		DataOutputStream dps=new DataOutputStream(fps);
		dps.writeDouble(97.485);//double基本类型(写进去后,数据是二进制形式,打开时乱码)
		dps.writeInt(45);//int基本类型
		dps.close();//关闭操作,和刷新一样
		dps.flush();
		fos.close();

输入流:读取

		DataInputStream dis=new DataInputStream(new FileInputStream(f));
		double c=dis.readDouble();
		int b=dis.readInt();//写进去的数据要按照顺序读取
		dis.close();

(3)、文件输入流
       和缓冲流差不多
              FileInputStream fis=new FileInputStream(new File(“src/a.txt”));

       可以将原来的数据覆盖掉
              FileOutputStream fos=new FileOutputStream(f2);

       在原来数据的基础上添加
              FileOutputStream fos=new FileOutputStream(f2,true);
              byte by[]=result.getBytes();//将字符串转化成字节数组

(4)、对象序列化与反序列化
             transient 阻止序列化,只能修饰属性,并且类必须实现Serializable接口
             private transient String password;

       对象流:将某个类的对象以字节形式进行传输–序列化 ( 对象-字节)

ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(new File("src/d.txt")));
Student s2=new Student("阿亮",18,"dsdsd");
oos.writeObject(s2);//写进去的对象信息是二进制的,打开时乱码

       反序列化:将字节转化成对象

ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));	
System.out.println(ois.readObject());;
System.out.println(ois.readObject());;
ois.close();

(5)、打印流:单向的输出流,和System.out.println(“ss”);差不多

PrintStream  ps=new PrintStream(new FileOutputStream(new File("src/d.txt")));
String s="1111";
ps.write(111);

九十八、字符流
       (1)、缓冲流

		BufferedWriter bw=new BufferedWriter(new FileWriter(new File("src/1.txt")));
		bw.write("dsahfsdfgifdsfdsfidsifd\n");
		bw.newLine();//换行
		bw.flush();
		bw.close();
		BufferedReader br=new BufferedReader(new FileReader(new File("src/1.txt")));
		String res=br.readLine();
		System.out.println(res);

       (2)、数组

		FileReader fr=new FileReader(new File("src/1.txt"));
		char c[] =new char[512];
		while(true) {
			int len=fr.read(c);
			if(len==-1) {
				break;
			}
			System.out.println(new String (c,0,len));
		}
		fr.close();
		FileWriter fw=new FileWriter(new File("src/1.txt"));
		fw.write("hello shenliang");
		//fw.flush();//强制刷新
		fw.close();

九十九、将字节流转化为字符流

	OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(new File("src/1.txt")),"GBk");//指定编码集
	osw.write("dsdsadsad");
	osw.flush();
	BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(new File("src/1.txt"))));
	System.out.println(br.readLine());
	br.close();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值