java将数据缓存到文件流_java缓冲流;对象流 ; 文件流;数组流;数据流;转换流;打印流;Properties类的使用...

I/O 流

File对象--内存中  映射关系 通过file对象操作硬盘中的文件或文件夹

文件夹的遍历  文件夹的删除-->递归;

操作文件中的内容

文件流(低级流):字节型:FileInputStream  FileOutputStream

字符型:FileReader  FileWriter

缓冲流(包装流)  将低级流包装起来

在流管道内增加缓冲的数据

让我们使用流读取的文字更加的流畅

高级流-->创建通过低级流

缓存流 :字节型:BufferedInputStream BufferedOutputStream

字符型:BufferedReader  BufferedWriter

BufferedInputStream 构建方式 使用低级流构建作为参数

基本使用与低级流的方法完全一致

read() skip() available() close()

BufferedOutputStream 构建方式 使用低级流构建;没有boolean类型的参数

基本使用与低级流的方法完全一致

write() flush() close()

BufferedReader

String value=readLine();

BufferedWriter

write(String);

newLine();

077bcf7abb772d4fe2ef9ac0a05f4f0b.png

打印流  InputStreamReader PrintWriter

字符串流   StringReader  StringWriter

*对象流 对象的序列化/反序列化

1.为什么要有文件?

文件永久性的保存信息  将很多的数据直接存入文件--数据持久化

2.如果按照以行为单位新信息

好处在于每一行记录的信息都是相关的

信息我们可以读取处理 直接看懂文件

不好在于第一不安全 直接看懂

不好在于只能记录String信息 不能记录一些动作(方法)

3.读取处理的信息String -->Person

4.如果能将对象拆分成字节码 直接写入文件

将对象直接存入文件中--对象流

对象的序列化:将一个完整的对象 拆分成字节碎片 记录在文件中

对象的反序列化:将文件中记录的对象随便 反过来组合成一个完整的对象

如果想要将对象序列化到文件中

需要让对象所属的类实现Serializable接口

是一个示意性接口

同时为了让对象可以反序列化,需要让对象中多存在一个属性

private static longserialVersionUID=任意L;

如果想要将对象反序列化

需要给对象提供一个序列化的版本号

BufferedInputStream,BufferedOutputStream

import java.io.*;

public class TestMain{

public static void main(String[] args){

try{

File f=new File("D:\\ddd\\a.txt");

FileInputStream fi=new FileInputStream(f);

BufferedInputStream bi=new BufferedInputStream(fi);//缓冲

bi.available();

FileOutputStream fo=new FileOutputStream(f);

BufferedOutputStream bo=new BufferedOutputStream(fo);

bo.available();

}catch(IOException e){

e.printStackTrace();

}

}

}

BufferedWriter

import java.io.*;

public class TestMain{

public static void main(String[] args){

try{

File f=new File("D:\\ddd\\a.txt");

FileReader fr=new FileReader(f);//字符输入流

BufferedReader br=new BufferedReader(fr);

String values=br.readLine();//读取文件中的一行信息

while(values!=null){

System.out.println(values);

values=br.readLine();

}

/*int code=br.read();

while(code!=-1){

System.out.print((char)code);

code=br.read();

}

*/

}catch(IOException e){

e.printStackTrace();

}

}

}

登录

import java.io.*;

public class TestMain{

public String login(String filename,String name,String password){

try{

FileReader fr=new FileReader(filename);

BufferedReader br=new BufferedReader(fr);

String np=br.readLine();

while(np!=null){

if(np.split("-")[0].equals(name)){

if(np.split("-")[1].equals(password)){

return "登录成功";

}

break;

}

np=br.readLine();

}

}catch(IOException e){

e.printStackTrace();

}

return "账号或密码错误";

}

public static void main(String[] args){

TestMain tm=new TestMain();

String rs=tm.login("D:\\ddd\\a.txt","xiaohua","12345");

System.out.println(rs);

}

}

3c2c761127915ff1cb9b0b8da2a12de2.png

BufferedWriter

import java.io.*;

public class TestMain{

public static void main(String[] args){

try{

BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\ddd\\a.txt",true));

bw.newLine();

bw.write("Java-666");

bw.flush();

}catch(IOException e){

e.printStackTrace();

}

}

}

import java.io.*;

public class TestMain implements Serializable{

private long serialVersionUID=6807772913284504510L;

private String name;

private int age;

public TestMain(){

}

public TestMain(String name,int age){

this.name=name;

this.age=age;

}

public String getName(){

return this.name;

}

public int getAge(){

return this.age;

}

public static void main(String[] args){

/*

TestMain tm=new TestMain("xixi",30);

try{//将对象记录在文件中;

FileOutputStream fos=new FileOutputStream("D:\\ddd\\a.txt");

ObjectOutputStream oos=new ObjectOutputStream(fos);//对象输出流

oos.writeObject(tm);

oos.flush();

}catch(IOException e){

e.printStackTrace();

}

*/

try{//对象反序列化

FileInputStream fis=new FileInputStream("D:\\ddd\\a.txt");

ObjectInputStream ois=new ObjectInputStream(fis);//对象输入流

TestMain p=(TestMain)ois.readObject();

System.out.println(p.getName());

}catch(Exception e){

e.printStackTrace();

}

}

}

---------------------------------------------------------------------------------------

Properties类的使用;java.util;使用方式像是map集合 value=getProperty(key)方法

读取的信息是文件  文件的后缀名.properties  文件里面key=value

package test0607;

import java.util.*;

import java.io.*;

public class TestMain{

public static void main(String[] args){

try{

Properties p=new Properties();

p.load(new FileReader("test0607\\a.properties"));

/*

Enumeration en=p.propertyNames();

while(en.hasMoreElements()){

String key=(String)en.nextElement();

String value=p.getProperty(key);

System.out.println(value);

}

*/

String v=p.getProperty("name");

System.out.println(v);

}catch(Exception e){

e.printStackTrace();

}

}

}

e147ea2405854a66f597fbb960df5380.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值