IO--字符流操作五种方式及字节流的四种方式+图谱

xiaoxiao2021-02-27  137

字节流复制文件的四种方式(简单的读取操作,将复制操作拆分即可)

import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * 字节流 文件赋值的四种方式 * @author ZYxiao * */ public class FourMethodInOutStream { public static void main(String[] args) throws IOException{ //method1(); //method2(); //method3(); method4(); } /** * 基本字节流,一次一字节 * @throws IOException */ private static void method1() throws IOException { // TODO Auto-generated method stub FileInputStream fis=new FileInputStream("osw.txt"); FileOutputStream fos =new FileOutputStream("test.txt"); int b =0; while((b = fis.read()) != -1){ fos.write(b); } fis.close(); fos.close(); } /** * 基本字节流,一次一字节 * @throws IOException */ private static void method2() throws IOException { // TODO Auto-generated method stub FileInputStream fis=new FileInputStream("osw.txt"); FileOutputStream fos =new FileOutputStream("test.txt"); byte bys[] =new byte[1024]; int len=0; //记录一次实际读取到的字符数 while((len = fis.read(bys)) != -1){ fos.write(bys,0,len); } fis.close(); fos.close(); } private static void method3() throws IOException { BufferedInputStream bis=new BufferedInputStream(new FileInputStream("osw.txt")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("test.txt")); int b=0; while((b = bis.read()) != -1){ bos.write(b); } bis.close(); bos.close(); } private static void method4() throws IOException { // TODO Auto-generated method stub BufferedInputStream bis=new BufferedInputStream(new FileInputStream("osw.txt")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("test.txt")); byte[] bys = new byte[1024]; int len =0; while((len = bis.read(bys)) != -1){ bos.write(bys,0,len); } bis.close(); bos.close(); } }

字符流复制文件的五种方式(简单的读取操作,将复制操作拆分即可)

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /** * 字符流赋值文件五种方式 * @author ZYxiao * */ public class FiveMethodReaderWriter { public static void main(String[] args) throws IOException { //method1(); //method2(); //method3(); //method4(); method5(); } /** * 基本字符流,一次读一个字节 * @throws IOException */ private static void method1() throws IOException { // TODO Auto-generated method stub FileReader fr=new FileReader("osw.txt"); FileWriter fw =new FileWriter("test.txt"); int b=0; while((b = fr.read())!= -1){ fw.write(b); } fr.close(); fw.close(); } /** * 基本字符流,一次读一个字节数组 * @throws IOException */ private static void method2() throws IOException { // TODO Auto-generated method stub FileReader fr=new FileReader("osw.txt"); FileWriter fw =new FileWriter("test.txt"); char[] b=new char[1024]; int len=0; while((len = fr.read(b))!= -1){ fw.write(b,0,len); } fr.close(); fw.close(); } /** * 高效字符流,一次读一个字节 * @throws IOException */ private static void method3() throws IOException { // TODO Auto-generated method stub BufferedReader br=new BufferedReader(new FileReader("osw.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt")); int b=0; while((b=br.read())!=-1){ bw.write(b); } br.close(); bw.close(); } /** * 高效字符流,一次读一个字节数组 * @throws IOException */ private static void method4() throws IOException{ // TODO Auto-generated method stub BufferedReader br=new BufferedReader(new FileReader("osw.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt")); char[] ch=new char[1024]; int len=0; while((len=br.read(ch))!=-1){ bw.write(ch,0,len); } br.close(); bw.close(); } /** * 高效字符流,一次读一行 * BufferedReader Writer的独有高效方法 * @throws IOException */ private static void method5() throws IOException { // TODO Auto-generated method stub BufferedReader br=new BufferedReader(new FileReader("osw.txt")); BufferedWriter bw=new BufferedWriter(new FileWriter("test.txt")); String s=null; while((s=br.readLine())!=null){ bw.write(s); bw.newLine(); bw.flush(); } bw.close(); br.close(); } }
转载请注明原文地址: https://www.6miu.com/read-16654.html

最新回复(0)