第十二章:IO流

来源:互联网 发布:网站数据库泄露 2017 编辑:程序博客网 时间:2024/06/03 21:00

持久化属性


Properties:持久属性集
示例:
Properties p = new Properties();
try {
/* load(既可以放字节流也可以放字符流):读取系统指定的文件,将文件的内容进行加载到Properties中
* 文件时以key=value的关系体现?
* 因为Properties继承Hashtable<>,实现Map<>接口*/
p.load(new FileReader("d:/a.properties"));
// p.load(new FileInputStream("d:/a.txt"));
/*p.getProperty(key):此方法返回的类型为字符串类型,
* 因为Properties的文件时以键值对(key=value)的形式体现
* 那么此方法是通过key获取value值 */
String name = p.getProperty("user");//从文件中获取到的值
System.out.println(name);
String pwd = p.getProperty("pwd");//从文件中获取到的值
int pass = Integer.parseInt(pwd);
Scanner sc= new Scanner(System.in);
System.out.println("请输入用户名:");
String username = sc.next();//控制台上输入的名字
System.out.println("请输入密码:");
int password = sc.nextInt();
if(username.equals(name)&&password==pass){
System.out.println("登录成功");
}else{
System.out.println("登录失败");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}

序列化和反序列化( 对象流)


序列化和反序列化:ObjectOutputSteam和ObjectInputStream

序列:是将java对象序列为字节的一个过程,
反序列化:将字节转换为java对象的一个过程( 统称为对象流)
Serializable:一个接口,普通类需要创建对象,普通类不能转换为对象流,
* 所以需要实现Seralizable接口,才能将对象进行序列化对反序列化(对象流)
* 若进行序列化与反序列化时,普通类没有实现此接口,那么程序报错(java.io.NotSerializableException)
反序列化示例
--------------------------------------------------------
Teacher t = new Teacher("张三", 21);
try {
/*序列化与反序列化:
* 只有ObjectOutputSteam和ObjectInputStream
* 构造方法中提供的参数为Stream(字节流)
* ObjectOutputSteam:写的内容为Object(任意类型) 包 含对象,提供的方法为writeObject(object)
* 将对象转换为字节写入到指定文件中
*/
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:/teacher.txt"));
oos.writeObject(t);

/*
* 反序列:切记反序列的为序列的对象(对象转换为字节),将字节转换为对象
* readObject():此方法返回的类型为Object,所以需要向下转型
*/
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:/teacher.txt"));
Teacher teacher = (Teacher)ois.readObject();
System.out.println(teacher);
ois.close();
oos.close();
ArrayList<Teacher> list = new ArrayList<>();
list.add(new Teacher("李四",22));
list.add(new Teacher("wangwu",22));
list.add(new Teacher("赵六",22));
list.add(new Teacher("田七",22));
ObjectOutputStream oosList = new ObjectOutputStream(new FileOutputStream("d:/list.txt"));
oosList.writeObject(list);
ObjectInputStream oisList = new ObjectInputStream(new FileInputStream("d:/list.txt"));
ArrayList<Teacher> teaList = (ArrayList<Teacher>)oisList.readObject();
// System.out.println(teaList);
for(Teacher tea:teaList){
System.out.println(tea);
}
oosList.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

File


File:文件和目录(文件夹)路径名的抽象表示形式
创建功能:
boolean createNewFile():创建文件 如果存在这样的文件,就不创建了
boolean mkdir():创建文件夹 如果存在这样的文件夹,就不创建了
boolean mkdirs():创建文件夹,如果父文件夹不存在,会帮你创建出
删除功能:boolean delete()
重命名功能 boolean renameTo(File dest)
判断功能:
boolean isDirectory():判断是否是目录
boolean isFile():判断是否是文件
boolean exists():判断是否存在
boolean canRead():判断是否可读
boolean canWrite():判断是否可写
boolean isHidden():判断是否隐藏
查看目录文件listFiles()
获取功能:
String getAbsolutePath():获取绝对路径
String getPath():获取相对路径
String getName():获取名称
long length():获取长度。字节数
long lastModified():获取最后一次的修改时间,毫秒值
获取功能:(数组)
String[] list():获取指定目录下的所有文件或者文件夹的名称数组
File[] listFiles():获取指定目录下的所有文件或者文件夹的File数组
过滤器(文件)FilenameFilter
--------------------------------------------------------------------------
File示例:
创建一个文件夹File file = new File("e:\\demo");
System.out.println("mkdir:" + file.mkdir());
创建一个文件
File file = new File("e:\\demo\\a.txt");
System.out.println(file.createNewFile());
删除一个文件
File file = new File("a.txt");
System.out.println( file.delete());
删除功能文件夹
File file = new File("aaa\\bbb\\ccc");
System.out.println( file.delete());
重命名功能
File newFile = new File("东方不败.jpg");
System.out.println( file.renameTo(newFile));
判断是否是目录 System.out.println( file.isDirectory());// false
判断是否是文件 System.out.println("isFile:" + file.isFile());// true
判断是否存在 System.out.println("exists:" + file.exists());// true
判断是否可读 System.out.println("canRead:" + file.canRead());// true
判断是否可写 System.out.println("canWrite:" + file.canWrite());// true
判断是否隐藏 System.out.println("isHidden:" + file.isHidden());// false
File file = new File("demo\\test.txt");
获取绝对路径 System.out.println("file.getAbsolutePath());
获取相对路径 System.out.println("getPath:" + file.getPath());
获取名称 System.out.println("getName:" + file.getName());
获取长度(字节数)System.out.println("length:" + file.length());
获取最后一次的修改时间,System.out.println(" file.lastModified());
判断E盘目录下是否有后缀名为.jpg的文件
public static void main(String[] args) {
File file = new File("e:\\");// 封装e判断目录
File[] fileArray = file.listFiles();// 获取该目录下所有文件或者文件夹的File数组
for (File f : fileArray) {// 遍历该File数组,得到每一个File对象,然后判断
if (f.isFile()) {// 是否是文件
if (f.getName().endsWith(".jpg")) {// 继续判断是否以.jpg结尾
System.out.println(f.getName());// 就输出该文件名
删除示例:
* File:创建文件或目录(文件夹)和实现文件或目录的一些功能
*/
File file = new File("d:/student.txt");
//判断是否存在(文件或目录)
if(!file.exists()){
try {
//createNewFile():创建文件
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
file.delete();//删除文件或目录
File file3 = new File("d:/a");
if(!file3.exists()){
file3.mkdir();//创建目录
}
File file2 = new File("d:/a/s.java");
if(!file2.exists()){
try {
file2.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 删除目录(文件夹)首先确认目录中是否有文件,
* 若有文件必须先将目录中的文件删除
* 然后才能删除目录
过滤器示例:
File file = new File("d:/aaa");
// FilenameFilter:过滤器(文件),接口,
// listFiles():查看目录文件
1.直接实例化接口(匿名类不类写法),
File[] fi = file.listFiles(new FilenameFilter() {
//dir:被找到的文件所在的目录。
// name: 文件的名称。
// endsWith(字符串):来自于String类,是否为指定的后缀结束
// 此方法返回的类型为boolean类型,若为真,那么将文件保存到File[]
//若为假,通过FilenameFilter将其过滤掉
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.endsWith(".java");
}
});
if(fi!=null){
for(File f:fi){
System.out.println(f);
}
}
// File[] fil = file.listFiles(new Name());
}
}
-------------------------------------------------------
递归查看目录下所有的文件和文件夹
public static void researchfile(File fo) {
File[] file = fo.listFiles();// 获取指定目录下的所有文件或者文件夹的File数组
if (file != null) {// 判断文件夹中的文件不等于空的
for (File fi : file) {// 遍历文件夹下所有文件
if (fi.isDirectory()) {// 判断是否是目录
researchfile(fi);// 自己调用自己把目录下文件打印出来
}
System.out.println(fi);// 打印所有的目录下的文件
}
}
}
@Test
public void aa() {
File file = new File("e:/");//要查看的文件夹的路径
researchfile(file);//调用递归的方法
}
}


IO流最新版


IO流概述:
IO流用来处理设备之间的数据传输(上传文件和下载文件)
IO流分类
字节流:
InputStream
FileInputStream (输入字节流)
BufferedInputStream (输入缓冲字节流)
OutputStream
FileOutputStream (输出字节流)
BufferedOutputStream
字符流:
Reader
FileReader
BufferedReader
Writer
FileWriter
BufferedWriter
字符流注意事项:
1 Reader和Writer(写) 只能使用在文本类型的,不能用在音频 视频 ,否者会损失精度;(打开无内容)
2.FileWriter有自动创建文件的功能,不需要通过File类中的createNewFile()创建文件( 只能创建文件 没有创建目录的功能);
IO流常用父类:
字节流的抽象基类:InputStream ,OutputStream。
字符流的抽象基类:Reader , Writer。
字节流读取数据
InputStream
FileInputStream
把刚才写的数据读取出来显示在控制台
FileInputStream的构造方法
FileInputStream(File file)
FileInputStream(String name)
FileInputStream的成员方法
public int read()
public int read(byte[] b)
PrintStream :创建具有指定文件新打印流(可以打印对象)
缓冲流:
字节缓冲输出流 BufferedOutputStream

字节缓冲输入流 BufferedInputStream
字符输出流OutputStreamWriter (写数据)

字符输入流InputStreamReader(读取数据)
PrintWrite 字节打印流(可以打印对象);
PrintRead
---------------------------
字符流示例:
FileWriter fw = new FileWriter("d:\\student.txt");
fw.write("你好啊!");
fw.close();//关流 节省资源
FileReader fr = new FileReader("d:/student.txt");//读取文件内容
FileWriter fileW = new FileWriter("d:/student_副本.txt");
int flag = 0;
char[] ch = new char[1024];
while((flag = fr.read(ch))!=-1){
String s = new String(ch,0,flag);
System.out.println(s);
fileW.write(s);
}
fileW.close();
fr.close();
} catch (Exception e) {
}
}
}
字节打印流
try {
PrintStream ps = new PrintStream("d:/user.txt");
ps.println("aaa");
ps.println("bbb");
ps.print(12);
ps.close();
Student stu = new Student("张三", 21);
Student stu2 = new Student("李四", 28);
Student stu3 = new Student("王五", 19);
PrintStream prs = new PrintStream("d:/a.txt");
prs.println(stu);
prs.println(stu2);
prs.println(stu3);
prs.close();
PrintWriter pw = new PrintWriter("d:/b.txt");
pw.println("张三");
pw.println("李四");
pw.println(stu);
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

字符缓冲流:
try{
BufferedReader br = new BufferedReader(new FileReader("d:/a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("d:/a_副本.txt"));
PrintWriter pw = new PrintWriter("d:/aa_.txt");
String s = "";
while((s = br.readLine())!=null){
System.out.println(s);
bw.write(s);
pw.println(s);
}
pw.close();
bw.close();
br.close();
} catch (Exception e) {
.printStackTrace();
}
}
}
字节流
try {
FileInputStream fis = new FileInputStream("E:\\娱乐\\音乐\\周杰伦 - 告白气球.mp3");
FileOutputStream fos = new FileOutputStream("d:/气球.mp3");
byte[] b = new byte[10000];
int flag = 0;
while((flag = fis.read(b))!=-1){
fos.write(b);
//fos.write(b,0,flag);
}
fos.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
-------------------------------------
byte[] b = "你好啊,今天天气挺好的".getBytes();//将字符串转换为字节存放到字节数组中
try {
FileOutputStream fos = new FileOutputStream("d:/student.txt");
fos.write(b);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}







原创粉丝点击