当前位置:   article > 正文

Java--流_java流

java流

数据源:

数据源data source,提供数据的原始媒介。常见的数据源有:数据库、文件、其他程 序、内存、网络连接、lo 设备。 数据源分为:源设备、目标设备。

源设备:为程序提供数据,一般对应输入流。

目标设备:程序数据的目的地,一般对应输出流。

流的概念:

流是一个抽象、动态的概念,是一连串连续动态的数据集合。 对于输入流而言,数据源就像水箱,流(stream)就像水管中流动着的水流,程序就是我们最终的用户。我们通过流(AStream)将数据源(Source)中的数据(information)输送到程序(Program)中。对于输出流而言,目标数据源就是目的地(dest),我们通过流(A Stream)将程序(Program)中的数据(information)输送到目的数据源(dest)中。

Java中流的概念细分

按流的方向分类:

输入流:数据流向是数据源到程序(以InputStream、Reader结尾的流)。 输出流:数据流向是程序到目的地(以 OutPutStream、Writer结尾的流)。

  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.nio.charset.StandardCharsets;
  6. //文件不规范写法
  7. public class Test {
  8.    public static void main(String[] args) throws IOException {
  9.        readfile();
  10.        writeFile();
  11.   }
  12.    public static void readfile() throws IOException {
  13.        try (FileInputStream fis = new FileInputStream("D:/b.txt");){
  14.            int x = fis.read();
  15.            char y = (char) fis.read();
  16.            char z = (char) fis.read();
  17.            char w = (char) fis.read();
  18.            int r  = fis.read();//读取完毕返回-1
  19.            System.out.println((char)x);
  20.            System.out.println(y);
  21.            System.out.println(z);
  22.            System.out.println(w);
  23.            System.out.println(r);
  24. //           fis.close();//不关闭,操作系统打开的系统资源文件一直被打开
  25.            //通过操作系统打开文件,硬盘资源,不做关闭时,资源一直被打开//打出信号让操作系统去关闭文件
  26.       } catch (FileNotFoundException e) {
  27.            e.printStackTrace();
  28.       }
  29.   }
  30.    public static void writeFile() throws IOException {
  31.        FileOutputStream fis = null;
  32.        try {
  33.           fis = new FileOutputStream("d:/b.txt");
  34.            fis.write("wsxz".getBytes(StandardCharsets.UTF_8));//转为字节数组输入
  35.            fis.write('s');
  36.            fis.write("t521".getBytes(StandardCharsets.UTF_8));
  37.            fis.close();
  38.       } catch (FileNotFoundException e) {
  39.            throw new RuntimeException(e);
  40.       }
  41.        finally{
  42.            if (fis != null) {
  43.                fis.close();
  44.           }
  45.       }
  46.   }
  47. }
  48. import java.io.FileInputStream;
  49. import java.io.FileNotFoundException;
  50. import java.io.IOException;
  51. public class Test2 {
  52.    public static void main(String[] args) throws IOException {
  53.        readFile();
  54.   }
  55.    public static void readFile() throws IOException {
  56.        FileInputStream x = null;
  57.        try {
  58.            x = new FileInputStream("d:/b.txt");
  59.            int y = x.read();
  60.            int z = x.read();
  61.            int w = x.read();
  62.            int a = x.read();
  63.            int b = x.read();
  64.            System.out.println(y);
  65.            System.out.println(z);
  66.            System.out.println(w);
  67.            System.out.println(a);
  68.            System.out.println(b);
  69.       } catch (FileNotFoundException e) {
  70.            throw new RuntimeException(e);
  71.       } catch (IOException e) {
  72.            throw new RuntimeException(e);
  73.       }
  74.        finally {//文件最后不管前面是否报错都会将文件关闭
  75.            if(x != null)
  76.                x.close();
  77.       }
  78.   }
  79. }
  80. import java.io.FileInputStream;
  81. import java.io.FileNotFoundException;
  82. import java.io.IOException;
  83. public class Test3 {
  84.    public static void main(String[] args) throws IOException {
  85.        readFile();
  86.   }
  87.    public static void readFile() throws IOException {
  88.        try (FileInputStream x = new FileInputStream("d:/b.txt");){//编译器自动生成finally结构关闭文件
  89.            int y = x.read();
  90.            int z = x.read();
  91.            int w = x.read();
  92.            int a = x.read();
  93.            int b = x.read();
  94.            System.out.println(y);
  95.            System.out.println(z);
  96.            System.out.println(w);
  97.            System.out.println(a);
  98.            System.out.println(b);
  99.       } catch (FileNotFoundException e) {
  100.            throw new RuntimeException(e);
  101.       } catch (IOException e) {
  102.            throw new RuntimeException(e);
  103.       }
  104.   }
  105. }

