JavaSE.10.IO流

JavaSE.10.IO流(InputOutputStream)

在这里插入图片描述

1.IO流体系结构

在这里插入图片描述

1.IO流

1.流的分类
按数据单位分:字节流(8bit) 字符流(16bit)
按数据流向分:输入流 输出流
按角色分:节点流(作用在文件上) 处理流(作用在已有的流上)
2.流的体系结构
抽象基类 节点流(文件流) 缓冲流(处理流的一种)
InputStream FileInputStream BufferedInputStream
(read(byte[] buffer)) (read(byte[] buffer))
OutputStream FileOutputStream BufferedOutputStream
(write(byte[] buffer,0,len)) (write(byte[] buffer,0,len))
Reader FileReader BufferedReader
(read(char[] cbuf)) (read(char[] cbuf))
Writer FileWriter BufferedWriter
(write(char[] cbuf,0,len)) (write(char[] cbuf,0,len))

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.Test;

public class IOStreamTest {
	
	@Test
	public void testFileReader() {
		File file = new File("txt");
		System.out.println(file.getAbsolutePath());
	}
	/*
	 * 将txt文件中的内容读入到程序中,并输出控制台
	 */
	@Test
	public void testFileReader1() throws IOException {
		/*
		 * 异常处理,为了保证流资源一定可以执行关闭操作,需要使用try-catch-finally处理
		 * 读入的文件一定要存在,否则会报错FileNotFoundException
		 * 
		 */
		FileReader fr = null;
		try {
			//1.实例化File类的对象,指明操作的文件
			File file = new File("F:\\eclipse-workpeace\\JavaPractice\\ioStream\\Hello.txt");
			//2.提供具体的流
			fr = new FileReader(file);
			//3.数据的读入
			//read():返回读入的一个字符,如果达到文件末尾,返回-1
			int data = fr.read();
			while(data != -1) {
				System.out.print((char)data);
				data = fr.read();
			}
			System.out.println();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(fr != null) {
					//4.关闭流
					fr.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	@Test
	public void testFIleReader2() throws IOException {
		FileReader fr = null;
		try {
			//1.File类的实例化
			File file = new File("F:\\eclipse-workpeace\\JavaPractice\\ioStream\\txt");
			//2.FIleReader流的实例化
			fr = new FileReader(file);
			//3.读入的操作
			char[] cbuf = new char[5];
			int len;
			while((len = fr.read(cbuf)) != -1) {
				for(int i = 0;i < len;i++) {
					System.out.print(cbuf[i]);
				}
				//方式2
				//String str = new String(cbuf,0,len);
				//System.out.print(str);
			}
			System.out.println();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(fr != null) {
				//4.资源的关闭
				fr.close();
			}
		}
	
	}
	
	@Test
	public void testFileWriter() throws IOException {
		//1.提供File类对象
		File file = new File("Hello1.txt");
		System.out.println(file.getAbsolutePath());
		//2.提供FileWriter的对象,用于输出的写出
		FileWriter fw = new FileWriter(file);
		//3.写出操作
		fw.write("i have a dream\n");
		fw.write("you need to have a dream");
		//4.流的关闭
		fw.close();
	}
	
	@Test
	public void testFileReaderFileWriter() throws IOException {
		//2.创建流的对象,输入流和输出流的对象
		FileReader fr = null;
		FileWriter fw = null;
		try {
			//1.创建File类的对象,指明读入和写出的文件
			File file = new File("Hello1.txt");
			File destFile = new File("Hello2.txt");
			
			fr = new FileReader(file);
			fw = new FileWriter(destFile);
			
			//3.数据的读入和写出操作
			char[] cbuf = new char[5];
			int len;//每次读入到cbuf字符的个数
			while((len = fr.read(cbuf)) != -1) {
				fw.write(cbuf,0,len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//4.关闭流资源
			try {
				if(fr != null)
					fr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(fw != null)
					fw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}
2.测试FileInputStream和FileOutputStream

结论:
1.对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
2.对于非文本文件(.jpg,.png…),使用字节流

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;

public class FileInputOutputStreamTest {
	
	@Test
	public void testFileInputStream1() throws IOException {
		//1.造文件
		File file = new File("Hello1.txt");
		
		//2.造流
		FileInputStream fis = new FileInputStream(file);
		
		//3.读数据
		byte[] buffer = new byte[5];
		int len;
		while((len = fis.read(buffer)) != -1) {
			String str = new String(buffer,0,len);
			System.out.print(str);
		}
		
		//4.关闭流
		fis.close();
	}
	
	/*
	 * 实现对图片的复制操作
	 */
	public void testFileInputOutputStream(String srcPath,String destPath) throws IOException {
		//
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			//
			File srcFile = new File(srcPath);
			File destFile = new File(destPath);
			
			fis = new FileInputStream(srcFile);
			fos = new FileOutputStream(destFile);
			
			//
			byte[] buffed = new byte[5];//一般一次读取1024个字节
			int len;
			while((len = fis.read(buffed)) != -1) {
				fos.write(buffed,0,len);
			}
			System.out.println("复制成功");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//
			try {
				if(fis != null)
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(fos != null)
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}
	
	@Test
	public void testCopyTimeBuffered() throws IOException {
		long start = System.currentTimeMillis();
		
		String srcPath = "14.jpg";
		String destPath = "141.jpg";
		
		testFileInputOutputStream(srcPath,destPath);
		
		long end = System.currentTimeMillis();
		
		System.out.println("花费的时间为" + (end - start));
	}
	
}
3.处理流之一:缓冲流的使用

1.缓冲流
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
2.作用,提供流的读取,写入的速度

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.Test;

public class BufferedTest {
	
	@Test
	public void bufferedStreamTest() throws IOException {
		//2.2造缓冲流
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			//1.造文件
			File srcFile = new File("14.jpg");
			File destFile = new File("141.jpg");
			//2.1造流
			FileInputStream fis = new FileInputStream(srcFile);
			FileOutputStream fos = new FileOutputStream(destFile);
			bis = new BufferedInputStream(fis);
			bos = new BufferedOutputStream(fos);
			
			//3.复制 读取写入
			byte[] buffer = new byte[10];
			int len;
			while((len = bis.read(buffer)) != -1) {
				bos.write(buffer,0,len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//4.资源关闭
			//要求:先关闭外层的流,在关闭内层的流
			try {
				if(bis != null)
				bis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(bos != null)
				bos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		//说明:关闭外层流的同事,内层流也会自动的进行关闭,可以省略
//		fis.close();
//		fos.close();
		
	}
	
	//实现文件复制的方法
	public void copyBuffered(String srcPath,String destPath) {
		//2.2造缓冲流
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			//1.造文件
			File srcFile = new File(srcPath);
			File destFile = new File(destPath);
			//2.1造流
			FileInputStream fis = new FileInputStream(srcFile);
			FileOutputStream fos = new FileOutputStream(destFile);
			bis = new BufferedInputStream(fis);
			bos = new BufferedOutputStream(fos);
			
			//3.复制 读取写入
			byte[] buffer = new byte[10];
			int len;
			while((len = bis.read(buffer)) != -1) {
				bos.write(buffer,0,len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//4.资源关闭
			//要求:先关闭外层的流,在关闭内层的流
			try {
				if(bis != null)
				bis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(bos != null)
				bos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@Test
	public void testCopyTimeBuffered() {
		long start = System.currentTimeMillis();
		
		String srcPath = "14.jpg";
		String destPath = "141.jpg";
		
		copyBuffered(srcPath,destPath);
		
		long end = System.currentTimeMillis();
		
		System.out.println("花费的时间为" + (end - start));
	}
	
	//使用缓冲流实现文本文档的复制
	@Test
	public void testBufferedReaderWriter() throws IOException {
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			br = new BufferedReader(new FileReader(new File("java.txt")));
			bw = new BufferedWriter(new FileWriter(new File("java1.txt")));
			//方式1
//			char[] cubf = new char[1024];
//			int len;
//			while((len = br.read(cubf)) != -1) {
//				bw.write(cubf,0,len);
//			}
			
			//方式2
			String data;
			while((data = br.readLine()) != null) {
				//bw.write(data + "\r\n");两个方法都可以换行
				bw.write(data);
				bw.newLine();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(br != null)
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(bw != null)
				bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}
4.处理流之二:转换流的应用

1.转换流 (属于字符流)
InputStreamReader :将一个字节的输入流转换为字符的输入流
OutputStreamWriter :将一个字符的输出流转换为字节的输出流
2.作用:提供字节流和字符流之间的转换
3.解码:字节、字节数组—字符、字符数组
编码:字符、字符数组—字节、字节数组
4.字符集的转换

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import org.junit.Test;

public class InputStreamReaderTest {
	
	@Test
	public void test1() throws IOException {
		FileInputStream fis = new FileInputStream(new File("java.txt"));
		//参数2指明了字符集:不输入则是系统默认字符集,输入时根据文件保存时的字符集来写入
		InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
		
		char[] cbuf = new char[20];
		int len;
		while((len = isr.read(cbuf)) != -1) {
			String str = new String(cbuf,0,len);
			System.out.print(str);
		}
	}
	
	//InputStreamReader和OutputStreamWriter
	//输入输出字符集的转换
	@Test
	public void test2() throws IOException {
		File file1 = new File("java.txt");
		File file2 = new File("javaGBK.txt");
		
		FileInputStream fis = new FileInputStream(file1);
		FileOutputStream fos = new FileOutputStream(file2);
		
		InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
		OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
		
		char[] cbuf = new char[20];
		int len;
		while((len = isr.read(cbuf)) != -1) {
			osw.write(cbuf,0,len);
		}
		isr.close();
		osw.close();
	}
		
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.Test;

public class PicTest {
	
	//图片加密
	@Test
	public void test1() throws IOException {
		BufferedInputStream br = null;
		BufferedOutputStream bw = null;
		try {
			br = new BufferedInputStream(new FileInputStream(new File("14.jpg")));
			bw = new BufferedOutputStream(new FileOutputStream(new File("142secret.jpg")));
			
			byte[] buffer = new byte[20];
			int len;
			while((len = br.read(buffer)) != -1) {
				//对字节数组进行修改 改变数组元素
				for(int i = 0;i < len;i++) {
					buffer[i] = (byte)(buffer[i] ^ 5);
				}
				
				bw.write(buffer,0,len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(bw != null)
				bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(br != null)
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	//图片解密
	@Test
	public void test2() throws IOException {
		BufferedInputStream br = null;
		BufferedOutputStream bw = null;
		try {
			br = new BufferedInputStream(new FileInputStream(new File("142secret.jpg")));
			bw = new BufferedOutputStream(new FileOutputStream(new File("142.jpg")));
			
			byte[] buffer = new byte[20];
			int len;
			while((len = br.read(buffer)) != -1) {
				//对字节数组进行修改 改变数组元素
				for(int i = 0;i < len;i++) {
					buffer[i] = (byte)(buffer[i] ^ 5);
				}
				
				bw.write(buffer,0,len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(bw != null)
				bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(br != null)
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}
4.对象流
1.对象流:ObjectInputStream和ObjectOutputStream
2.序列化:
序列化:用ObjectOutputStream类保存基本类型数据或对象机制
反序列化:用ObjectInputStream类读取基本数据类型数据或对象的机制
注意:不能序列化static和transient修饰的成员变量
3.对象的序列化:
①对象的序列化机制:允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘					上,或通过网络将这种二进制流传输到另一个网络节点,当其他程序获取了这种二进制流,就可以恢				复成原来的Java对象
②序列化是RMI(Remote Method Invoke-远程方法调用)过程的参数和返回值都必须实现的机制,RMI是JavaEE的基础
③实现Serializable接口(标识接口)的对象转化为字节数据,使其保存传输可以被还原
④如果没有实现序列化接口则会抛出NotSerialilizableException,需要实现Serializable或Extermalizable
public class ObjectStreamTest1{
	/*
	 * 序列化过程:将内存中的Java对象保存到磁盘中或通过网络传输出去
	 */
	@Test
	public void test1() {
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream("object.txt"));
			oos.writeObject(new String("我是彭于晏!"));
			oos.flush();
			oos.writeObject(new Person("刘德华",18));
			oos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(oos != null) {
				try {
					oos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
			}
			
		}
	}
	
	/*
	 * 反序列化:将磁盘文件中的对象还原为内存中的一个Java对象
	 */
	@Test
	public void test2() {
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream("object.txt"));
			Object obj1 = ois.readObject();
			String str = (String)obj1;
			Object obj2 = ois.readObject();
			Person p = (Person)obj2;
			System.out.println(str);
			System.out.println(p);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

/*
 * Person类需要满足如下要求,方可序列化
 * 1.需要实现接口:Serializable
 * 2.当前类提供一个全局常量:serialVersionUID
 * 3.除了当前类需要实现Serializable接口之外,还需要保证其属性也必须是可序列化的(基本数据类型默认可序列 化)
 * 注意:不能序列化static和transient修饰的成员变量
 */
public class Person implements Serializable{
	/**
	 * serialVersionUID用来表明类的不同版本间的兼容性,简言之,目的是以序列化对象进行版本控制,
	 * 有关各版本反序列化时是否兼容
	 * 如果没有显示的定义,则会默认自动生成
	 */
	private static final long serialVersionUID = 4360563436307888671L;
    
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
	
	
}

2.其他流

1.标准输入输出流

1.标准的输入,输出流
System.in :标准的输入流,默认从键盘输入
System.out :标准的输出流,默认从控制台输出
2.System类的setIn(InputStream is) / setOut(printStream ps)方式重新制定输入和输出的流
3练习
从键盘中输入字符串,要求将读取到整行字符串转换成大写输出,并操作
直到输出"e"或"exit"时,退出程序

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.junit.Test;

public class OtherStreamTest {
	//方法1  使用Scanner实现
	//方法2 使用System.in实现  System.in --- 转换流 --- BufferedReader和readline()
	@Test
	public void test1() throws IOException{
		BufferedReader br = null;
		try {
			InputStreamReader isr = new InputStreamReader(System.in);
			br = new BufferedReader(isr);
			
			
			while(true) {
				System.out.println("请输入字符串:");
				String data = br.readLine();
				if("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)) {
					System.out.println("程序结束");
					break;
				}
				
				String upperCase = data.toUpperCase();
				System.out.println(upperCase);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(br != null)
					br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}
2.打印流
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import org.junit.Test;

public class PrintStreamTest {
	/*
	 * 打印流
	 */
	@Test
	public void test() throws FileNotFoundException {
		PrintStream ps = null;
		try {
			FileOutputStream fos = new FileOutputStream("PrintStream.txt");
			ps = new PrintStream(fos,true);
			if(ps != null) {
				System.setOut(ps);
			}
			
			for(int i = 0;i <= 255;i++) {
				System.out.print((char) i);
				if(i % 50 == 0) {
					System.out.println();
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(ps != null)
				ps.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/*
	 * 数据流 
	 * DataInputSteam
	 * DataOutputSteam
	 * 
	 * 作用:用于读取或写出基本数据局类型的变量或字符串
	 * 练习:将内存中的字符串,基本数据类型的变量写出到文件中
	 */
	@Test
	public void test1() throws IOException {
		DataOutputStream dos = new DataOutputStream(new FileOutputStream("DataStream.txt"));
		
		dos.writeUTF("我是谁");
		dos.flush();//刷新操作后就会写入
		dos.writeInt(23);
		dos.flush();
		dos.writeBoolean(true);
		dos.flush();
		
		dos.close();
		
	}
	
	@Test
	public void test2() throws IOException {
		DataInputStream dis = new DataInputStream(new FileInputStream("DataStream.txt"));
		
		String name = dis.readUTF();
		int age = dis.readInt();
		Boolean sex = dis.readBoolean();
				
		System.out.println(name + age + sex);
		
		dis.close();
	}
	
}
3.NIO
NIO,Non-Blocking IO:更加高效的方式进行读写
两套NIO:1.标准输入输出NIO  2.网络编程NIO
Path,Paths,Files核心API
第三方jar包:Commons-io-2.5.jar
4.RandomAccessFile
 1.RandomAccessFile声明在java.io包下,直接继承于java.lang.Object类,实现了DataInput和DataOutput接口
2.RandomAccessFile既可以作为一个输入流,又可以作为一个输出流
    mode参数:
    "r" Open for reading only.
    "rw" Open for reading and writing.
    "rws" Open for reading and writing,
    "rwd"   Open for reading and writing
3.如果作为输出流时,写出到的文件如果不存在,则自动创建
	如果写出到的文件存在,则会对原有文件内容从头开始进行覆盖
4.可以通过相关的操作,实现RandomAccessFile插入的效果
	seek(int long)调整脚标位置为long处,并开始插入
5.应用:可以使用RandomAccessFile类实现多线程断点下载
public class RandomAccessFileTest1 {
	@Test
	public void test1() throws FileNotFoundException {
		RandomAccessFile raf1 = null;
		RandomAccessFile raf2 = null;
		try {
			raf1 = new RandomAccessFile(new File("11.jpg"),"r");
			raf2 = new RandomAccessFile(new File("11(1).jpg"),"rw");
			
			byte[] buffer = new byte[1024];
			int len;
			while((len = raf1.read(buffer)) != -1) {
				raf2.write(buffer,0,len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(raf1 != null) {
				try {
					raf1.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(raf2 != null) {
				try {
					raf2.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	@Test
	public void test2() {
		RandomAccessFile raf1 = null;
		try {
			raf1 = new RandomAccessFile(new File("hhhhh.txt"),"rw");
			raf1.seek(3);//将指针调到脚标为3的位置
			raf1.write("xyz".getBytes());//覆盖已有数据
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(raf1 != null) {
				try {
					raf1.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	/*
	 * 实现数据的插入效果
	 */
	@Test
	public void test3() {
		RandomAccessFile raf1 = null;
		try {
			raf1 = new RandomAccessFile(new File("hhhhh.txt"),"rw");
			raf1.seek(3);//将指针调到脚标为3的位置
			StringBuilder builder = new StringBuilder((int)new File("hhhhh.txt").length());
			byte[] buffer = new byte[20];
			int len;
			while((len = raf1.read(buffer)) != -1) {
				builder.append(new String(buffer,0,len));
			}
			
			//调回指针写入"xyz"
			raf1.seek(3);
			raf1.write("xyz".getBytes());//覆盖已有数据
			
			//将StringBuilder中的数据写入到文件中
			raf1.write(builder.toString().getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(raf1 != null) {
				try {
					raf1.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		//思考:将StringBuilder替换为ByteArrayOutputStream
	}
}
5.编码集

在这里插入图片描述

作业:

1.图片加密和图片解密
public class PicTest {
	//图片加密
	@Test
	public void test1() throws IOException {
		BufferedInputStream br = null;
		BufferedOutputStream bw = null;
		try {
			br = new BufferedInputStream(new FileInputStream(new File("14.jpg")));
			bw = new BufferedOutputStream(new FileOutputStream(new File("142secret.jpg")));
			
			byte[] buffer = new byte[20];
			int len;
			while((len = br.read(buffer)) != -1) {
				//对字节数组进行修改 改变数组元素
				for(int i = 0;i < len;i++) {
					buffer[i] = (byte)(buffer[i] ^ 5);
				}
				
				bw.write(buffer,0,len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(bw != null)
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(br != null)
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	//图片解密
	@Test
	public void test2() throws IOException {
		BufferedInputStream br = null;
		BufferedOutputStream bw = null;
		try {
			br = new BufferedInputStream(new FileInputStream(new File("142secret.jpg")));
			bw = new BufferedOutputStream(new FileOutputStream(new File("142.jpg")));
			
			byte[] buffer = new byte[20];
			int len;
			while((len = br.read(buffer)) != -1) {
				//对字节数组进行修改 改变数组元素
				for(int i = 0;i < len;i++) {
					buffer[i] = (byte)(buffer[i] ^ 5);
				}
				
				bw.write(buffer,0,len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(bw != null)
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(br != null)
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值