Java IO

1. 什么是Java IO

  • I/O是Input/Output的缩写,用于处理设备之间的数据传输,如读写文件,网络传输;
  • Java程序中,对于数据的输入、输出操作以“流(Stream)” 的方式进行;
  • java.io包下提供了各种“流”和接口,用于获取不同种类的数据

2. 流的分类

  • 按操作数据单位不同:  字节流(8bit)  、字符流(16bit)
  • 按数据流的流向不同:输入流(*InputStream / *Reader)、输出流(*OutputStream / *Writer)
  • 按流的角色不同:节点流、处理流

3. 流的使用

3.1FileReader  FileWriter

主要用来读取写入纯文本文件,包括 .java ,.txt , .class 等

package com.isoftstone.test;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.Test;

/**
 * @author zmjjobs
 * 抽象基类         节点流 /文件流             
 * InputStream    FileInputStream
 * OutputSream    FileOutputSteam
 * Reader         FileReader
 * Writer         FileWriter
 */
public class TestFileReaderAndFileWriter {
	@Test
	public void testFile1(){
		FileReader fr = null;
		FileWriter fw = null;
		try {
			//实例化File对象
			File srcFile = new File("F:/JavaZongJieBiJi/Java英语缩写名词.txt");
			System.out.println(srcFile.getAbsolutePath());
			//输入流
			fr = new FileReader(srcFile);
			File destFile = new File("F:/output.txt");
			///输出流
			//输出流对应的文件名如果不存在  则会自动创建一个
			//输出流对应的文件名如果存在  则默认按覆盖文件处理;append=true,则会追加内容
			fw = new FileWriter(destFile);
			//fw = new FileWriter(outputFile,true);
			//读取  每次只能读取一个字符
			int data;
			while((data = fr.read()) != -1) {
				fw.write(data);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(fw != null) {
				try {
					//关闭流
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fr != null) {
				try {
					//关闭流
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Test
	public void testFile2(){
		FileReader fr = null;
		FileWriter fw = null;
		try {
			//实例化File对象
			File srcFile = new File("F:/JavaZongJieBiJi/Java英语缩写名词.txt");
			//输入流
			fr = new FileReader(srcFile);
			File destFile = new File("F:/output.txt");
			//输出流
			//输出流对应的文件名如果不存在  则会自动创建一个
			//输出流对应的文件名如果存在  则默认按覆盖文件处理;append=true,则会追加内容
			fw = new FileWriter(destFile);
			//fw = new FileWriter(outputFile,true);
			//读取  每次读取N个字符
			char[] cbuf = new char[5];
			int len;
			while((len = fr.read(cbuf)) != -1) {
				//这里new String里面必须要指定截取长度
				//因为cbuf每次属于替换的过程
				fw.write(cbuf,0,len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(fw != null) {
				try {
					//关闭流
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fr != null) {
				try {
					//关闭流
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

3.2 FileInputStream  FileOutputSteam 

主要用来读取写入字节文件(非文本文件), 包括 图片、视频、word文档等

package com.isoftstone.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;

/**
 * @author zmjjobs
 * 抽象基类         节点流 /文件流          
 * InputStream    FileInputStream
 * OutputSream    FileOutputSteam
 * Reader         FileReader
 * Writer         FileWriter
 */
public class TestFileInputStreamAndFileOutputSream {
	@Test
	public void testFile1(){
		FileInputStream fi = null;
		FileOutputStream fo = null;
		try {
			File srcFile = new File("F:/MySelfPic/1491749608618.jpg");
			fi = new FileInputStream(srcFile);
			File destFile = new File("F:/dest.jpg");
			fo = new FileOutputStream(destFile);
			int data;
			while((data = fi.read()) != -1) {
				fo.write(data);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(fo != null) {
				try {
					//关闭流
					fo.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fi != null) {
				try {
					//关闭流
					fi.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	@Test
	public void testFile2(){
		FileInputStream fi = null;
		FileOutputStream fo = null;
		try {
			File srcFile = new File("F:/MySelfPic/1491749608618.jpg");
			fi = new FileInputStream(srcFile);
			File destFile = new File("F:/dest.jpg");
			fo = new FileOutputStream(destFile);
			byte[] cbuf = new byte[1024];
			int len;
			while((len = fi.read(cbuf)) != -1) {
				fo.write(cbuf,0,len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(fo != null) {
				try {
					//关闭流
					fo.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fi != null) {
				try {
					//关闭流
					fi.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 对图片进行加密解密      图片解密跟图片加密代码是一样的,怎样加密的,怎么解密
	 * 也适应于.doc,.pdf等
	 * 其实对文本文件加密解密也可以使用,只要不读取,纯复制没有问题
	 */
	@Test
	public void testImageJiaMi(){
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("F:/MySelfPic/1491749608618.jpg");
			fos = new FileOutputStream("F:/JiaMiTuPian.jpg");
			byte[] cbuf = new byte[1024];
			int len;
			while((len = fis.read(cbuf)) != -1) {
				for (int i = 0; i < len; i++) {
					//加密或解密
					cbuf[i] = (byte) (cbuf[i] ^ 123);
				}
				fos.write(cbuf,0,len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

3.3 BufferedReader  BufferedWriter

package com.isoftstone.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.junit.Test;

/**
 * @author zmjjobs
 * 抽象基类         节点流 /文件流     缓冲流(处理流的一种)              
 * InputStream    FileInputStream    BufferedInputStream
 * OutputSream    FileOutputSteam    BufferedOutputSream
 * Reader         FileReader         BufferedReader
 * Writer         FileWriter         BufferedWriter
 */
public class TestBufferedReaderAndBufferedWriter {
	@Test
	public void testFile1(){
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			br = new BufferedReader(new FileReader(new File("F:/JavaZongJieBiJi/Java英语缩写名词.txt")));
			bw = new BufferedWriter(new FileWriter(new File("F:/abc.txt")));
			//每次最多读取1024个字符
			char[] cbuf = new char[1024];
			int len;
			while((len = br.read(cbuf)) != -1) {
				bw.write(cbuf,0,len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
 	}
	
	@Test
	public void testFile2(){
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			br = new BufferedReader(new FileReader(new File("F:/JavaZongJieBiJi/Java英语缩写名词.txt")));
			bw = new BufferedWriter(new FileWriter(new File("F:/abc.txt")));
			//每次读取一行
			String data;
			while((data = br.readLine()) != null) {
				bw.write(data);
				//每次记着要换行
				bw.newLine();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

3.4 转换流  InputStreamReader  OutputStreamWriter

  • InputStreamReader是将字节的输入流转换成字符的输入流;
  • OutputStreamWriter是将字符的输出流转换成字节的输出流;
  • 可用于 将一个字符集的文件转换成另外一个字符集的文件
package com.isoftstone.test;
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 TestInputSteamReaderAndOutputStreamWriter {
	@Test
	public void test(){
		FileInputStream fis = null;
		FileOutputStream fos = null;
		InputStreamReader isr = null;
		OutputStreamWriter osw = null;
		try {
			fis = new FileInputStream("F:/JavaZongJieBiJi/Java英语缩写名词.txt");
			fos = new FileOutputStream("F:/abc.txt");
			//isr = new InputStreamReader(fis);//使用工程默认的字符集
			//isr = new InputStreamReader(fis,"UTF-8");
			isr = new InputStreamReader(fis,"GBK");//使用指定的的字符集
			osw = new OutputStreamWriter(fos,"UTF-8");//指定输出文件的字符集,这里相当于把GBK的文件输出了一份UTF8的文件
			char[] cbuf = new char[1024];
			int len;
			while((len = isr.read(cbuf)) != -1) {
				osw.write(cbuf,0,len);
				//打印到控制台,如果不是对应的字符集则会显示乱码,但是不影响输出文件
				System.out.println(new String(cbuf,0,len));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (osw != null) {
				try {
					osw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (isr != null) {
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
		}
		
	}
}

3.5 标准输入输出流  System.in   System.out

package com.isoftstone.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.junit.Test;

public class TestSystemInOut {
	@Test
	public void test(){
		//System.in 标准输入
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String data;
		try {
			while(true) {
				System.out.println("请输入字符串:");
				try {
					//读取输入整行
					data = br.readLine();
					if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)) {
						System.out.println("程序结束.....");
						break;
					}
					//输出到控制台
					System.out.println(data.toUpperCase());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

3.6 PrintStream

package com.isoftstone.test;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import org.junit.Test;

public class TestPrintStream {
	@Test
	public void test(){
		PrintStream ps = null;
		try {
			//创建打印输出流,设置为自动刷新
			ps = new PrintStream(new FileOutputStream("F:/aaa.txt"),true);
			if (ps != null) {
				//把控制台要输出的内容写入文件中
				System.setOut(ps);
			}
			for (int i = 0; i < 255; i++) {
				System.out.print((char)i);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (ps != null)
				ps.close();
		}
	}
}

3.7 DataInputStream   DataOutputStream

用于读取和写出 基本数据类型的 变量和字符串

package com.isoftstone.test;
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 org.junit.Test;

public class TestDataInputStreamAndDataOutputStream {
	@Test
	public void test(){
		DataOutputStream dos = null;
		try {
			//将内存中的基本数据保存到文件中
			dos = new DataOutputStream(new FileOutputStream("F:/dest.txt"));
			dos.writeUTF("小鸡小鸡咯咯哒!");
			dos.flush();
			dos.writeInt(1);
			dos.flush();
			dos.writeBoolean(true);
			dos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (dos != null) {
				try {
					dos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		DataInputStream dis = null;
		try {
			//将文件中的基本数据读取到内存
			dis = new DataInputStream(new FileInputStream("F:/dest.txt"));
			//这里读取的顺序必须是存入的顺序,否则报错
			System.out.println("title=" + dis.readUTF());
			System.out.println("index=" + dis.readInt());
			System.out.println("isExist=" + dis.readBoolean());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (dis != null) {
				try {
					dis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

3.8 ObjectInputStream  ObjectOutputStream

  • 把对象以流的方式写入到文件中保存,叫对象的序列化,用ObjectOutputStream;
  • 把文件中保存的对象以流的方式读取出来,叫对象的反序列化,用ObjectInputStream;
  • 当然对象若参与序列化,需要实现Serializable接口;
package com.isoftstone.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.junit.Test;
import com.isoftstone.bean.Person;

public class TestObjectInputStreamAndObjectOutputStream {
	@Test
	public void test(){
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream("F:/person.txt"));
			oos.writeObject(new Person(101,"zmj",23));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (oos != null) {
				try {
					oos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream("F:/person.txt"));
			Person person = (Person) ois.readObject();
			System.out.println(person);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

4. 修改过的文件路径输出到文件

                        https://blog.csdn.net/qq_30426943/article/details/104426616

代码获取地址: https://github.com/zmjjobs/JavaIO.git

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朱梦君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值