Java IO学习笔记二

时间:2021-10-11 11:41:08

Java IO学习笔记二

流的概念

  • 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使用输出流完成。
  • 程序中的输入输出都是以流的形式保存的,流中保存的实际上全都是字节文件。

字节流和字符流

  • 实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作文件
  • 在java.io包中操作文件内容的主要有两大类:字节流、字符流,两类都分为输入和输出操作。在字节流中输出数据主要是使用OutputStream完成,输入使的是InputStream,在字符流中输出主要是使用Writer类完成,输入流主要使用Reader类完成。(这四个都是抽象类)

操作流程

在Java中IO操作也是有相应步骤的,以文件操作为例,主要的操作流程如下:

  1. 使用File类打开一个文件
  2. 通过字节流或字符流的子类,指定输出的位置
  3. 进行读/写操作
  4. 关闭输入/输出

字节流

字节流主要是操作byte类型数据,以byte数组为准,主要操作类就是OutputStreamInputStream

FileOutputStream

  • 文件输出流是用于将数据写入 File 或 FileDescriptor 的输出流。文件是否可用或能否可以被创建取决于基础平台。特别是某些平台一次只允许一个 FileOutputStream(或其他文件写入对象)打开文件进行写入。在这种情况下,如果所涉及的文件已经打开,则此类中的构造方法将失败。
  • FileOutputStream 用于写入诸如图像数据之类的原始字节的流。要写入字符流,请考虑使用 FileWriter。
  • 主要的功能就是用来向文件中写入内容的

构造函数

  • FileOutputStream(File file) 创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
  • FileOutputStream(File file, boolean append) 如果在文件后面追加内容,如果append为true则追加内容
  • FileOutputStream(String name) 创建一个向具有指定名称的文件中写入数据的输出文件流。
  • FileOutputStream(String name, boolean append) 创建一个向具有指定 name 的文件中写入数据的输出文件流。

常用的方法

  • close() 关闭文件输出流
  • void write(byte[] b) 将 b.length 个字节从指定 byte 数组写入此文件输出流中。
  • void write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流,这里需要注意的是中文所占字节数为3,英文所占字节数为1
  • void write(int b) 将指定字节写入此文件输出流,这个是按照ascii码写入文件的,并不是直接写入的是整数

实例

package File_demo;

import java.io.*;

public class demo {
public static void main(String[] args) {
FileOutputStream outputStream = null;
File file = new File("/tmp" + File.separator + "test.txt");
try {
outputStream = new FileOutputStream(file);
try {
int data = 48;
String name = "陈加兵\n"; //使用\n换行
byte[] bytes = name.getBytes(); //将字符串转换成byte数组
outputStream.write(bytes, 0, 3); //将中文字符串的第一个字写入,这里一个中文占了三个字节
String age = "chenjiabing\n";
outputStream.write(age.getBytes());
outputStream.write(data); //这里的写入的acsii码中的( } catch (IOException e) {
e.printStackTrace();
} } catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close(); //关闭文件流
} catch (IOException e) {
e.printStackTrace();
} }
} }
}

当然也可以一个一个的字节输出

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream; public class Test11 {
public static void main(String[] args) throws IOException {
File f = new File("d:" + File.separator+"test.txt");
OutputStream out=new FileOutputStream(f);//如果文件不存在会自动创建
String str="Hello World";
byte[] b=str.getBytes();
for(int i=0;i<b.length;i++){
out.write(b[i]);
}
out.close();
}
}

FileInputStream

  • FileInputStream 从文件系统中的某个文件中获得输入字节。哪些文件可用取决于主机环境。
  • FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。
  • 主要的功能是读取文件中的内容

构造函数

  • FileInputStream(File file) 通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的File对file指定。
  • FileInputStream(String name) 通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。

常用方法

  • int read() 从输入流中读取数据字节,如果到达文件的末尾就返回-1
  • int read(byte[] b) 将文件中的内容读取到byte数组中,如果到达文件末尾返回-1
  • int read(byte[] b, int off, int len) 从此输入流中将最多 len 个字节的数据读入一个 byte 数组中,这个用于截取字节流,注意这里中文是占三个字节
  • long skip(long n) 从输入流中跳过并丢弃 n 个字节的数据,一旦跳过字节那么就从跳过的字节的后面开始读取
  • int available() 返回的数据是输入流中的字节数,如果没有字节就返回0,因此可以用这个函数判断文件中是否还有内容

实例

  • 针对知道的文件的大小然后创建一个数组存储,之后将数组转换成字符串,当然我们也可以一个一个的读取
File file=new File("/tmp"+File.separator+"test.txt");
FileInputStream inputStream=null;
try {
inputStream=new FileInputStream(file); try {
byte[] bytes=new byte[(int)file.length()]; //file.length返回文件的大小,这样就不会浪内存空间了
int flag=inputStream.read(bytes); //将文件的内容读入到数组中
System.out.println(new String(bytes)); //将bytes数组转换成字符串输出
System.out.println(flag);
}catch (IOException e)
{
e.printStackTrace();
}
}catch (FileNotFoundException e)
{
e.printStackTrace();
}finally {
if(inputStream!=null)
{
try {
inputStream.close();
}catch (IOException e)
{
e.printStackTrace();
} }
}
  • 一个一个的读文件