按处理的数据单元分类:

字节流:以字节为单位获取数据,命名上以Stream结屋的流一般是字节流 。如FilelnputStream、FileOutputStream。

字符流:以字符为单位获取数据,命名上以Reader/Writer结屋的流一般是字符流,如 FileReaderFileWriter

  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.nio.charset.StandardCharsets;
  6. public class Test4 {
  7.    public static void main(String[] args) {
  8.        readFile();
  9.        writeFile();
  10.        CopyFile();
  11.   }
  12.    public static  void readFile()
  13.   {
  14.        try (FileInputStream x = new FileInputStream("d:/b.txt");){
  15.            StringBuilder y = new StringBuilder();
  16.            int temp = 0;
  17.            while((temp = x.read()) != -1)
  18.           {
  19.                y.append((char) temp);
  20.           }
  21.            System.out.println(y);
  22.       } catch (IOException e) {
  23.            throw new RuntimeException(e);
  24.       }
  25.   }
  26.    public static void writeFile()
  27.   {
  28.        FileOutputStream z = null;
  29.        try {
  30.            z = new FileOutputStream("d:/b.txt");
  31.            z.write("wsxzst5626454664".getBytes(StandardCharsets.UTF_8));
  32.       } catch (FileNotFoundException e) {
  33.            throw new RuntimeException(e);
  34.       } catch (IOException e) {
  35.            throw new RuntimeException(e);
  36.       } finally {
  37.            try {
  38.                z.close();
  39.           } catch (IOException e) {
  40.                throw new RuntimeException(e);
  41.           }
  42.       }
  43.   }
  44.  public static void CopyFile(){
  45.        try(FileInputStream c = new FileInputStream("d:/微信图片.jpG");
  46.        FileOutputStream v = new FileOutputStream("d:/微信图片复制.jpG");){
  47.            int temp = 0;
  48.            while((temp = c.read()) != -1)
  49.           {
  50.                v.write(temp);
  51.           }
  52.       } catch (FileNotFoundException e) {
  53.            throw new RuntimeException(e);
  54.       } catch (IOException e) {
  55.            throw new RuntimeException(e);
  56.       }
  57.   }
  58.    public static void CopyFile(){
  59.        try(FileInputStream c = new FileInputStream("d:/微信图片.jpG");
  60.            FileOutputStream v = new FileOutputStream("d:/微信图片复制2.jpG");){
  61.            byte[] buff = new byte[1024];
  62.            while((c.read(buff)) != -1)
  63.           {
  64.                v.write(buff);
  65.           }
  66.       } catch (FileNotFoundException e) {
  67.            throw new RuntimeException(e);
  68.       } catch (IOException e) {
  69.            throw new RuntimeException(e);
  70.       }
  71.   }
  72. }
  73. import java.io.FileNotFoundException;
  74. import java.io.FileReader;
  75. import java.io.FileWriter;
  76. import java.io.IOException;
  77. public class Test5 {
  78.    public static void main(String[] args) {
  79.        try(FileReader x = new FileReader("d:/c.txt");
  80.            FileWriter y = new FileWriter("d:/c_copy.txt");){
  81.            int temp = 0;
  82.            while((temp = x.read()) != -1) {
  83.                y.write(temp);
  84.           }
  85.       } catch (FileNotFoundException e) {
  86.            throw new RuntimeException(e);
  87.       } catch (IOException e) {
  88.            throw new RuntimeException(e);
  89.       }
  90.   }
  91. }

按处理对象不同分类:

节点流:可以直接从数据源或目的地读写数据,如 FilelnputStream、FileReaderDatalnputStream等。

