Java教程

java.io流

本文主要是介绍java.io流,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

io流

常用的文件操作

创建文件

//1.
File file = new File(pathname);
file.createNewFile();
//2.
File file = new File(parentFile(File), fileName(String));
file.createNewFile();
//3.
File file = new File(parentPath(String), fileName(String));

获取文件信息

getName(),getAbsolutePath(),getParent(),length(),exists(),isFile(),isDirectory()

创建/删除文件

mkdir(),mkdirs(),delete()

流的分类

字节流

字节输入流(InputStream)

FileInputStream

演示

public class Test01 {
    public static void main(String[] args) {
        String name = "src\\1.txt";
        int read = 0;

        try {
            FileInputStream fileInputStream = new FileInputStream(name);

            while((read = fileInputStream.read())!=-1){
                System.out.print((char)read);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        System.out.println();
        try {
            byte[] bytes = new byte[1024];
            FileInputStream fileInputStream = new FileInputStream(name);

            while((read = fileInputStream.read(bytes))!=-1){
                System.out.println(new String(bytes));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

字节输出流(OutputStream)

FileOutputStream

实例

String name = "src\\1.txt";

        try{
            FileOutputStream fileOutputStream = new FileOutputStream(name);

            fileOutputStream.write("asdf".getBytes());

        }catch(Exception e){
            e.printStackTrace();
        }

字符流

字符输入流(Reader)

FileReader
//1.第一种连接方式
int temp = 0;
        FileReader fileReader = new FileReader("1.txt");

        while((temp = fileReader.read()) != -1){
            System.out.print((char)temp);
        }
        fileReader.close();

//2.第二种两种方式
char[] temp = new char[8];
        int i = 0;
        FileReader fileReader = new FileReader("1.txt");

        while((i = fileReader.read(temp)) != -1){
            System.out.print(new String(temp, 0, i));
        }
        fileReader.close();

字符输出流(Writer)

FileWriter
 String thing = "哈哈哈";

        FileWriter j = new FileWriter("1.txt", true);

        j.write(thing.toCharArray());
        j.close();

包装流

包装流(buffered)

字符处理流

注意:

bufferedReader和bufferedWriter是按照字符操作,不要去操作二进制文件例如声音,视频,pdf,doc文档等

BufferedReader(包装reader实现类的节点流)

    String name = "1.txt";
        //创建字节流
        FileReader reader = new FileReader(name);

        BufferedReader buffer = new BufferedReader(reader);
        String str;
        int count = 0;

        while((str = buffer.readLine())!=null){
            System.out.println(str);
        }
        
        //关闭包装流,里边的字节流底层自动关闭
        buffer.close();

BufferedWriter(包装writer实现类的节点流)

String name = "1.txt";

        FileWriter writer = new FileWriter(name, true);

        BufferedWriter buffer = new BufferedWriter(writer);

        buffer.write("你好爱豆if哈子双方都一杭州市地方");

        buffer.newLine();
        buffer.write("你好爱豆if哈子双方都一杭州市地方");

        buffer.newLine();
        buffer.write("你好爱豆if哈子双方都一杭州市地方");

        buffer.newLine();

        buffer.flush();
    

字节处理流

BufferedInputStream

BufferedOutputStream

实例

String s1 = "1.txt";//源文件
        String s2 = "4.txt";//拷贝文件

        FileInputStream fileInputStream = new FileInputStream(s1);
        FileOutputStream fileOutputStream = new FileOutputStream(s2);

        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

        byte[] bytes = new byte[1024];

        int len = 0;

        while((len = bufferedInputStream.read(bytes)) != -1){
            bufferedOutputStream.write(bytes, 0, len);
        }

        bufferedInputStream.close();
        bufferedOutputStream.close();

序列化

  • 序列化就是在保存数据时,保存数据的值和数据类型

  • 反序列化就是在恢复数据时,恢复数据的值和数据类型

  • 如果需要类可序列化,需要该类实现以下两个接口之一

    • Serializable //标记接口
    • Externalizable //该接口需要实现方法

包装流(Object)

ObjectOutStream

String name = "5.txt";

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(name));

        objectOutputStream.writeInt(100);
        objectOutputStream.writeBoolean(true);
        objectOutputStream.writeChar('a');
        objectOutputStream.writeUTF("wangafasdf");

        objectOutputStream.close();

ObjectInputStream

String name = "5.txt";

        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(name));

        System.out.println(objectInputStream.readInt());
        System.out.println(objectInputStream.readBoolean());
        System.out.println(objectInputStream.readChar());
        System.out.println(objectInputStream.readUTF());
        objectInputStream.close();

注意事项

  1. 读写顺序要一致
  2. 要求实现序列化或反序列化对象,需要实现Serializable
  3. 序列化对选时,默认奖励不拿所有属性都序列化,但除了static或者transient修饰的对象修饰的成员
  4. 序列化对象时,要求里面所有属性的烈性也需要实现序列化接口
  5. 序列化的类中建议添加SeriaIVersionUID,为了提高版本的兼容性
  6. 序列化具备可继承性,也就是如果某类已经实现序列化,则他的所有子类也已经默认实现了序列化.

转化流

  • 转换流InputStreamReader和OutputSteamWriter

  • InputStreamReader:Reader的子类,可以将InputStream包装成Reader

  • OutputStreamWriter:Writer的子类,实现将OutputStream包装成Writer

  • 档处理纯文本数据时,如果使用字符流效率更高,并且可以有效解决中文的问题,所以建议将字节流转换成字符流

  • 可以在使用指定编码格式比如utf-8,gbk,gb2312,ISO8859-1

InputStreamReader

String name = "6.txt";

        BufferedReader j = new BufferedReader(new InputStreamReader(new FileInputStream(name), "gbk"));

        String str;
        while((str = j.readLine()) != null){
            System.out.println(str);
        }
        j.close();
//说明:文件是gbk编码,idea是utf-8,所以使用转化流转换

OutputStreamWriter

String name = "7.txt";
        BufferedWriter k = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(name), "utf-8"));
        k.write("和嘿嘿");
        k.close();

//将字符串以utf-8的方式存储

打印流

PrintStream

String name = "8.txt";
        System.setOut(new PrintStream(name));
        PrintStream i = System.out;
        i.println("afsasdfads");
        i.close();
//改变System.out指向,字符打印到文件中

printWriter

String name = "9.txt";

        PrintWriter i = new PrintWriter(new PrintWriter(name));
        i.println("afsasdfads");
        i.close();

配置文件Properties

读取

String name = "db.properties";

        Properties properties = new Properties();
        properties.load(new FileReader(name));


        properties.list(System.out);

        System.out.println(properties.getProperty("username"));

创建/修改

当文件没有时为创建,有文件时为修改

String name = "new.properties";

        Properties properties = new Properties();

        properties.setProperty("username", "谁谁谁");
        properties.setProperty("password", "123456");
        properties.store(new FileWriter(name), null);
这篇关于java.io流的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!