java Day19-20 IO流

装饰设计模式

将老的,过时的类作为参数,传入到新的升级在这里插入代码片类中进行功能扩展。自定义的升级类就叫做装饰类。

class play
{
    public void play()
    {
        System.out.println("play");
    }
}
class superPlay
{
    private play p;
    superPlay(play p)
    {
        this.p=p;
    }
    public void superplay()
    {
        System.out.println("play");
        System.out.println("playgames");
    }
}
class demo
{
    public static void main(String[] args)
    {
        play p1=new play();
        superPlay sp=new superPlay(p1);
        sp.superplay();
    }
}

装饰设计模式和继承的区别
不同的功能的参数相同,当对类进行增强的时候,我们可以让新的功能extends老类,这样增加了扩展性。它和继承的不同是类与类之间的关系降低了。

lineNumberReader

读取文件行数

import java.io.*;

class demo
{
    public static void main(String[] args)throws IOException
    {
        FileWriter fw=new FileWriter("ioDemo.txt");
        BufferedWriter bw=new BufferedWriter(fw);
        bw.write("i am  ok ok \n");
        bw.write("can you hear me ??");
        bw.close();
        FileReader fr=new FileReader("ioDemo.txt");
        LineNumberReader lnr=new LineNumberReader(fr);
        String line=null;
        while((line=lnr.readLine())!=null)
        {
            System.out.println(lnr.getLineNumber()+line);
        }
        lnr.close();
    }
}

字节流输入输出

对图片或其他形式的数据进行操作的时候就需要用到字节流。
FileOutputStream 输入
FileInputStream 输出

import java.io.*;

class demo//字节流操作复制一个图片
{
    public static void main(String[] args)throws IOException
    {
        FileOutputStream fos=new FileOutputStream("D://UserData//Desktop//ioDemo.txt");
        FileInputStream  fis=new FileInputStream("D://UserData//Desktop//pic.png");
        byte[] arr=new byte[2048];
        int num=0;
        while((num=fis.read(arr))!=-1)
        {
            fos.write(arr,0,num);
        }
    }
}

标准输入

从键盘上获取输入,与输出System.out对应的就是System.in
获取输入的时候,用到InputStream类,它是一个字节流的输入方法,每次读取一个字节。

InputStream in=System.in;

通过调用in对象的read()方法,从键盘上输入数据,每次读取一个字节,返回读取的ASCII值,如果没读到就返回-1.

改变输入输出设备

setIn(new printstream x);设置输入设备为x
setOut(new printstream x):设置输出设备为x

文件

文件类:对文件的操作。
创建一个文件对象:

创建一个指定路径的文件对象

File fp=new File("Filename");

文件类的使用
1.文件的创建
fp.creatNewFile(“path”):在指定位置创建文件,如果文件不存在,则创建文件并返回true,如果文件存在,则返回false。
fp.mkdir():创建一个文件夹,创建此抽象路径名称指定的目录(及只能创建一级的目录,且需要存在父目录)
fp.mkdirs():创建一个多级文件夹。创建此抽象路径指定的目录,包括所有必须但不存在的父目录。(及可以创建多级目录,无论是否存在父目录)
2.文件的删除
fp.delete():删除文件,删除成功返回true,删除失败返回false
void deleteOnExit():在虚拟机结束运行的时候删除文件。
3.文件的判断

以下方法用于判断文件的类型。
fp.isFile():判断是否是文件,是则返回true,不是返回false
fp.isDirectory():判断是否是文件夹,是则返回true,不是返回false
fp.isHidden():判断文件是否是隐藏的,是则返回true,不是则返回false
fp.isAbsolute():判断文件是否是绝对路径,如果是则返回true不是则返回false。

4.文件的获取
fp.length():返回文件的长度
fp.getAbsolutePath():放回文件的绝对路径
fp.getName():返回文件的名字
fp.getPath():返回文件的相对路径
fp.getParent():返回文件的父目录路径。
fp.renameTo(fp2):将文件名字改为目的文件对象的名字,包括目录。

5.文件列表
fp.listRoots():返回一个数组,数组中存放的是所有磁盘。
fp.list():返回一个数组,数组中装的是目录下的所有文件的列表,
带参数的fp.list():按照要求,来过滤文件,并列出文件列表。
新建FilenameFilter对象,然后把这个对象中的判断要求写好,然后把这个对象传入list做参数。

