JAVAIO流

Java IO(Input/Output)指java提供的用于读取和写入数据的输入输出库,主要用于处理数据的传输。Java.io 包几乎包含了所有操作输入、输出需要的类。

javaIO按流的方向分为输入流和输出流;

javaIO按流的数据单位不同分为字节流和字符流;

字节流

在字节流类层次结构的顶部,包含两个抽象类:InputStream用于面向字节的输入,OutputStream用于面向字节的输出操作。

InputStream和OutputStream的子类有FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream等。

解释
InputStream面向字节的输入流的顶级抽象类。
ByteArrayInputStream此类的一个实例包含一个用于读取字节流的内部缓冲区。
FilterInputStream此类的一个实例包含一些其他输入流,作为进一步操作的基本数据源。
BufferedInputStream(字节缓冲流)这使FilterInputStream实例能够使用缓冲区来存储输入数据。
DataInputStream此类的一个实例能够以与机器无关的方式从底层输入流中读取原始 Java 类型。
LineNumberInputStream此类的一个实例有助于跟踪输入流的当前行号。
PushbackInputStream这提供了在读取数据字节后推回或“未读”数据字节的能力。
FileInputStream此类的一个实例用于从文件系统中的文件中获取输入字节。
 ObjectInputStream该类的一个实例用于在对象被 ObjectOutputSteam 序列化后对其进行反序列化。
PipedInputStream这个类的一个实例为输入字节提供了一个管道或缓冲区,它以 FIFO 方式工作。
SequenceInputStream此类的一个实例表示两个或多个输入流的逻辑串联,这些输入流一个接一个地依次读取。
OutputStream面向字节的输入流的顶级抽象类。
ByteArrayOutputStream此类的一个实例包含一个用于写入字节流的内部缓冲区。
 FilterOutputStream此类的一个实例包含一些其他输出流,作为进一步操作的基本数据源。

BufferedOutputStream

(字节缓冲流)

这使 FilterOutputStream实例能够使用缓冲区来输出数据。
 DataOutputStream此类的一个实例能够以与机器无关的方式将原始 Java 类型写入底层输出流。
PrintStream这使 OutputStream 对象能够方便地打印各种数据值的表示形式。
FileOutputStream此类的一个实例用于输出流,用于将数据写入文件或文件描述符。
ObjectOutputStream这个类的一个实例用于序列化一个可以用ObjectInputStream反序列化的对象。
PipedOutputStream这个类的一个实例为输出字节提供了一个管道或缓冲区,它以先进先出的方式工作。

字节缓冲流作用:

  • 提高文件读写效率
  • 提高网络传输效率
  • 处理基本数据类型
  • 支持多线程

FileInputStream和FileOutputStream构造方法:

可通过下列构造方法创建指向文件的输入/输出流

FileInputStream(String name);
FileInputStream(File file);

FileOnputStream(String name);
FileOnputStream(File file);


FileInputStream

try{
FileInputStream in = new FileInputStream("hello.text");
              //创建指向文件hello.text的输入流
}
catch(IOException e){
   System.out.println("File read error:"+e);
}

File f = new File("hello.text");  
//指定输入流的源

Example1

import java.io.*;
public class Example1{
   public static void main(String args[]){
      int n=-1;
      byte[]a=new byte[100];
      try{
       File f= new File("Example1.java");
     InputStream in= new FileInputStream(f);
     while((n=in.read(a,0,100)!=-1){
        String s = new String(a,0,n);
   System.out.print(s);  
 }
in.close();
}
catch(IOException e){
   System.out.println("File read Error"+e);
     }
   }
}

Example2

import java.io.*;
public class Example2{
    public static void main(String args[]){
      byte[]=a"新年快乐".getBytes();
      byte[]=b"Happy New Year".getBytes();
      File file= new File("a.text");    //输出目的地
      try{
          OutputStream out = new FileOutputStream(file,ture); //指向目的地的输出流
          System.out.println(file.getName()+"的大小:"+file.length()+"字节");
                                                              //a.txt的大小:0字节
                                                              //向目的地写数据
          out.write(a);
          out.close();
          out = new FileOutputStream(file,ture);   //准备向文件尾加内容
          System.out.println(file.getName()+"的大小:"+file.length()+"字节");
                                                              //a.txt的大小:8字节
          out.write(b,0,b.length);           
          System.out.println(file.getName()+"的大小:"+file.length()+"字节");
                                                               //a.txt的大小:22字节
          out.close();
      }
 catch(IOException e){
     System.out.println("Error"+e);
       } 
    }
}

字符流

字符流 = 字节流 + 编码表

字符流可以看做是特殊的字节流,在使用字节流读取文件夹时,如果读取的是文档文件,有时可能会因为字符编码导致读取出现问题

跟字节流的FileInputStream和FileOutputStream类相类似,字符流也有相应的文件读写流FileWriter和FileReader类,这两个类主要是对文本文件进行读写操作。

FileReader/FileWriter:可以直接写文件名的路径。

与InputStreamReader相比坏处:无法指定读取和写出的编码,容易出现乱码。