处理流:不直接连接到数据源或目的地,是”处理流的流”。通过对其他流的处理提高程序的性能,如 BufferedInputStream、BufferedReader等。

  1. import java.io.*;
  2. public class Test6 {
  3.    public static void main(String[] args) {
  4.    try(FileInputStream x = new FileInputStream("d:/微信图片.jpG");
  5.        FileOutputStream y = new FileOutputStream("d:/微信图片2.jpG");
  6.        BufferedInputStream z = new BufferedInputStream(x);
  7.        BufferedOutputStream w = new BufferedOutputStream(y);){
  8.        int temp = 0;
  9.        while((temp = x.read()) != -1)
  10.       {
  11.            w.write(temp);
  12.       }
  13.   } catch (FileNotFoundException e) {
  14.        throw new RuntimeException(e);
  15.   } catch (IOException e) {
  16.        throw new RuntimeException(e);
  17.   }
  18.   }
  19. }

文件字符流

前面介绍的文件字节流可以处理所有的文件,但是字节流不能很好的处理Unicode字符,经常会出现“乱码”现象。所以,我们处理文本文件,一般可以使用文件字符流,它以字符为单位进行操作。

  1. import java.io.*;
  2. public class Test9 {
  3.    public static void main(String[] args) {
  4.        writeDate();
  5.        readData();
  6.   }
  7.    public static void writeDate(){
  8.        try(DataOutputStream x = new DataOutputStream(new FileOutputStream("d:/data.txt"))) {
  9.            x.writeChars("wsx");
  10.            x.writeInt(85);
  11.            x.writeChar('x');
  12.            x.writeInt(12);
  13.            x.writeBoolean(true);
  14.            x.writeUTF("吴时兴");
  15.            x.flush();//刷新缓存区,将流中数据写入文件
  16.       } catch (FileNotFoundException e) {
  17.            throw new RuntimeException(e);
  18.       } catch (IOException e) {
  19.            throw new RuntimeException(e);
  20.       }
  21.   }
  22.    public static void readData()
  23.   {
  24.        try(DataInputStream y = new DataInputStream(new FileInputStream("d:/data.txt"))){
  25.            System.out.println(y.readChar());
  26.            System.out.println(y.readChar());
  27.            System.out.println(y.readChar());
  28.            System.out.println(y.readInt());
  29.            System.out.println(y.readChar());
  30.            System.out.println(y.readInt());
  31.            System.out.println(y.readBoolean());
  32.            System.out.println(y.readUTF());
  33.       } catch (FileNotFoundException e) {
  34.            throw new RuntimeException(e);
  35.       } catch (IOException e) {
  36.            throw new RuntimeException(e);
  37.       }
  38.   }
  39. }

缓冲字节流

Java缓冲流本身并不具有10流的读取与写入功能,只是在别的 流(节点流或其他处理流)上加上缓冲功能提高效率,就像是把别的流包装起来一样,因此缓冲流是一种处理流(包装流)。 当对文件或者其他数据源进行频繁的读写操作时,效率比较低, 这时如果使用缓冲流就能够更高效的读写信息。因为缓冲流是先将数据缓存起来,然后当缓存区存满后或者手动刷新时再一次性的读取到程序或写入目的地。 因此,缓冲流还是很重要的,我们在10操作时记得加上缓冲流来提升性能。BufferedInputStream和BufferedOutputStream这两个流是缓冲字节流,通过内部缓存数组来提高操作流的效率。下面我们通过两种方式(普通文件字节流与缓冲文件字节流)实现一个视频文件的复制,来体会一下缓冲流的好处。

  1. import java.io.*;
  2. public class Test7 {
  3.    public static void main(String[] args) {
  4.        try(BufferedReader x = new BufferedReader(new FileReader("d:/c.txt"));
  5.            BufferedWriter y = new BufferedWriter(new FileWriter("d:/c4.txt"));){
  6.            String temp = "";
  7.            while((temp = x.readLine()) != null)
  8.           {
  9.                y.write(temp);
  10.                y.newLine();
  11.           }
  12.       } catch (FileNotFoundException e) {
  13.            throw new RuntimeException(e);
  14.       } catch (IOException e) {
  15.            throw new RuntimeException(e);
  16.       }
  17.   }
  18. }
  19. import java.io.*;
  20. public class Test6 {
  21.    public static void main(String[] args) {
  22.    try(FileInputStream x = new FileInputStream("d:/微信图片.jpG");
  23.        FileOutputStream y = new FileOutputStream("d:/微信图片2.jpG");
  24.        BufferedInputStream z = new BufferedInputStream(x);
  25.        BufferedOutputStream w = new BufferedOutputStream(y);){
  26.        int temp = 0;
  27.        while((temp = x.read()) != -1)
  28.       {
  29.            w.write(temp);
  30.       }
  31.   } catch (FileNotFoundException e) {
  32.        throw new RuntimeException(e);
  33.   } catch (IOException e) {
  34.        throw new RuntimeException(e);
  35.   }
  36.   }
  37. }

