IO流(三)其他流与File类

1:其他的流

         1: DataOutputStream ;多了一些操作基本数据类型的功能

          DataInputStream:读取文件。

         用DataOutputStream流写进去的数据,就用DataInputStream流来读取。

 1 import java.io.DataInputStream;
 2 
 3 import java.io.DataOutputStream;
 4 
 5 import java.io.FileInputStream;
 6 
 7 import java.io.FileOutputStream;
 8 
 9 import java.io.IOException;
10 
11  
12 
13 public class DateIODemo1 {
14 
15  
16 
17          public static void main(String[] args) throws IOException {
18 
19                   
20 
21                    FileOutputStream fos = new FileOutputStream("e:\cc.txt");
22 
23                    /*
24 
25                     * 数据输出流是一种处理流,所以应该传入一个节点流作为参数
26 
27                     * 数据输出流写到文本上的内容是不是按照程序中的原样写进文件中的
28 
29                     */
30 
31                    DataOutputStream dos = new DataOutputStream(fos);
32 
33                   
34 
35                    dos.writeInt(123);//4字节
36 
37                    dos.writeBoolean(false);//1字节
38 
39                    dos.writeDouble(12.34);//8字节
40 
41                   
42 
43                    //数据输出流是一种处理流,所以应该传入一个节点流作为参数
44 
45                    FileInputStream fis = new FileInputStream("e:\cc.txt");
46 
47                    DataInputStream dis = new DataInputStream(fis);
48 
49                   
50 
51                    //按顺序读取才能读到正确的数据
52 
53                    System.out.println(dis.readInt());
54 
55                    System.out.println(dis.readBoolean());
56 
57                    System.out.println(dis.readDouble());
58 
59                   
60 
61                    dis.close();
62 
63                    dos.close();
64 
65                   
66 
67          }
68 
69 }
View Code

         2:关于内存的流:不用制定硬盘中的某个文件路径,因为所有的数据都是写入到内存上的,所以这些数据不会被永久保存。

      2.1 ByteArrayOutputStream;内存写入流。如果不是字符,则可以调用toByteArray方法获得内存中的数据。

 1 import java.io.ByteArrayOutputStream;
 2 
 3 import java.io.IOException;
 4 
 5  
 6 
 7 public class ByteArrayOutputDemo1 {
 8 
 9  
10 
11          public static void main(String[] args) throws IOException, InterruptedException {
12 
13                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
14 
15                    bos.write("我写的这些东西真的放到了内存里面了".getBytes());
16 
17  
18 
19                    String str = bos.toString();
20 
21                    System.out.println(str);
22 
23                   
24 
25          }
26 
27 }
28 
29  
View Code

                   2.2 ByteArrayInputStream:内存读取流。

 1 import java.io.ByteArrayInputStream;
 2 
 3 import java.io.IOException;
 4 
 5  
 6 
 7 public class ByteArrayInputStreamDemo1 {
 8 
 9          public static void main(String[] args) throws IOException {
10 
11                    byte[] b = "我真的真的在内存里面".getBytes();
12 
13                    ByteArrayInputStream bis = new ByteArrayInputStream(b);
14 
15                    byte[] b2 = new byte[1024];
16 
17                    int len = -1;
18 
19                    while((len = bis.read(b2)) != -1){
20 
21                             System.out.println(new String(b2, 0, len));
22 
23                    }       
24 
25          }
26 
27 }
View Code

         3:打印流: PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节。在需要写入字符而不是写入字节的情况下,应该使用 PrintWriter 类。

                   PrintStream

                   PrintWriter

 1 import java.io.BufferedReader;
 2 
 3 import java.io.InputStream;
 4 
 5 import java.io.InputStreamReader;
 6 
 7 import java.io.PrintStream;
 8 
 9 //接收键盘录入,写到文件中。
10 
11 public class PrintStreamDemo2 {
12 
13          public static void main(String[] args)throws Exception {
14 
15                    InputStream is = System.in;
16 
17                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
18 
19                    //打印流。
20 
21                    PrintStream ps = new PrintStream("f:\aaa.txt");
22 
23                    String str = null;
24 
25                    while(true){
26 
27                             str = br.readLine();
28 
29                             if("886".equals(str)){
30 
31                                      break;
32 
33                             }
34 
35                             ps.println(str);//将数据和换行写到目的地中。
36 
37                    }
38 
39                    br.close();
40 
41          }
42 
43  
44 
45 }
View Code

         4:RandomAccessFile:随机流。这个流并不是像平时说的那样“随机”,只是程序员可以使用这个类类读取文件中指定位置的数据,或者在文件中指定是位置写入数据。关键的方法就是seek();

 1 import java.io.FileNotFoundException;
 2 
 3 import java.io.IOException;
 4 
 5 import java.io.RandomAccessFile;
 6 
 7  
 8 
 9 public class RandomAccessFiileDemo1 {
10 
11         
12 
13          public static void main(String[] args) throws IOException {
14 
15                    RandomAccessFile raf = new RandomAccessFile("e:\cc.txt", "rw");
16 
17                    raf.writeBoolean(true);
18 
19                    raf.writeBytes("he");
20 
21 //               raf.writeFloat(123.99f);
22 
23                    raf.write(123);
24 
25                   
26 
27                    raf.seek(0);//可以让指针指向文件的某个位置
28 
29                    System.out.println(raf.readBoolean());
30 
31                    System.out.println(raf.read());
32 
33                    System.out.println(raf.readInt());
34 
35                    //System.out.println(raf.readInt());
36 
37                   
38 
39                    raf.close();
40 
41                   
42 
43          }
44 
45         
46 
47  
48 
49 }
View Code

         5:对象流.    Object   操作对象的流

                   这里会涉及到一个关键字transient(中文:短暂的,临时的),用来修饰成员变量。被static或者transient修饰的成员变量时改变量的值不会被对象流写出去的,也不会被读取到。对象流操作对象必须事项序列化接口Serializable,要不会出现java.io.InvalidClassException 无效的类型异常。

 1 import java.io.FileInputStream;
 2 
 3 import java.io.FileOutputStream;
 4 
 5 import java.io.IOException;
 6 
 7 import java.io.ObjectInputStream;
 8 
 9 import java.io.ObjectOutputStream;