 FileReader fr = new FileReader("C:\\Users\\acer\\workspace\\encode\\new4\\test1"); //输入流
 FileWriter fw = new FileWriter(C:\\Users\\acer\\workspace\\encode\\new4\\test2");//输出流
char[] buffer=new char[8*1024];
       int c;
       while((c=fr.read(buffer, 0, buffer.length))!=-1){
            fw.write(buffer, 0, c);
            fw.flush();         
}
       fr.close();
        fw.close();

注意:FileReader和FileWriter不能增加编码参数,所以当项目和读取文件编码不同时,就会产生乱码。 这种情况下,只能回归InputStreamReader和OutputStreamWriter。

字符流的过滤器

字符流的过滤器有BufferedReader和BufferedWriter/PrintWriter

除了基本的读写功能外,它们还有一些特殊的功能。

  • BufferedReader----->readLine 一次读一行,并不识别换行
  • BufferedWriter----->write 一次写一行,需要换行
  • PrintWriter经常和BufferedReader一起使用,换行写入比BufferedWriter更方便

定义方式:

BufferedReader br =new BufferedReader(new InputStreamReader(new FileInputStream(目录的地址)))
BufferedWriter br =new BufferedWriter(new InputStreamWriter(new FileOutputStream(目录的地址)))
PrintWriter pw=new PrintWriter(目录/Writer/OutputStream/File);

使用方法:

 //对文件进行读写操作
        String file1="C:\\Users\\acer\\workspace\\encode\\new4\\test1";
          String file2="C:\\Users\\acer\\workspace\\encode\\new4\\test2";
          BufferedReader br = new BufferedReader(new InputStreamReader(
                  new FileInputStream(file1)));
          BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(
                  new FileOutputStream(file2)));
  String line;
          while((line=br.readLine())!=null){
             System.out.println(line);//一次读一行,并不能识别换行
             bw.write(line);
             //单独写出换行操作
             bw.newLine();
             bw.flush();
 }
                 br.close();
         bw.close();
 }        

转换流

作用:将字节流和字符流进行转换。

InputStreamReader作用:字节输入流转换为字符输入流;

OutputStreamWriter作用:字符输出流转为字节输出流;

File file= new file("a.text");
File target = new File("b.text");


FileInputStream fis= new FileInputStream(file);
InputSteamReader isr = new InputStreamReader(fis,"Unicode");


FileOutputStream fos = new FileOutputStream(target,ture);
OutputStreamWriter osw = new FileOutputWriter(fos,"GBK");

/
  


isr.close();
osw.close();

补充: 

注:文件使用后记得关闭

System.in&&System.out&&Scanner

标准输入流标准输出流

例子:

Scanner input = new Scanner(System.in);

预习不充分的地方: 

数据流

数据流:用来操作基本数据类型和字符串

DataInputStream:将文件中存储的基本数据类型和字符串写入内存的变量中;

DataOutputStream:将内存中的基本数据类型和字符串的变量写出文件中;

对象流

用来存储和读取基本数据类型数据或对象的处理流。

对象流:ObjectInputStream,ObjectInputStream。

Example:

import java.io.*;
class Student implements Serializable{
private String name;
private int age;

public Student(String name,int age){
this.name=name;
this.age=age;
}

public void greeting(){
System.out.println("hello ,my name is "+name);
}

public String toString(){
return "Student["+name+","+age+"]";
    }
}
public class ObjectOutTest{
public static void main(String args[]){
ObjectOutputStream oos=null;
try{
oos=new ObjectOutputStream(
new FileOutputStream("student.txt"));
Student s1=new Student("Jerry",24);
Student s2=new Student("Andy",33);

oos.writeObject(s1);
oos.writeObject(s2);
}catch(Exception e){
e.printStackTrace();
}finally{
if(oos!=null)
try{
oos.close();
}catch(Exception e){
e.printStackTrace();
            }
        }
    }
}

import java.io.*;
public class ObjectInTest{
public static void main(String args[]){
ObjectInputStream ois=null;
Student s=null;
try{
ois=new ObjectInputStream(
new FileInputStream("student.txt"));
System.out.println("--------------------");
s=(Student)ois.readObject();
System.out.println(s);
s.greeting();
System.out.println("--------------------");
s=(Student)ois.readObject();
System.out.println(s);
s.greeting();
}catch(Exception e){
e.printStackTrace();
}finally{
if(ois!=null)
try{
ois.close();
}catch(Exception e){
e.printStackTrace();
            }
        }
     }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值