缓冲字符流

BufferedReader/BufferedWriter增加了缓存机制,大大提高了读写文本文件的效率,同时,提供了更方便的按行读取的方法:readLine();处理文本时,我们一般可以使用缓冲字符流。

  1. import java.io.*;
  2. public class Test7 {
  3.    public static void main(String[] args) {
  4.        try(BufferedReader x = new BufferedReader(new FileReader("d:/c.txt"));
  5.            BufferedWriter y = new BufferedWriter(new FileWriter("d:/c4.txt"));){
  6.            String temp = "";
  7.            while((temp = x.readLine()) != null)
  8.           {
  9.                y.write(temp);
  10.                y.newLine();
  11.           }
  12.       } catch (FileNotFoundException e) {
  13.            throw new RuntimeException(e);
  14.       } catch (IOException e) {
  15.            throw new RuntimeException(e);
  16.       }
  17.   }
  18. }

字节数组流

ByteArrayInputStream和ByteArrayOutputStream经常用在需要流和数组之间转化的情况!说白了,FilelnputStream是把文件当做数据源。ByteArrayInputStream则是把内存中的”某个字节数组对象”当做数据源。

  1. import java.io.ByteArrayInputStream;
  2. import java.io.IOException;
  3. import java.nio.charset.StandardCharsets;
  4. public class Test8 {
  5.    public static void main(String[] args) {
  6.        test("dsafsgagd".getBytes(StandardCharsets.UTF_8));
  7.   }
  8.    public static void test(byte[] bytes){
  9.        int temp = 0;
  10.        int num = 0;
  11.        try(ByteArrayInputStream x = new ByteArrayInputStream(bytes)){
  12.            while((temp = x.read()) != -1){
  13.                System.out.println((char) temp);
  14.                num++;
  15.           }
  16.            System.out.println("读取字节数组为" + num);
  17.       } catch (IOException e) {
  18.            throw new RuntimeException(e);
  19.       }
  20.   }
  21. }

数据流

数据流将“基本数据类型与字符串类型”作为数据源,从而允许程序以与机器无关的方式从底层输入输出流中操作Java基本数据类型与字符串类型。 DatalnputStream和DataOutputStream提供了可以存取与机器无关的所有Java基础类型数据(如:int、double、String等)的方法。 DatalnputStream和DataOutputStream是处理流,可以对其他节点流或处理流进行包装,增加一些更灵活、更高效的功能。

  1. import java.io.*;
  2. public class Test9 {
  3.    public static void main(String[] args) {
  4.        writeDate();
  5.        readData();
  6.   }
  7.    public static void writeDate(){
  8.        try(DataOutputStream x = new DataOutputStream(new FileOutputStream("d:/data.txt"))) {
  9.            x.writeChars("wsx");
  10.            x.writeInt(85);
  11.            x.writeChar('x');
  12.            x.writeInt(12);
  13.            x.writeBoolean(true);
  14.            x.writeUTF("撒旦今年");
  15.            x.flush();//刷新缓存区,将流中数据写入文件
  16.       } catch (FileNotFoundException e) {
  17.            throw new RuntimeException(e);
  18.       } catch (IOException e) {
  19.            throw new RuntimeException(e);
  20.       }
  21.   }
  22.    public static void readData()
  23.   {
  24.        try(DataInputStream y = new DataInputStream(new FileInputStream("d:/data.txt"))){
  25.            System.out.println(y.readChar());
  26.            System.out.println(y.readChar());
  27.            System.out.println(y.readChar());
  28.            System.out.println(y.readInt());
  29.            System.out.println(y.readChar());
  30.            System.out.println(y.readInt());
  31.            System.out.println(y.readBoolean());
  32.            System.out.println(y.readUTF());
  33.       } catch (FileNotFoundException e) {
  34.            throw new RuntimeException(e);
  35.       } catch (IOException e) {
  36.            throw new RuntimeException(e);
  37.       }
  38.   }
  39. }

