1.Java中流的分类有哪些?

1)从流的流动方向上看:一般分为输入流(InputStream)和输出流(OutputStream)。

  • 输入流:如System.in是一个InputStream类型输入流
  • 输出流:如System.out 是一个PrintStream类型输出流

程序可以用输出流向文件中写数据,用输入流从文件中读数据。

 

2)从流的读取类型上分:一般分为字节流和字符流。

  • 字节流:如System.in是一个InputStream类型字节流 
  • 字符流:如new InputStreamReader(System.in)是一个字符流对象

3)从流发生的源头可以分为节点流和过滤流。

  • 节点流:直接操作目标设备对应的流, 如文件流,标准输入输出流
  • 过滤流:继承带有关键字Filter的流 , 用于包装操作节点流,方便读写各种类型的数据

2.字节流InputStream和OutputStream的子类分别有哪些?请举例说明其使用场景。与其对应的字符流分别有哪些?

1)InputStream的子类

 

 

 2)OutputStream的子类

 

 

 

字符流:

3)Reader类族

 

 

 4)Writer类族

 

 

 

3.字节流和字符流的转化是怎样的?Java对此提供了哪些支持?

1)输入字节流转为字符流需要用到InputstreamReader的构造方法:

InputStreamReader(InputStream in)I

例如:

InputStreamReader ins = new InputStreamReader(System.in);

InputStreamReader ins = new InputStreamReader(new   

                             FileInputStream(“test.txt”));


2)输出字符流转为字节流用到OutputStreamWriter或PrintWriter的构造方法:

OutputStreamWriter(OutputStream out)

PrintWriter(OutputStream out)

例如:

OutputStreamWriter outs = new OutputStreamWriter(new   

                             FileOutputStream(“test.txt”));

 

4.Java中的过滤流(流的装配)有什么作用?请举例说明常用的过滤流。

过滤流:   BufferedInputStream和BufferedOutputStream, 缓存作用,用于装配文件磁盘、网络设备、终端等读写开销大的节点流,提高读写性能

过滤流BufferedReader的使用:用于缓存字符流,可以一行一行的读

 1 import java.io.*;
 2 public class inDataSortMaxMinIn { 
 3     public static void main(String args[]) {
 4      try{
 5         BufferedReader keyin = new BufferedReader(new 
 6                                      InputStreamReader(System.in)); 
 7         String c1;
 8         int i=0;
 9         int[] e = new int[10];   
10         while(i<10){
11            try{
12                c1 = keyin.readLine();
13                e[i] = Integer.parseInt(c1);
14                i++;
15             }  
16             catch(NumberFormatException ee){
17                    System.out.println("请输入正确的数字!");
18             }
19        }
20     }
21     catch(Exception e){
22         System.out.println("系统有错误");
23  }}}

 

过滤流 DataInputStream和DataOutputStream的使用, 可从字节流中写入、读取Java基本数据类型,不依赖于机器的具体数据类型,方便存储和恢复数据

 1 import java.io.*;
 2 public class DataStream {
 3   public static void main(String[] args)throws Exception{
 4    try {
 5     DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new 
 6                                           FileOutputStream("test.txt")));
 7     dos.writeInt(3);//写入整型
 8         dos.writeDouble(3.14);//写入浮点型
 9     dos.writeUTF(“hello”);//写入字符串
10     dos.close();
11     DataInputStream dis = new DataInputStream(new BufferedInputStream(new 
12                                           FileInputStream("test.txt")));
13     System.out.println(dis.readInt()); //读取整型,输出3
14     System.out.println(dis.readDouble()); //读取浮点型,输出3.14
15     System.out.println(dis.readUTF()); //读取字符串,输出hello
16     dis.close();
17    } catch (FileNotFoundException e) {
18          e.printStackTrace();
19     }
20   }
21 }

 

 

5.什么是对象的序列化和反序列化?Java对此提供了哪些支持?

序列化和反序列化:序列化将实现了Seriallizable接口的对象转换成一个字节序列,并能够在以后将这个字节序列完全恢复为原来的对象,后者又称反序列化。

序列化的目的:便于介质存储和网络传输

Java提供了ObjectInputStream类和ObjectOutputStream类

 

6.Java的File类表示什么?有什么作用?

Java中的File类,以抽象的方式代表文件名和目录路径名。该类主要用于文件和目录的创建、文件的查找和文件的删除等。

它有四个构造函数:
File(String parent,String child)
File(File parent,String child)
File(URI uri)
File(String pathname)
封装了以下主要方法:
canWrite() 返回文件是否可以读写
canRead() 返回文件是否可读
compareTo(File pathname)检查文件路径间的顺序
createNewFile() 当文件不存在时生成文件
delete() 从文件系统内删除该文件
deleteOnExit() 程序顺利结束时删除文件
equals(Object obj) 检查特定对象的路径名是否相等
exists() 判断文件是否存在
getAbsoluteFile() 返回文件完整路径的File实例

7.Java对文件的读写分别提供了哪些支持?

读取文件:

 1 import java.io.*;
 2 public class OpenFile 
 3 {
 4     public static void main(String args[]) throws IOException
 5     {
 6         try
 7         {                                          //创建文件输入流对象
 8             FileInputStream  rf = new FileInputStream("OpenFile.java");
 9             int n=512,c=0;
10             byte buffer[] = new byte[n];
11             while ((c=rf.read(buffer,0,n))!=-1 )   //读取输入流
12             {
13                 System.out.print(new String(buffer,0,c));                
14             }            
15             rf.close();                            //关闭输入流
16         }
17         catch (IOException ioe)
18         { System.out.println(ioe);}
19         catch (Exception e)
20         { System.out.println(e);}
21     }
22 }

写入文件:

 1 import java.io.*;
 2 public class Write1 
 3 {
 4     public static void main(String args[])
 5     {
 6         try
 7         {   System.out.print("Input: ");
 8             int count,n=512;
 9             byte buffer[] = new byte[n];
10             count = System.in.read(buffer);        //读取标准输入流
11             FileOutputStream  wf = new FileOutputStream("Write1.txt");
12                                                    //创建文件输出流对象
13             wf.write(buffer,0,count);              //写入输出流
14             wf.close();                            //关闭输出流
15             System.out.println("Save to Write1.txt!");
16         }
17         catch (IOException ioe)
18         { System.out.println(ioe);}
19         catch (Exception e)
20         { System.out.println(e);}
21     }
22 }

 

posted on 2020-12-08 20:54  Katharsis_1  阅读(109)  评论(0)    收藏  举报