JAVA学习-IO流

IO流

FileInputStream

文件输入流

将文件中的字符输入到控制台

package com.myIO.demo01;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Demo01 {
    public static void main(String[] args) {
        Demo01 demo01 = new Demo01();
        demo01.readMyFile();
    }
    public void readMyFile(){
        String filPath = "G:\\java\\hi.txt";
        FileInputStream fileInputStream =null;
        int myRead = 0;
        try {
            fileInputStream = new FileInputStream(filPath);
            //read方法,返回为-1表示读取结束
            while((myRead = fileInputStream.read()) != -1){
                System.out.print((char)myRead);//这里把myRead转成char输出
            }
        } catch (IOException e) {//read方法也有异常需要catch,所以这里直接改成IO 异常
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
        }
    }
}

**这里,我的hi.txt文件放在g:\java\hi.txt 转义符关系要写成 \\ **

read(byte[])方法

可以指定一次读取所读取的字节数,如果读取正常,返回实际读取的个数,如果返回-1表示读取完毕

package com.myIO.demo02;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Demo02 {
    public static void main(String[] args) {
        Demo02 demo02 = new Demo02();
        demo02.myFileRead();

    }

    public void myFileRead(){
        String filePath = "g:\\java\\hi.txt";
        FileInputStream fileInputStream = null;
        int readLen = 0;
        byte[] bytes = new byte[5];//五个五个读取
        try {
            fileInputStream = new FileInputStream(filePath);
            while ((readLen = fileInputStream.read(bytes)) != -1){//这里是将读取到的长度返回给readLen,并读取五个字节给bytes
                System.out.println(new String(bytes,0,readLen));//利用了String的构造器,从bytes中索引0开始读取,长度为readLen
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }
}

FileOutputStream

写入数据到文件,如果文件不存在,则会创建这个文件

三种方法写入文件

第一种,字节写入

package com.myIO.demo03;

import java.io.FileOutputStream;
import java.io.IOException;

public class Demo03 {
    public static void main(String[] args) {
        Demo03 demo03 = new Demo03();
        demo03.myFileWrite();
    }
    public void myFileWrite(){
        String filePath = "g:\\java\\a.txt";
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(filePath);
            fileOutputStream.write('a');
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

第二种,字符串写入

利用字符数组

package com.myIO.demo03;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Demo03 {
    public static void main(String[] args) {
        Demo03 demo03 = new Demo03();
        demo03.myFileWrite();
    }
    public void myFileWrite(){
        String filePath = "g:\\java\\a.txt";
        FileOutputStream fileOutputStream = null;
        String a = "hello,world";
        try {
            fileOutputStream = new FileOutputStream(filePath);
            fileOutputStream.write(a.getBytes());//getBytes方法,将字符串转换为字节数组
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

第三种,规定长度的字符串写入

package com.myIO.demo03;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Demo03 {
    public static void main(String[] args) {
        Demo03 demo03 = new Demo03();
        demo03.myFileWrite();
    }
    public void myFileWrite(){
        String filePath = "g:\\java\\a.txt";
        FileOutputStream fileOutputStream = null;
        String a = "hello,world";
        try {
            fileOutputStream = new FileOutputStream(filePath);
            fileOutputStream.write(a.getBytes(),0,3);//从索引0开始写入,长度为3
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

**注意,以上写入均为覆盖写入,也就是每次写入都是全新的内容。如想要接着上次内容写入,则需要:

fileOutputStream = new FileOutputStream(filePath,true);//默认为false,不接着写入

文件拷贝

利用FileInputStream与FileOutputStream进行文件的拷贝

package com.myIO.demo04;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo04 {
    public static void main(String[] args) {
        Demo04 demo04 = new Demo04();
        demo04.myFileCopy();

    }
    public void myFileCopy() {
        String srcFilePath = "g:\\java\\1.jpg";
        String destFilePath = "g:\\java\\2.jpg";
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream =null;
        byte[] bytes = new byte[1024];
        int fileLen = 0;
        try {
            fileInputStream = new FileInputStream(srcFilePath);
            fileOutputStream = new FileOutputStream(destFilePath);
            while ((fileLen = fileInputStream.read(bytes)) != -1){
                fileOutputStream.write(bytes,0,fileLen);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

FileReader

read方法,每次读取一个字符(注意,不是字节,所以可以直接读取汉字)

package com.myIO.demo05;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Demo05 {
    public static void main(String[] args) {
        Demo05 demo05 = new Demo05();
        demo05.myRader();

    }

    public void myRader(){
        FileReader fileReader = null;
        int myread = 0;
        String filePath = "g:\\java\\hi.txt";
        try {
            fileReader = new FileReader(filePath);
            while ((myread = fileReader.read() )!= -1){
                System.out.print((char)myread);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

也可以读取字符数组,提高效率

package com.myIO.demo05;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Demo05 {
    public static void main(String[] args) {
        Demo05 demo05 = new Demo05();
        demo05.myRader();

    }

    public void myRader(){
        FileReader fileReader = null;
        String filePath = "g:\\java\\hi.txt";
        int readerLen = 0;
        char[] chars = new char[5];
        try {
            fileReader = new FileReader(filePath);
            while ((readerLen = fileReader.read(chars) )!= -1){
                System.out.print(new String(chars,0,readerLen));//利用了String的构造器来直接输出字符数组
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

FileWriter

字符写入,可以写入字符,字符数组,字符串。

但必须要有close(),或则flash()才能将数据完成写入!!!

BufferedReader

包装流/处理流

package com.myIO.demo07;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Demo07 {
    public static void main(String[] args) {
        Demo07 demo07 = new Demo07();
        demo07.bufRead();

    }
    public void bufRead(){
        BufferedReader bufferedReader = null;
        String readPath = "g:\\java\\hi.txt";
        String myRead = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(readPath));
            while ((myRead = bufferedReader.readLine()) != null){//readLine方法,按行读取,高效率
                System.out.println(myRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedReader.close();//直接关闭包装流就行,源码可查是关闭了传进去的FileReader
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

BufferedWriter

package com.myIO.demo08;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Demo08 {
    public static void main(String[] args) {
        Demo08 demo08 = new Demo08();
        demo08.bufWriter();
    }
    public void bufWriter(){
        BufferedWriter bufferedWriter = null;
        String filePath = "g:\\java\\a.txt";
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(filePath,true));//如果需要接着输写,需要在FileWriter构造器里面append 填入true,默认false
            bufferedWriter.write("乌拉");
            bufferedWriter.newLine();//加入新一行,自动适合系统
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedWriter.close();//这一步忘了就完了!一定要记住!!!
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

文件拷贝

BufferedReader与BufferedWriter是读写字符的,不要用它们去处理二进制文件,例如声音,doc,pdf等

BufferedOutputStream/BufferedInputStream

可以处理二进制文件

package com.myIO.demo09;

import java.io.*;

public class Demo09 {
    public static void main(String[] args) {
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        byte[] bytes = new byte[1024];
        int srcLen = 0;
        String srcPath = "g:\\java\\1.jpg";
        String  destPath = "g:\\java\\3.jpg";
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream(srcPath));
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destPath));
            while ((srcLen = bufferedInputStream.read(bytes)) != -1){
                bufferedOutputStream.write(bytes,0,srcLen);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }

    }
}

对象处理流

ObjectInputStream ObjectOutputStream

可以实现对数据和类型的序列化以及反序列化

注意

  • 读写顺序要一致
  • 要求实现序列化或反序列化对象,需要实现Serializable接口
  • 序列化的类中建议添加SerialVersionUID,为了提高版本的兼容性
  • 序列化对象时,默认将里面所有属性都进行序列化,但除了static或transient修饰的成员
  • 序列化对象时,要求里面属性的类型也需要实现序列化接口
  • 序列化具备可继承性,也就是如果某类已经是吸纳了序列化,则它的所有子类也实现了序列化

转换流

InputStreamReader

可以指定读取文件编码,将字节流转换成字符流。

OutputStramWriter

按照指定编码保存文件,字节流转换为字符流

打印流

打印流只有输出流,没有输入流

PrintStream

PrintStream out = System.out;
out.print("hello");//输出在屏幕
/////////////////////
//也可以利用print底层使用的write来输出
out.write("hello".getBytes());//这里要用getBytes(),因为这是字节输出流

默认输出的位置为屏幕,但可以修改:

System.setOut(new PrintStream("g:\\txy.txt"));//修改输出位置到了g:\txy.txt
System.out.println("hello");//输出位置在g:\txy.txt

PrintWriter

记住Writer都要close

Properties

读取配置文件

//Properties pro = (Reader);

遍历配置文件

pro.list(System.out);

根据键获取值

pro.getProperty("name");//返回一个String

创建

Properties pro = new Properties;
pro.setProperties("key","value");
pro.setProperties("...","...");
//将k-v保存到配置文件
pro.store(new FiloutputStream("src\\mysql2.properties"),null);

如果该文件没有对应的key就是创建

有对应的key就是修改

posted @ 2022-03-02 18:17  老哥不老  阅读(31)  评论(0编辑  收藏  举报