IO——字节流

字节输出流OutputStream    此抽象类,是表示输出字节流的所有类的超类。操作的数据都是字节,定义了输出字节流的基本共性功能方法。

输出流中定义的方法:

    close():关闭输出流并释放与此输出流有关的所有系统资源

    flush():刷新此输出流并强制写出所有缓冲的输出字节

    write(byte[] b):将b.length个字节从指定的byte数组写入此输出流

    write(byte[] b, int off, int len):将指定byte数组中从偏移量off开始的len个字节写入此输出流

    write(int b):将指定的字节写入此输出流

FileOutputStream类    OutputStream有很多子类,其中子类FileOutputStream可用来写入数据到文件。FileOutputStream类,即文件输出类,是用于将数据写入File的输出流。

1.FileOutputStream类写入数据到文件中,代码演示:

public class FileOutputStreamDemo {
	public static void main(String[] args) throws IOException {
		//需求:将数据写入到文件中。
		//创建存储数据的文件。
		File file = new File("c:\\file.txt");
		//创建一个用于操作文件的字节输出流对象。一创建就必须明确数据存储目的地。
		//输出流目的是文件,会自动创建。如果文件存在,则覆盖。
		FileOutputStream fos = new FileOutputStream(file);
		//调用父类中的write方法。
		byte[] data = "abcde".getBytes();
		fos.write(data);
		//关闭流资源。
		fos.close();
	}
}

2.给文件中续写或换行

    直接new FileOutputStream(file)创建对象,写入数据会覆盖原有文件。在FileOutputStream的构造函数中,可以接受一个Boolean类型的值,如果值是true,就会在文件末位继续添加。

FileOutputStream的构造方法:

给文件中续写数据和换行的代码演示:

public class FileOutputStreamDemo2 {
	public static void main(String[] args) throws Exception {
		File file = new File("c:\\file.txt");
		FileOutputStream fos = new FileOutputStream(file, true);[设置给指定文件续写数据]
		String str = "\r\n"[实现换行]+"itcast";
		fos.write(str.getBytes());
		fos.close();
	}
}

3.IO异常的处理

演示在实际开发中对异常如何进行处理:

public class FileOutputStreamDemo3 {
	public static void main(String[] args) {
		File file = new File("c:\\file.txt");
		//定义FileOutputStream的引用
		FileOutputStream fos = null;
		try {
			//创建FileOutputStream对象
			fos = new FileOutputStream(file);
			//写出数据
			fos.write("abcde".getBytes());
		} catch (IOException e) {
			System.out.println(e.toString() + "----");
		} finally {
			//一定要判断fos是否为null,只有不为null时,才可以关闭资源
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					throw new RuntimeException("");
				}
			}
		}
	}
}

字节输入流InputStream    通过InputStream可以实现把文件中的数据读到内存中。InputStream这个抽象类,是表示字节输入流的所有类的超类,定义了字节输入流的基本共性功能方法。

输入流中定义的方法:

    read():从输入流中读取数据的下一个字节

    read(byte[] b):读取一定数量的字节,并存储到字节数组中,返回读取到的字节数。

FileInputStream类    从文件系统中的某个文件中获得输入字节。

1.FileInputStream类读取数据read方法   

在读取文件中的数据时,调用read方法,实现从文件中读取数据

代码演示:

public class FileInputStreamDemo {
	public static void main(String[] args) throws IOException {
		File file = new File("c:\\file.txt");
		//创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。
		FileInputStream fis = new FileInputStream(file);
		//读取数据。使用 read();一次读一个字节。
		int ch = 0;
		while((ch=fis.read())!=-1){
			System.out.pr		}intln("ch="+(char)ch);

		// 关闭资源。
		fis.close();
	}
}

2.读取数据read(byte[] b)方法   

在读取文件中的数据时,调用read方法,每次只能读取一个,太麻烦。可以定义数组作为临时的存储容器,这时可以调用重载的read方法,一次可以读取多个字符。

public class FileInputStreamDemo2 {
	public static void main(String[] args) throws IOException {
		/*
		 * 演示第二个读取方法, read(byte[]);
		 */
		File file = new File("c:\\file.txt");
		// 创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。
		FileInputStream fis = new FileInputStream(file);		
		//创建一个字节数组。
		byte[] buf = new byte[1024];//长度可以定义成1024的整数倍。		
		int len = 0;
		while((len=fis.read(buf))!=-1){
			System.out.println(new String(buf,0,len));
		}
		fis.close();
	}
}

使用读写操作完成文件的复制

1.复制文件

原理:读取一个已有的数据,并将这些读到的数据写入到另一个文件中。

public class CopyFileTest {
	public static void main(String[] args) throws IOException {
		//1,明确源和目的。
		File srcFile = new File("c:\\YesDir\test.JPG");
		File destFile = new File("copyTest.JPG");
		
		//2,明确字节流 输入流和源相关联,输出流和目的关联。
		FileInputStream fis = new FileInputStream(srcFile);
		FileOutputStream fos = new FileOutputStream(destFile);
		
		//3, 使用输入流的读取方法读取字节,并将字节写入到目的中。
		int ch = 0;
		while((ch=fis.read())!=-1){
			fos.write(ch);
		}
		//4,关闭资源。
		fos.close();
		fis.close();
	}
}

上述代码输入流和输出流之间通过ch这个变量进行数据交换。

2.缓冲数组方式复制文件

上述代码复制文件每次从源文件读取一个,然后写入指定文件,接着再读取一个字符,再写一个,效率极低,并且频繁的从文件读数据和写数据。

public class CopyFileByBufferTest {
	public static void main(String[] args) throws IOException {
		File srcFile = new File("c:\\YesDir\test.JPG");
		File destFile = new File("copyTest.JPG");
		// 明确字节流 输入流和源相关联,输出流和目的关联。
		FileInputStream fis = new FileInputStream(srcFile);
		FileOutputStream fos = new FileOutputStream(destFile);
		//定义一个缓冲区。
		byte[] buf = new byte[1024];
		int len = 0;
		while ((len = fis.read(buf)) != -1) {
			fos.write(buf, 0, len);// 将数组中的指定长度的数据写入到输出流中。
		}
		// 关闭资源。
		fos.close();
		fis.close();
	}
}
上述代码一次把文件中多个数据都读进内容中,然后再一次写出去,速度比前面的代码快很多。

猜你喜欢

转载自blog.csdn.net/qq_41787619/article/details/80171327