【JAVA SE】—— 文件IO流 (经常忘记的知识点总结)2

简介: 【JAVA SE】—— 文件IO流 (经常忘记的知识点总结)2

6、打印流

1.何谓打印流

平时我们在控制台打印输出,是调用print方法和println方法完成的,各位用了这么久的输出语句肯定没想过这两个方法都来自于java.io.PrintStream类吧,哈哈。该类能够方便地打印各种数据类型的值,是一种便捷的输出方式。


2.打印流分类:

字节打印流PrintStream,字符打印流PrintWriter


3.打印流特点:

A:只操作目的地,不操作数据源
B:可以操作任意类型的数据
C:如果启用了自动刷新,在调用println()方法的时候,能够换行并刷新
D:可以直接操作文件

这个时候有同学就要问了,哪些流可以直接操作文件呢?答案很简单,如果该流的构造方法能够同时接收File和String类型的参数,一般都是可以直接操作文件的!

PrintStream是OutputStream的子类,PrintWriter是Writer的子类,两者处于对等的位置上,所以它们的API是非常相似的。二者区别无非一个是字节打印流,一个是字符打印流。

4.字节输出打印流PrintStream复制文本文件

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

public class PrintStreamDemo {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new FileReader("copy.txt"));
        PrintStream ps=new PrintStream("printcopy.txt");
        String line;
        while((line=br.readLine())!=null) {
            ps.println(line);
        }
        br.close();
        ps.close();
    }
}

5.字符输出打印流PrintWriter复制文本文件

  •  import java.io.BufferedReader;
     import java.io.FileReader;
     import java.io.FileWriter;
     import java.io.IOException;
     import java.io.PrintWriter;
     /**
     
      * 使用打印流复制文本文件
        */
        public class PrintWriterDemo {
        public static void main(String[] args) throws IOException {
            BufferedReader br=new BufferedReader(new FileReader("aa.txt"));
            PrintWriter pw=new PrintWriter("printcopyaa.txt");
            String line;
            while((line=br.readLine())!=null) {
                pw.println(line);
            }
            br.close();
            pw.close();
        }
        }
    

7、Properties概述

java.util.Properties 继承于Hashtable ,来表示一个持久的属性集。它使用键值结构存储数据,每个键及其对应值都是一个字符串。该类也被许多Java类使用,比如获取系统属性时,System.getProperties 方法就是返回一个Properties对象。


Properties类

构造方法

public Properties() :创建一个空的属性列表。

基本的存储方法

  • public Object setProperty(String key, String value) : 保存一对属性。
  • public String getProperty(String key) :使用此属性列表中指定的键搜索属性值。
  • public Set stringPropertyNames() :所有键的名称的集合。
public class ProDemo {
    public static void main(String[] args) throws FileNotFoundException {
        // 创建属性集对象
        Properties properties = new Properties();
        // 添加键值对元素
        properties.setProperty("filename", "a.txt");
        properties.setProperty("length", "209385038");
        properties.setProperty("location", "D:\\a.txt");
        // 打印属性集对象
        System.out.println(properties);
        // 通过键,获取属性值
        System.out.println(properties.getProperty("filename"));
        System.out.println(properties.getProperty("length"));
        System.out.println(properties.getProperty("location"));

        // 遍历属性集,获取所有键的集合
        Set<String> strings = properties.stringPropertyNames();
        // 打印键值对
        for (String key : strings ) {
              System.out.println(key+" -- "+properties.getProperty(key));
        }
    }
}

输出结果:

{filename=a.txt, length=209385038, location=D:\a.txt}
a.txt
209385038
D:\a.txt
filename -- a.txt
length -- 209385038
location -- D:\a.txt

与流相关的方法

public void load(InputStream inStream): 从字节输入流中读取键值对。

参数中使用了字节输入流,通过流对象,可以关联到某文件上,这样就能够加载文本中的数据了。现在文本数据格式如下:

filename=Properties.txt
length=123
location=C:\Properties.txt

加载代码演示:

public class ProDemo {
    public static void main(String[] args) throws FileNotFoundException {
        // 创建属性集对象
        Properties pro = new Properties();
        // 加载文本中信息到属性集
        pro.load(new FileInputStream("Properties.txt"));
        // 遍历集合并打印
        Set<String> strings = pro.stringPropertyNames();
        for (String key : strings ) {
              System.out.println(key+" -- "+pro.getProperty(key));
        }
     }
}

输出结果:

filename -- Properties.txt
length -- 123
location -- C:\Properties.txt

文本中的数据,必须是键值对形式,可以使用空格、等号、冒号等符号分隔。


目录
相关文章
|
1天前
|
Java 机器人 程序员
Java中的文件I/O操作:流、读写和NIO详解
Java中的文件I/O操作:流、读写和NIO详解
|
1天前
|
XML Java 数据格式
java删除xml文件内容
java删除xml文件内容
3 0
|
1天前
|
XML Java 数据格式
java创建xml文件内容
java创建xml文件内容
7 0
|
1天前
|
XML Java 数据格式
java解析xml文件内容
java解析xml文件内容
4 0
|
1天前
|
存储 Java 机器人
Java中的IO操作技巧与性能优化
Java中的IO操作技巧与性能优化
|
1天前
|
存储 Java 机器人
如何在Java中实现文件压缩与解压?
如何在Java中实现文件压缩与解压?
|
1天前
|
存储 自然语言处理 Java
Java-File类与IO流(2)
Java-File类与IO流(2)
6 0
|
1天前
|
安全 Java
java线程之List集合并发安全问题及解决方案
java线程之List集合并发安全问题及解决方案
7 1
|
1天前
|
Java
java线程之定制化通信(多轮顺序打印问题)
java线程之定制化通信(多轮顺序打印问题)
6 1
|
1天前
|
缓存 监控 安全
深入理解Java中的线程池和并发编程
深入理解Java中的线程池和并发编程