JAVA I/O(输入/输出)-2

文件输入/输出流

package zhao;

import java.io.*;

public class Demo {
    
    

	public static void main(String[] args) {
    
    
		File f = new File("word.txt");
		FileOutputStream out = null;
		try {
    
    
			out = new FileOutputStream(f, false);// true在文件末尾追加内容,false替换文件内容

			String str = "你见过洛杉矶凌晨四点的太阳吗?";
			byte b[] = str.getBytes();// 字符串转换为字节数组
			out.write(b);// 将字节数组中数据写入到文件中

		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (out != null) {
    
    
				try {
    
    
					out.close();
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}

		FileInputStream in = null;

		try {
    
    
			in = new FileInputStream(f);// 输入流读文件
			byte b2[] = new byte[1024];// 缓冲区
			int len = in.read(b2);// 读入缓冲区的总字节数
			System.out.println("文件中的数据是:" + new String(b2, 0, len));// 去掉空格

		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (in != null) {
    
    
				try {
    
    
					in.close();
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}
	}
}

在这里插入图片描述

文件字符流

在这里插入图片描述

package zhao;

import java.io.*;

public class Demo {
    
    

	public static void main(String[] args) {
    
    
		File f = new File("word.txt");

		FileWriter fw = null;
		try {
    
    
			fw = new FileWriter(f, false);// true在原文件后追加,false覆盖

			String str = "自强不息,厚德载物。";
			fw.write(str);// 将字符串写入到文本文档
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (fw != null) {
    
    
				try {
    
    
					fw.close();
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}

		FileReader fr = null;
		try {
    
    
			fr = new FileReader(f);

			char ch[] = new char[1024];// 缓冲区
			int count;// 已读出的字符数
			while ((count = fr.read(ch)) != -1) {
    
    // 循环读取文件中的数据,直到所有字符都读完
				System.out.println("文件中的内容为:" + new String(ch, 0, count));
			}
		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (fr != null) {
    
    
				try {
    
    
					fr.close();
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}
	}
}

在这里插入图片描述

缓冲字节流

在这里插入图片描述
没有缓冲字节流

package zhao;

import java.io.*;

public class Demo {
    
    

	public static void main(String[] args) {
    
    
		File f = new File("C:\\Users\\zhao\\Desktop\\0GLCFCS30产品说明.docx");

		FileInputStream in = null;
		long start = System.currentTimeMillis();// 获取流开始时毫秒值
		try {
    
    
			in = new FileInputStream(f);
			byte b[] = new byte[1024];// 缓冲区字节数组(这个缓冲区与Buffered不同)
			while (in.read() != -1) {
    
    

			}
			long end = System.currentTimeMillis();// 获取流结束时毫秒值
			System.out.println("运行经历的毫秒数:" + (end - start));
		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (in != null) {
    
    
				try {
    
    
					in.close();
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}
	}
}

运行经历的毫秒数:4535

添加缓冲字节流

package zhao;

import java.io.*;

public class Demo {
    
    

	public static void main(String[] args) {
    
    
		File f = new File("C:\\Users\\zhao\\Desktop\\0GLCFCS30产品说明.docx");
		BufferedInputStream bi=null;//大大提高运行效率
		FileInputStream in = null;
		long start = System.currentTimeMillis();// 获取流开始时毫秒值
		try {
    
    
			in = new FileInputStream(f);
			bi=new BufferedInputStream(in);//将文件字节流包装成缓冲字节流
			byte b[] = new byte[1024];// 缓冲区字节数组(这个缓冲区与Buffered不同)
			while (bi.read(b) != -1) {
    
    //使用缓冲流读取数据

			}
			long end = System.currentTimeMillis();// 获取流结束时毫秒值
			System.out.println("运行经历的毫秒数:" + (end - start));
		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (in != null) {
    
    
				try {
    
    
					in.close();
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
			if (bi!=null) {
    
    
				try {
    
    
					bi.close();
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}
	}
}

运行经历的毫秒数:2

上面是缓冲输入字节流,下面是缓冲输出字节流

package zhao;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo2 {
    
    

	public static void main(String[] args) {
    
    
		File f = new File("word.txt");
		FileOutputStream out = null;
		BufferedOutputStream bo = null;

		try {
    
    
			out = new FileOutputStream(f);
			bo = new BufferedOutputStream(out);// 将文件输出流包装
			String str = "仰天大笑出门去,我辈岂是蓬蒿人。";
			byte b[] = str.getBytes();
			bo.write(b);
			// 使用缓冲字节输出流时,要多进行刷新操作,避免等待
			bo.flush();// 刷新、强制将缓冲区数据写入文件中,即使缓冲区没有写满

		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (out != null) {
    
    
				try {
    
    
					out.close();
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
			if (bo != null) {
    
    
				try {
    
    
					bo.close();
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}
	}

}

缓冲字符流

在这里插入图片描述
缓冲字符输出流

package zhao;

import java.io.*;

public class Demo {
    
    

	public static void main(String[] args) {
    
    
		File f = new File("word.txt");
		FileWriter fw = null;
		BufferedWriter bw = null;

		try {
    
    
			fw = new FileWriter(f);
			bw = new BufferedWriter(fw);

			String str1 = "生活不止眼前的苟且";
			String str2 = "还有诗和远方的田野";
			bw.write(str1);
			bw.newLine();// 创建一个新行
			bw.write(str2);
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    // 要注意流的关闭顺序,先创建的后关闭
			if (bw != null) {
    
    
				try {
    
    
					bw.close();
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
			if (fw != null) {
    
    
				try {
    
    
					fw.close();
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}

		}

	}
}

在这里插入图片描述
缓冲字符输入流

package zhao;

import java.io.*;

public class Demo2 {
    
    

	public static void main(String[] args) {
    
    
		File f = new File("word.txt");
		FileReader fr = null;
		BufferedReader br = null;

		try {
    
    
			fr = new FileReader(f);
			br = new BufferedReader(fr);
			String tmp = null;
			int i = 1;
			while ((tmp = br.readLine()) != null) {
    
    // 循环读取文件中的内容
				System.out.println("第" + i + "行:" + tmp);
				i++;
			}
		} catch (FileNotFoundException e) {
    
    
			e.printStackTrace();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} finally {
    
    
			if (br != null) {
    
    
				try {
    
    
					br.close();
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
			if (fr != null) {
    
    
				try {
    
    
					fr.close();
				} catch (IOException e) {
    
    
					e.printStackTrace();
				}
			}
		}
	}

}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46688667/article/details/113107988