对象流

ObjectInputStream/ObjectOutputStream是以“对象”为数据源,但是必须将传输的对象进行序列化与反序列化操作。 需要序列化的对象,需要实现接口:iava.io.Serializable对象序列化的作用有如下两种: 持久化:把对象的字节序列永久地保存到硬盘上 通常存放在一 个文件中,比如:休眠的实现。以后服务器session管理,hibernate将对象持久化实现。 网络通信:在网络上传送对象的字节序列。比如:服务器之间的数据通信、对象传递。

不参与序列化

static属性不参与序列化。 对象中的某些属性如果不想被序列化,不能使用static,而是使用transient修饰。

  1. public class User implements java.io.Serializable{
  2.    private int id;
  3.    private String name;
  4.    transient private String pwd;
  5.    public User(int id, String name, String pwd) {
  6.        this.id = id;
  7.        this.name = name;
  8.        this.pwd = pwd;
  9.   }
  10.    public int getId() {
  11.        return id;
  12.   }
  13.    public void setId(int id) {
  14.        this.id = id;
  15.   }
  16.    public String getName() {
  17.        return name;
  18.   }
  19.    public void setName(String name) {
  20.        this.name = name;
  21.   }
  22.    public String getPwd() {
  23.        return pwd;
  24.   }
  25.    public void setPwd(String pwd) {
  26.        this.pwd = pwd;
  27.   }
  28. }
  29. import java.io.*;
  30. import java.util.ArrayList;
  31. public class Test10 {
  32.    public static void main(String[] args) {
  33.        writeObject();
  34.        ReadObject();//密码没有参与序列化
  35.   }
  36.    public static void  writeObject(){
  37.        try(ObjectOutputStream x = new ObjectOutputStream(new FileOutputStream("d:/Obj.txt"))){
  38.            ArrayList<User> list = new ArrayList<>();
  39.            list.add(new User(1001,"吴时兴","123456"));
  40.            list.add(new User(1002,"吴时兴2","123458526"));
  41.            list.add(new User(1003,"吴时兴3","123996"));
  42.            list.add(new User(1004,"吴时兴4","1235656"));
  43.            x.writeObject(list);
  44.            x.flush();
  45.       } catch (FileNotFoundException e) {
  46.            throw new RuntimeException(e);
  47.       } catch (IOException e) {
  48.            throw new RuntimeException(e);
  49.       }
  50.   }
  51.    public static void ReadObject(){
  52.        try(ObjectInputStream y = new ObjectInputStream(new FileInputStream("d:/Obj.txt"))){
  53.            ArrayList<User> list  = (ArrayList)y.readObject();
  54.            for(User u : list){
  55.                System.out.println(u.getId()+ "," + u.getName() + "," + u.getPwd());
  56.           }
  57.       } catch (FileNotFoundException e) {
  58.            throw new RuntimeException(e);
  59.       } catch (IOException e) {
  60.            throw new RuntimeException(e);
  61.       } catch (ClassNotFoundException e) {
  62.            throw new RuntimeException(e);
  63.       }
  64.   }
  65. }
  66. import java.io.*;
  67. import java.util.ArrayList;
  68. public class Test10 {
  69.    public static void main(String[] args) {
  70.        writeObject();
  71.        ReadObject();//密码没有参与序列化
  72.   }
  73.    public static void  writeObject(){
  74.        try(ObjectOutputStream x = new ObjectOutputStream(new FileOutputStream("d:/Obj.txt"))){
  75.            ArrayList<User> list = new ArrayList<>();
  76.            list.add(new User(1001,"吴时兴","123456"));
  77.            list.add(new User(1002,"吴时兴2","123458526"));
  78.            list.add(new User(1003,"吴时兴3","123996"));
  79.            list.add(new User(1004,"吴时兴4","1235656"));
  80.            x.writeObject(list);
  81.            x.flush();
  82.       } catch (FileNotFoundException e) {
  83.            throw new RuntimeException(e);
  84.       } catch (IOException e) {
  85.            throw new RuntimeException(e);
  86.       }
  87.   }
  88.    public static void ReadObject(){
  89.        try(ObjectInputStream y = new ObjectInputStream(new FileInputStream("d:/Obj.txt"))){
  90.            ArrayList<User> list  = (ArrayList)y.readObject();
  91.            for(User u : list){
  92.                System.out.println(u.getId()+ "," + u.getName() + "," + u.getPwd());
  93.           }
  94.       } catch (FileNotFoundException e) {
  95.            throw new RuntimeException(e);
  96.       } catch (IOException e) {
  97.            throw new RuntimeException(e);
  98.       } catch (ClassNotFoundException e) {
  99.            throw new RuntimeException(e);
  100.       }
  101.   }
  102. }