10 
11  
12 
13 import com.java.domain.Student;
14 
15  
16 
17 public class WriterObjectDemo1 {
18 
19         
20 
21          public static void main(String[] args) throws IOException, ClassNotFoundException {
22 
23                    FileOutputStream fos = new FileOutputStream("e:\stu.txt");
24 
25                    ObjectOutputStream oos = new ObjectOutputStream(fos);
26 
27                   
28 
29                    oos.writeObject(new Student("zzz", 12));
30 
31                    oos.close();
32 
33                   
34 
35                    // java.io.InvalidClassException 无效的类型异常
36 
37                   
38 
39                    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e:\stu.txt"));
40 
41                    System.out.println(ois.readObject());
42 
43                    ois.close();
44 
45                   
46 
47                   
48 
49          }
50 
51  
52 
53 }
View Code
import java.io.Serializable;

 

public class Student implements Serializable {

         //使用对象

         private static final long serialVersionUID = 1L;

         private String name;

         //private static int age;

         private transient int age;

//      private int age;

        

         public Student() {

                   super();

                   // TODO Auto-generated constructor stub

         }

         public Student(String name, int age) {

                   super();

                   this.name = name;

                   this.age = age;

         }

         @Override

         public String toString() {

                   return "Student [name=" + name + ", age=" + age + "]";

         }

         public void test(){

                  

         }

        

}
Student类

         6:文件类.     File

                   6.1:File:表示文件或者目录。

                   6.2 常用的一些方法

          delete() 删除文件或目录。

          exists() 判断文件或目录是否存在。

          getAbsoluteFile()  返回路径名的绝对路径名形式。返回值-File

          getAbsolutePath()  返回路径名的绝对路径名字符串。返回值-String

          getName() 返回由路径名表示的文件或目录的名称。

          boolean isDirectory()  路径名表示的文件是否是一个目录。

           boolean isFile()  路径名表示的文件是否是一个标准文件。

          length()  返回路径名表示的文件的长度。

 String[]

list()返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。

 String[]

list(FilenameFilter filter)返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中满足指定过滤器的文件和目录。

 File[]

listFiles()返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。

 File[]

listFiles(FileFilter filter)返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件和目录。

 File[]

listFiles(FilenameFilter filter)返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件和目录。

          mkdir()  创建此抽象路径名指定的目录(只能创建一层)。

          mkdirs() 创建此抽象路径名指定的目录(可创建多层)。

经典示例:

// 遍历所有文件

       

 1   public static void getFiles(String path) {
 2 
 3  
 4 
 5                    File file = new File(path);
 6 
 7                    if (!file.exists()) {
 8 
 9                             System.out.println("没有找到该路径或者文件");
10 
11                             return;
12 
13                    }
14 
15                    if (file.isFile()) {
16 
17                             System.out.println(file.getAbsolutePath());
18 
19                    } else {
20 
21                             System.out.println(file.getAbsolutePath());
22 
23                             File[] files = file.listFiles();
24 
25                             // 遍历文件
26 
27                             for (File file2 : files) {// 递归遍历
28 
29                                      // 如果是目录在重新调用自己
30 
31                                      getFiles(file2.getAbsolutePath());
32 
33                             }
34 
35                    }
36 
37          }

         // 删除所有文件   

 1   public static void deleteFile2(String path) {
 2 
 3                    File file = new File(path);
 4 
 5                    if (!file.exists()) {
 6 
 7                             return;
 8 
 9                    }
10 
11                    if (file.isFile()) {
12 
13                             file.delete();
14 
15                    } else {
16 
17                             File[] files = file.listFiles();
18 
19                             for (File file2 : files) {
20 
21                                      deleteFile2(file2.getAbsolutePath());
22 
23                             }
24 
25                             file.delete();
26 
27                    }
28 
29          }
原文地址:https://www.cnblogs.com/xinge1993/p/4722587.html