字节流、字符流 概述 根据数据的类型分为:字节流 和字符流 。
字节流 :以字节为单位,读写数据的流。
字符流 :以字符为单位,读写数据的流。
根据数据的流向分为:输入流 和输出流 。
输入流 :把数据从其他设备
上读取到内存
中的流。
输出流 :把数据从内存
中写出到其他设备
上的流。
字节流 概述 一切文件数据(文本、图片、视频等)在存储时,都是以二进制数字的形式保存,都一个一个的字节,那么传输时一样如此。所以,字节流可以传输任意文件数据。在操作流的时候,我们要时刻明确,无论使用什么样的流对象,底层传输的始终为二进制数据。
字节输出流(OutputStream) java.io.OutputStream
抽象类是表示字节输出流的所有类的超类,将指定的字节信息写出到目的地。它定义了字节输出流的基本共性功能方法。
public void close()
:关闭此输出流并释放与此流相关联的任何系统资源。
public void flush()
:刷新此输出流并强制任何缓冲的输出字节被写出。
public void write(byte[] b)
:将 b.length字节从指定的字节数组写入此输出流。
public void write(byte[] b, int off, int len)
:从指定的字节数组写入 len字节,从偏移量 off开始输出到此输出流。
public abstract void write(int b)
:将指定的字节输出流。
小贴士:
close方法,当完成流的操作时,必须调用此方法,释放系统资源。
FileOutputStream类 java.io.FileOutputStream
类是文件输出流,用于将数据写出到文件。
构造方法
public FileOutputStream(File file)
:创建文件输出流以写入由指定的 File对象表示的文件。
public FileOutputStream(String name)
: 创建文件输出流以指定的名称写入文件。
当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有这个文件,会创建该文件。如果有这个文件,会清空这个文件的数据。
1 2 3 4 5 6 7 8 9 10 public class FileOutputStreamConstructor throws IOException { public static void main (String[] args) { File file = new File("a.txt" ); FileOutputStream fos = new FileOutputStream(file); FileOutputStream fos = new FileOutputStream("b.txt" ); } }
写出字节数据 1. 写出字节 :write(int b)
方法,每次可以写出一个字节数据,
代码使用演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class FOSWrite { public static void main (String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("fos.txt" ); fos.write(97 ); fos.write(98 ); fos.write(99 ); fos.close(); } } 输出结果: abc
2. 写出字节数组 :write(byte[] b)
,每次可以写出数组中的数据,
代码使用演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class FOSWrite { public static void main (String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("fos.txt" ); byte [] b = "你好!" .getBytes(); fos.write(b); fos.close(); } } 输出结果: 你好!
3. 写出指定长度字节数组 :write(byte[] b, int off, int len)
,每次写出从off索引开始,len个字节,
代码使用演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class FOSWrite { public static void main (String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("fos.txt" ); byte [] b = "abcde" .getBytes(); fos.write(b,2 ,2 ); fos.close(); } } 输出结果: cd
数据追加续写
经过以上的演示,每次程序运行,创建输出流对象,都会清空目标文件中的数据。如何保留目标文件中数据,还能继续添加新数据呢?
public FileOutputStream(File file, boolean append)
: 创建文件输出流以写入由指定的 File对象表示的文件。
public FileOutputStream(String name, boolean append)
: 创建文件输出流以指定的名称写入文件。
这两个构造方法,参数中都需要传入一个boolean类型的值,true
表示追加数据,false
表示清空原有数据。这样创建的输出流对象,就可以指定是否追加续写了,代码使用演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class FOSWrite { public static void main (String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("fos.txt" ,true ); byte [] b = "abcde" .getBytes(); fos.write(b); fos.close(); } } 文件操作前:cd 文件操作后:cdabcde
写出换行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public class FOSWrite { public static void main (String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("fos.txt" ); byte [] words = {97 ,98 ,99 ,100 ,101 }; for (int i = 0 ; i < words.length; i++) { fos.write(words[i]); fos.write("\r\n" .getBytes()); } fos.close(); } } 输出结果: a b c d e
java.io.InputStream
抽象类是表示字节输入流的所有类的超类,可以读取字节信息到内存中。它定义了字节输入流的基本共性功能方法。
public void close()
:关闭此输入流并释放与此流相关联的任何系统资源。
public abstract int read()
: 从输入流读取数据的下一个字节。
public int read(byte[] b)
: 从输入流中读取一些字节数,并将它们存储到字节数组 b中 。
java.io.FileInputStream
类是文件输入流,从文件中读取字节。
构造方法
FileInputStream(File file)
: 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。
FileInputStream(String name)
: 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。
当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有该文件,会抛出FileNotFoundException
。
1 2 3 4 5 6 7 8 9 10 public class FileInputStreamConstructor throws IOException { public static void main (String[] args) { File file = new File("a.txt" ); FileInputStream fos = new FileInputStream(file); FileInputStream fos = new FileInputStream("b.txt" ); } }
读取字节数据 读取字节 :read
方法,每次可以读取一个字节的数据,提升为int类型,读取到文件末尾,返回-1
,代码使用演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public class FISRead { public static void main (String[] args) throws IOException { FileInputStream fis = new FileInputStream("read.txt" ); int read = fis.read(); System.out.println((char ) read); read = fis.read(); System.out.println((char ) read); read = fis.read(); System.out.println((char ) read); read = fis.read(); System.out.println((char ) read); read = fis.read(); System.out.println((char ) read); read = fis.read(); System.out.println( read); fis.close(); } } 输出结果: a b c d e -1
循环改进读取方式,代码使用演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class FISRead { public static void main (String[] args) throws IOException { FileInputStream fis = new FileInputStream("read.txt" ); int b ; while ((b = fis.read())!=-1 ) { System.out.println((char )b); } fis.close(); } } 输出结果: a b c d e
使用字节数组读取 :read(byte[] b)
,每次读取b的长度个字节到数组中,返回读取到的有效字节个数,读取到末尾时,返回-1
,代码使用演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class FISRead { public static void main (String[] args) throws IOException { FileInputStream fis = new FileInputStream("read.txt" ); int len ; byte [] b = new byte [2 ]; while (( len= fis.read(b))!=-1 ) { System.out.println(new String(b)); } fis.close(); } } 输出结果: ab cd ed
错误数据d
,是由于最后一次读取时,只读取一个字节e
,数组中,上次读取的数据没有被完全替换,所以要通过len
,获取有效的字节,代码使用演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class FISRead { public static void main (String[] args) throws IOException { FileInputStream fis = new FileInputStream("read.txt" ); int len ; byte [] b = new byte [2 ]; while (( len= fis.read(b))!=-1 ) { System.out.println(new String(b,0 ,len)); } fis.close(); } } 输出结果: ab cd e
案例练习:图片复制 代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public class Copy { public static void main (String[] args) throws IOException { FileInputStream fis = new FileInputStream("D:\\test.jpg" ); FileOutputStream fos = new FileOutputStream("test_copy.jpg" ); byte [] b = new byte [1024 ]; int len; while ((len = fis.read(b))!=-1 ) { fos.write(b, 0 , len); } fos.close(); fis.close(); } }
流的关闭原则:先开后关,后开先关。
字符流 当使用字节流读取文本文件时,可能会有一个小问题。就是遇到中文字符时,可能不会显示完整的字符,那是因为一个中文字符可能占用多个字节存储。所以Java提供一些字符流类,以字符为单位读写数据,专门用于处理文本文件。
字符输入流 java.io.Reader
抽象类是表示用于读取字符流的所有类的超类,可以读取字符信息到内存中。它定义了字符输入流的基本共性功能方法。
public void close()
:关闭此流并释放与此流相关联的任何系统资源。
public int read()
: 从输入流读取一个字符。
public int read(char[] cbuf)
: 从输入流中读取一些字符,并将它们存储到字符数组 cbuf中 。
FileReader类 java.io.FileReader
类是读取字符文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。
构造方法
FileReader(File file)
: 创建一个新的 FileReader ,给定要读取的File对象。
FileReader(String fileName)
: 创建一个新的 FileReader ,给定要读取的文件的名称。
当你创建一个流对象时,必须传入一个文件路径。类似于FileInputStream 。
1 2 3 4 5 6 7 8 9 10 public class FileReaderConstructor throws IOException { public static void main (String[] args) { File file = new File("a.txt" ); FileReader fr = new FileReader(file); FileReader fr = new FileReader("b.txt" ); } }
读取字符数据 读取字符 :read
方法,每次可以读取一个字符的数据,提升为int类型,读取到文件末尾,返回-1
,循环读取,代码使用演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class FRRead { public static void main (String[] args) throws IOException { FileReader fr = new FileReader("read.txt" ); int b ; while ((b = fr.read())!=-1 ) { System.out.println((char )b); } fr.close(); } }
使用字符数组读取 :read(char[] cbuf)
,每次读取b的长度个字符到数组中,返回读取到的有效字符个数,读取到末尾时,返回-1
,代码使用演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class FRRead { public static void main (String[] args) throws IOException { FileReader fr = new FileReader("read.txt" ); int len ; char [] cbuf = new char [2 ]; while ((len = fr.read(cbuf))!=-1 ) { System.out.println(new String(cbuf)); } fr.close(); } }
获取有效的字符改进,代码使用演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class FISRead { public static void main (String[] args) throws IOException { FileReader fr = new FileReader("read.txt" ); int len ; char [] cbuf = new char [2 ]; while ((len = fr.read(cbuf))!=-1 ) { System.out.println(new String(cbuf,0 ,len)); } fr.close(); } }
字符输出流 java.io.Writer
抽象类是表示用于写出字符流的所有类的超类,将指定的字符信息写出到目的地。它定义了字节输出流的基本共性功能方法。
void write(int c)
写入单个字符。
void write(char[] cbuf)
写入字符数组。
abstract void write(char[] cbuf, int off, int len)
写入字符数组的某一部分,off数组的开始索引,len写的字符个数。
void write(String str)
写入字符串。
void write(String str, int off, int len)
写入字符串的某一部分,off字符串的开始索引,len写的字符个数。
void flush()
刷新该流的缓冲。
void close()
关闭此流,但要先刷新它。
FileWriter类 java.io.FileWriter
类是写出字符到文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。
构造方法
FileWriter(File file)
: 创建一个新的 FileWriter,给定要读取的File对象。
FileWriter(String fileName)
: 创建一个新的 FileWriter,给定要读取的文件的名称。
当你创建一个流对象时,必须传入一个文件路径,类似于FileOutputStream。
1 2 3 4 5 6 7 8 9 10 public class FileWriterConstructor { public static void main (String[] args) throws IOException { File file = new File("a.txt" ); FileWriter fw = new FileWriter(file); FileWriter fw = new FileWriter("b.txt" ); } }
写出数据 写出字符 :write(int b)
方法,每次可以写出一个字符数据,代码使用演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class FWWrite { public static void main (String[] args) throws IOException { FileWriter fw = new FileWriter("fw.txt" ); fw.write(97 ); fw.write('b' ); fw.write('C' ); fw.write(30000 ); } } 输出结果: abC田
关闭和刷新 因为内置缓冲区的原因,如果不关闭输出流,无法写出字符到文件中。但是关闭的流对象,是无法继续写出数据的。如果我们既想写出数据,又想继续使用流,就需要flush
方法了。
flush
:刷新缓冲区,流对象可以继续使用。
close
:先刷新缓冲区,然后通知系统释放资源。流对象不可以再被使用了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class FWWrite { public static void main (String[] args) throws IOException { FileWriter fw = new FileWriter("fw.txt" ); fw.write('刷' ); fw.flush(); fw.write('新' ); fw.flush(); fw.write('关' ); fw.close(); fw.write('闭' ); fw.close(); } }
写出数据 写出字符数组 :write(char[] cbuf)
和 write(char[] cbuf, int off, int len)
,每次可以写出字符数组中的数据,用法类似FileOutputStream,代码使用演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class FWWrite { public static void main (String[] args) throws IOException { FileWriter fw = new FileWriter("fw.txt" ); char [] chars = "黑马程序员" .toCharArray(); fw.write(chars); fw.write(b,2 ,2 ); fos.close(); } }
写出字符串 :write(String str)
和 write(String str, int off, int len)
,每次可以写出字符串中的数据,更为方便,代码使用演示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class FWWrite { public static void main (String[] args) throws IOException { FileWriter fw = new FileWriter("fw.txt" ); String msg = "黑马程序员" ; fw.write(msg); fw.write(msg,2 ,2 ); fos.close(); } }
续写和换行 :操作类似于FileOutputStream。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class FWWrite { public static void main (String[] args) throws IOException { FileWriter fw = new FileWriter("fw.txt" ,true ); fw.write("我叫" ); fw.write("\r\n" ); fw.write("仝少坤" ); fw.close(); } } 输出结果: 我叫 仝少坤
Properties类 可以作为工具类的配置文件使用
public Properties()
:创建一个空的属性列表。
基本的存储方法
public Object setProperty(String key, String value)
: 保存一对属性。
public String getProperty(String key)
:使用此属性列表中指定的键搜索属性值。
public Set<String> stringPropertyNames()
:所有键的名称的集合。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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)
: 从字节输入流中读取键值对。
参数中使用了字节输入流,通过流对象,可以关联到某文件上,这样就能够加载文本中的数据了。文本数据格式:
1 2 3 filename =a.txt length =209385038 location =D:\a.txt
加载代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class ProDemo2 { public static void main (String[] args) throws FileNotFoundException { Properties pro = new Properties(); pro.load(new FileInputStream("read.txt" )); Set<String> strings = pro.stringPropertyNames(); for (String key : strings ) { System.out.println(key+" -- " +pro.getProperty(key)); } } } 输出结果: filename -- a.txt length -- 209385038 location -- D:\a.txt