File file=new File("/tmp"+File.separator+"test.txt");
FileInputStream inputStream=null;
try {
inputStream=new FileInputStream(file); try {
int len=0; //读取的字节
int i=0; //下标
byte[] bytes=new byte[(int)file.length()]; //创建数组
while((len=inputStream.read())!=-1) //判断是否读取到文件的末尾
{
bytes[i]=(byte)len; //将读到的整形数据转换成bytes类型的,存储在数组中
i++;
}
System.out.println(new String(bytes)); //转换成字符串 }catch (IOException e)
{
e.printStackTrace();
}
}catch (FileNotFoundException e)
{
e.printStackTrace();
}finally {
if(inputStream!=null)
{
try {
inputStream.close();
}catch (IOException e)
{
e.printStackTrace();
} }
}
  • 使用available控制结束
File file = new File("/tmp" + File.separator + "test.txt");
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file); try { byte[] bytes = new byte[(int) file.length()]; //file.length返回文件的大小,这样就不会浪内存空间了
int i = 0;
while (inputStream.available() != 0) {
try {
bytes[i] = (byte) inputStream.read();
i++;
} catch (IOException e) {
e.printStackTrace();
} }
System.out.println(new String(bytes)); } catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
} }
}

字符流

  • 在程序中一个字符等于两个字节,那么java提供了Reader、Writer两个专门操作字符流的类。
  • 前面已经说过字符流要用到缓冲区,因此在关闭字符流的时候一定要刷新缓冲区,清空缓冲区中的内容

字符输出流

FileWriter

  • 用来写入字符文件的便捷类。此类的构造方法假定默认字符编码和默认字节缓冲区大小都是可接受的。
  • FileWriter 用于写入字符流。要写入原始字节流,请考虑使用 FileOutputStream
  • 主要功能是向文件中写入内容

构造函数

  • FileWriter(File file) 根据给定的 File 对象构造一个 FileWriter 对象。
  • FileWriter(File file,boolean append) 追加
  • FileWriter(String fileName) 根据给定的文件名构造一个 FileWriter 对象。
  • FileWriter(String fileName, boolean append) 根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象。

常用方法

  • write(String str) 将字符写入文件
  • write(String str,int offest,int len) 截取字符串部分内容写入文件
  • write(int c) 写入单个字符,并不是整数
  • close() 关闭流,在关闭之前必须刷新缓冲区
  • flush() 刷新缓冲区

实例

File file=new File("/tmp"+File.separator+"test"+File.separator+"test.txt");
File f1=new File("/tmp"+File.separator+"test");
if(!f1.exists())
{
f1.mkdir();
System.out.println("文件创建成功");
}
FileWriter fileWriter=null; try {
fileWriter=new FileWriter(file);
String str="hello chenjiabing\n";
String name="陈加兵";
int data=48;
fileWriter.write(str); //写入字符串
fileWriter.write(name); //写入中文字符串,这里直接写入不用转换成byte数组了
fileWriter.write(data); //写入单个字符
}catch (IOException e)
{
e.printStackTrace();
}finally {
if(fileWriter!=null)
{ try {
fileWriter.flush(); //刷新缓冲区
fileWriter.close(); //关闭字符流
}catch (IOException e)
{
e.printStackTrace();
}
}
}

注意: 这里字符流如果不关闭的话,那么就会就会写入文件失败,文件中没有内容,但是如果只有flush而没有close文件还是会写入成功的

字符输入流

FileReader

  • 用来读取字符文件的便捷类。此类的构造方法假定默认字符编码和默认字节缓冲区大小都是适当的
  • 主要的功能是读取文件内容

构造函数

  • FileReader(File file) 在给定从中读取数据的 File 的情况下创建一个新 FileReader
  • FileReader(String fileName) 在给定从中读取数据的文件名的情况下创建一个新 FileReader

常用函数

  • int read(char[] cbuf) 将字符读入数组。
  • int read() 读取单个字符,之后使用char类型强制转换成字符就行
  • read(char[] cbuf, int off, int len) 将字符读入数组的某一部分。
  • boolean ready() 判断是否准备读取此流,如果读到文件末尾那么就返回false
  • long skip(long n) 跳过字符。

实例

  • 用字符数组读取
File file = new File("/tmp" + File.separator + "test" + File.separator + "test.txt");
FileReader fileReader = null;
try {
fileReader = new FileReader(file);
char[] c = new char[(int) file.length()]; //根据文件的大小申请数组大小,不浪费
try {
int len = fileReader.read(c); //将文件的内容读取到字符数组中
for (int i = 0; i < c.length; i++) {
System.out.println(c[i]); //将一个一个字符输出
}
} catch (IOException e) {
e.printStackTrace();
} } catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
  • 使用ready控制是否读到文件末尾,当然也可以使用int read()==-1判断
File file = new File("/tmp" + File.separator + "test" + File.separator + "test.txt");
FileReader fileReader = null;
try {
fileReader = new FileReader(file);
char[] c = new char[(int) file.length()]; //根据文件的大小申请数组大小,不浪费
try {
while(fileReader.ready()) //判断是否读到文件末尾
{
System.out.println((char)fileReader.read()); //转换成字符
}
} catch (IOException e) {
e.printStackTrace();
} } catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

参考文章