转换流

InputStreamReader/OutputStreamWriter用来实现将字节流转化成字符流。

  1. import java.io.*;
  2. public class Test11 {
  3.    public static void main(String[] args) {
  4.        try(BufferedReader x = new BufferedReader(new InputStreamReader(System.in));
  5.            BufferedWriter y = new BufferedWriter(new OutputStreamWriter(System.out));){
  6.            String str = x.readLine();
  7.            while(!"exit".equals(str)){
  8.                y.write("键盘输入:" + str);
  9.                y.newLine();
  10.                y.flush();
  11.                str = x.readLine();
  12.           }
  13.       } catch (IOException e) {
  14.            throw new RuntimeException(e);
  15.       }
  16.   }
  17. }

随意访问文件流

RandomAccessFile可以实现两个作用: 1.实现对一个文件做读和写的操作。 2.可以访问文件的任意位置。不像其他流只能按照先后顺序读取。

  1. import java.io.FileNotFoundException;
  2. import java.io.IOException;
  3. import java.io.RandomAccessFile;
  4. public class Test12 {
  5.    public static void main(String[] args) {
  6.        try(RandomAccessFile x = new RandomAccessFile("d:/data.txt","rw")){
  7.            int [] data = {10,20,3,63,52,85,96,85,74,20};
  8.            for (int i = 0; i < data.length; i++) {
  9.                x.writeInt(data[i]);
  10.           }
  11.            x.seek(4);
  12.            System.out.println(x.readInt());
  13.            x.seek(8);
  14.            x.writeInt(45);
  15.            for (int i = 0; i < 10; i++) {
  16.                x.seek(i * 4);
  17.                System.out.print(x.readInt() + "\t");
  18.           }
  19.       } catch (FileNotFoundException e) {
  20.            throw new RuntimeException(e);
  21.       } catch (IOException e) {
  22.            throw new RuntimeException(e);
  23.       }
  24.   }
  25. }

装饰器模式构建IO流体系