class demo
{
    public static void main(String[] args)
    {
        File fp=new File("D://");
        FilenameFilter fnf=new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".txt");
            }
        };
        String [] arr=fp.list(fnf);
        for(String name:arr)
        {
            System.out.println(name);
        }
    }
}

fp.listFiles():返回一个文件数组,装着文件,可以获取文件的名字和大小。

properties类

HashTable的子类,具有HashMap的属性,数据的存储方式是键值对方式存储。

properties pop=new properties();

增加
pop.setProperty(k,v);
获取
pop.getProperty(k);
遍历
pop.stringPropertyNames():这个方法会返回一个set集合,set集合可以用增强for循环,也可以用迭代器遍历。

load():将流中的信息加载到Properties中。
list():将Properties中的信息列出到指定输出设备上。

 public static void main(String[] args)throws IOException
    {
        Properties pro=new Properties();
        InputStreamReader isr=new InputStreamReader(new FileInputStream("D://UserData//Desktop//rose.txt"),"GBK");
        pro.load(isr);
        pro.list(System.out);
    }

store(“输出流”,”注释信息“;)
将信息写入文件,但是这个方法会将文件覆盖。如果不覆盖,在流中添加true。

 public static void main(String[] args)throws IOException
    {
        Properties pro=new Properties();
        InputStreamReader isr=new InputStreamReader(new FileInputStream("D://UserData//Desktop//rose.txt"),"GBK");
        OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("D://UserData//Desktop//rose.txt"),"GBK");
        pro.load(isr);
        pro.list(System.out);
        pro.setProperty("哈哈哈哈","撒旦撒旦");
        pro.store(osw," ceshi?");
    }

PrintStream与PrintWriter

字节打印流:PrintStream():该方法提供了打印方法,可以将任何类型的数据原样打印出来。
字符打印流:PrintWriter():该方法可以接收打印目的地,同样提供了打印方法,在参数列表中,接收一个流,将信息打印到流中,在流后面加上true,就可以自动刷新。

public static void main(String[] args)throws IOException
    {
        Scanner in=new Scanner(System.in);
        PrintWriter pw=new PrintWriter(new FileWriter("D://UserData//Desktop//rose.txt"),true);
        String line=null;
        while((line=in.next())!=null)
        {
            pw.println(line.toUpperCase());
        }
        pw.close();
        in.close();
    }

合并流

SequenceInputStream():
将多个流合并,然后写入。

Vector<FileInputStream> v= new Vector<FileInputStream>();
        v.add(new FileInputStream("1.txt"));
        v.add(new FileInputStream("2.txt"));
        v.add(new FileInputStream("3.txt"));
        Enumeration<FileInputStream> en=v.elements();

        SequenceInputStream sis=new SequenceInputStream(en);

切割文件

public static void main(String[] args)throws IOException
    {
        byte[] buf=new byte[1024*100];
        FileInputStream fis=new FileInputStream("D://UserData//Desktop//ioDemo.png");
        int count=0;
        int len=0;
        while((len=fis.read(buf))!=-1)
        {
            FileOutputStream fos=new FileOutputStream("D://UserData//Desktop//"+(count++)+".ppart");
            fos.write(buf,0,len);
            fos.close();
        }
        fis.close();

对象的序列化

将对象写入文件的工具:ObjectOutputStream
将对象读出的工具:ObjectInputStream
写入和读出的工具可以将一个对象写入或读出文件。
代码如下。

写入时,必须将对象序列化,让对象类实现Serializable接口,即可以实现序列化。

import java.io.*;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Scanner;
import java.util.Vector;

class person implements Serializable
{
    private String name;
    private int age;
    person(String name,int age)
    {
        this.name=name;
        this.age=age;
    }
    public String toString()
    {
        return name+age;
    }
}
class prodemo
{
    public static void main(String[] args)throws Exception
    {
        ObjectOutputStream obj=new ObjectOutputStream(new FileOutputStream("D://UserData//Desktop//rose.txt"));
        obj.writeObject(new person("123",19));
        ObjectInputStream oos=new ObjectInputStream(new FileInputStream("D://UserData//Desktop//rose.txt"));
        person p=(person)oos.readObject();
        System.out.println(p);
    }
}

如果想读取之前序列化写入的信息,要使得UID不变。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值