装饰器模式简介 装饰器模式是GOF23种设计模式中较为常用的一种模式。它 可以实现对原有类的包装和装饰,使新的类具有更强的功能。

  1. import org.apache.commons.io.FileUtils;
  2. import org.apache.commons.io.IOUtils;
  3. import java.io.File;
  4. import java.io.FileFilter;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.net.URL;
  8. import java.util.List;
  9. public class Test14 {
  10.    public static void main(String[] args) {
  11. //       writeFile();
  12. //       Read();
  13.        ReadUrl();
  14. //       FileCopy();
  15. //       directoryCopy();
  16.   }
  17.    public static void writeFile(){
  18.        StringBuilder x = new StringBuilder();
  19.        for(int i = 0;i < 1000;i++)
  20.       {
  21.            x.append(Math.random() + "\n");
  22.       }
  23.        try {
  24.            FileUtils.write(new File("d:/4.txt"),x.toString(),"gbk");
  25.       } catch (IOException e) {
  26.            throw new RuntimeException(e);
  27.       }
  28.   }
  29.    public static void Read(){
  30.        try {
  31.            List<String> y = FileUtils.readLines(new File("d:/4.txt"),"gbk");
  32.            for(String temp : y)
  33.           {
  34.                System.out.println(temp);
  35.           }
  36.       } catch (IOException e) {
  37.            throw new RuntimeException(e);
  38.       }
  39.   }
  40.    public static void ReadUrl(){
  41.        try {
  42.            URL v = new URL("https://vidhub.cc//");
  43.            InputStream b = v.openStream();
  44.            String xo = IOUtils.toString(b,"UTF-8");
  45.            System.out.println(xo);
  46.       } catch (IOException e) {
  47.            throw new RuntimeException(e);
  48.       }
  49.   }
  50.    public static void FileCopy(){
  51.        File x = new File("d:/data.txt");
  52.        File y = new File("d:/aa.txt");
  53.        try {
  54.            FileUtils.copyFile(x,y);
  55.       } catch (IOException e) {
  56.            throw new RuntimeException(e);
  57.       }
  58.   }
  59.    public static void directoryCopy(){
  60.        File x1 = new File("d:/1");
  61.        File x2 = new File("d:/3");
  62.        try {
  63.            FileUtils.copyDirectory(x1, x2, new FileFilter() {
  64.                @Override
  65.                public boolean accept(File pathname) {
  66.                    if(pathname.isDirectory() || pathname.getName().endsWith("jpG"))
  67.                   {
  68.                        return true;
  69.                   }
  70.                    else{
  71.                        return false;
  72.                   }
  73.               }
  74.           });
  75.       } catch (IOException e) {
  76.            throw new RuntimeException(e);
  77.       }
  78.   }
  79. }
  80. //及就是新类包含原类并对与有原类的功能进行了添加

外导jar包的使用:

  1. //阿帕奇开源代码导入commons下的IO流包
  2. import org.apache.commons.io.FileUtils;
  3. import org.apache.commons.io.IOUtils;
  4. import java.io.File;
  5. import java.io.FileFilter;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.net.URL;
  9. import java.util.List;
  10. public class Test14 {
  11.    public static void main(String[] args) {
  12. //       writeFile();
  13. //       Read();
  14.        ReadUrl();
  15. //       FileCopy();
  16. //       directoryCopy();
  17.   }
  18.    public static void writeFile(){
  19.        StringBuilder x = new StringBuilder();
  20.        for(int i = 0;i < 1000;i++)
  21.       {
  22.            x.append(Math.random() + "\n");
  23.       }
  24.        try {
  25.            FileUtils.write(new File("d:/4.txt"),x.toString(),"gbk");
  26.       } catch (IOException e) {
  27.            throw new RuntimeException(e);
  28.       }
  29.   }
  30.    public static void Read(){
  31.        try {
  32.            List<String> y = FileUtils.readLines(new File("d:/4.txt"),"gbk");
  33.            for(String temp : y)
  34.           {
  35.                System.out.println(temp);
  36.           }
  37.       } catch (IOException e) {
  38.            throw new RuntimeException(e);
  39.       }
  40.   }
  41.    public static void ReadUrl(){
  42.        try {
  43.            URL v = new URL("https://vidhub.cc//");
  44.            InputStream b = v.openStream();
  45.            String xo = IOUtils.toString(b,"UTF-8");
  46.            System.out.println(xo);
  47.       } catch (IOException e) {
  48.            throw new RuntimeException(e);
  49.       }
  50.   }
  51.    public static void FileCopy(){
  52.        File x = new File("d:/data.txt");
  53.        File y = new File("d:/aa.txt");
  54.        try {
  55.            FileUtils.copyFile(x,y);
  56.       } catch (IOException e) {
  57.            throw new RuntimeException(e);
  58.       }
  59.   }
  60.    public static void directoryCopy(){
  61.        File x1 = new File("d:/1");
  62.        File x2 = new File("d:/3");
  63.        try {
  64.            FileUtils.copyDirectory(x1, x2, new FileFilter() {
  65.                @Override
  66.                public boolean accept(File pathname) {
  67.                    if(pathname.isDirectory() || pathname.getName().endsWith("jpG"))
  68.                   {
  69.                        return true;
  70.                   }
  71.                    else{
  72.                        return false;
  73.                   }
  74.               }
  75.           });
  76.       } catch (IOException e) {
  77.            throw new RuntimeException(e);
  78.       }
  79.   }
  80. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/469668
推荐阅读
相关标签
  

闽ICP备14008679号