2019独角兽企业重金招聘Python工程师标准>>>
(3)练习拷贝文件
import s20190517.ReaderTest3; import java.io.*; public class CopyDemo { public static void main(String[] args) { //创建文件 File file = new File("src\\s20190517\\ReaderTest3.java"); File file1 = new File("ReaderTest3.java"); Reader reader = null; Writer writer = null; try { reader = new FileReader(file); writer = new FileWriter(file1); int len = 1; char[] cs = new char[1024]; while ((len = reader.read(cs)) != -1) { writer.write(cs, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } if (writer != null) { writer.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
(4)高效输入流
package s20190520; import java.io.*; public class BufferedReaderDemo { public static void main(String[] args) { //创建一个BufferedReader对象 BufferedReader reader=null; try { //注意BufferedReader构造器传入的是Reader对象为参数 reader=new BufferedReader(new FileReader("src\\s20190520\\CopyDemo.java")); /*//读取一个文本行,边界是null String s = reader.readLine(); System.out.println(s);*/ //使用循环读取整个文件 String line=null; while ((line=reader.readLine())!=null){ System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } finally { //外层关闭的时候内层自动关闭 if(reader!=null){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
(5)高效输出流
import java.io.*; public class BufferedWriterDemo { public static void main(String[] args) { //创建一个BufferedWriter对象 BufferedWriter writer=null; try { writer=new BufferedWriter(new FileWriter("a.txt")); writer.write("莫名我就喜欢你"); writer.newLine();//换行,要写下一行的时候必须进行这个操作才会换行 writer.flush();//刷新 } catch (Exception e) { e.printStackTrace(); } finally { //外层关闭的时候内层自动关闭 if(writer!=null){ try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
import java.io.*; public class BufferCopyDemo { public static void main(String[] args) { BufferedReader reader=null; BufferedWriter writer=null; try { reader=new BufferedReader(new FileReader("src\\s20190517\\ReaderTest3.java")); writer=new BufferedWriter(new FileWriter("ReaderTest3.java")); //定义一个读取和被写入的字符串 String line=null; //给字符串赋值,null作为循环终止条件 while ((line=reader.readLine())!=null){ //每次写入一行换行并刷新 writer.write(line); writer.newLine(); writer.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if(reader!=null){ reader.close(); } if(writer!=null){ writer.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
83.字节流
import java.io.*; public class OutputStreamDemo { public static void main(String[] args) { //创建一个输出字节流 OutputStream out = null; try { byte[] bs={98,99,100,101,102}; //可以new File或者直接写文件名 out = new FileOutputStream(new File("aa.txt")); //写入单个字符 out.write(98); //写入整个数组 out.write(bs); //写入数组的指定部分 out.write(bs,2,2); } catch (Exception e) { e.printStackTrace(); } finally { //关闭资源 if(out!=null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
字节流不需要flush
import java.io.*; public class InputStreamDemo { public static void main(String[] args) { InputStream in=null; try { in=new FileInputStream(new File("aa.txt")); //读到的内容也是以assci码显示 int read = in.read(); //转化为字节,读一个字节,如果是汉字或者其他国家文字一次读半个字符可能会出现乱码 System.out.println((char)read); read = in.read(); System.out.println((char)read); } catch (Exception e) { e.printStackTrace(); } finally { if(in!=null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
import java.io.*; import java.util.Arrays; public class InputStreamDemo1 { public static void main(String[] args) { InputStream in=null; try { in=new FileInputStream("aa.txt"); byte[] bs=new byte[6]; int len = in.read(bs); System.out.println("读取的长度是"+len+" 读取的内容是"+Arrays.toString(bs) +" 转换成字符串打印"+new String(bs)); len = in.read(bs); System.out.println("读取的长度是"+len+" 读取的内容是"+new String(bs) ); } catch (Exception e) { e.printStackTrace(); } finally { if(in!=null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class InputStreamDemo2 { public static void main(String[] args) { InputStream in=null; try { in=new FileInputStream("aa.txt"); //定义一个字符串,如果中间有汉字最好长度尽可能长包括 byte[] bs=new byte[3]; //定义读取的长度 int len = 0; while((len=in.read(bs))!=-1){ System.out.print(new String(bs,0,len)); } } catch (Exception e) { e.printStackTrace(); } finally { if(in!=null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
拷贝二进制文件(如压缩包、图片)
import java.io.*; public class CopyDemo1 { public static void main(String[] args) { OutputStream out=null; InputStream in=null; try { //创建字节流输入对象,输入需要读取的对象参数 in=new FileInputStream(new File("C:\\Users\\cheung\\Desktop\\DCIM\\P90122-184132.jpg")); //创建字节流输出对象 out=new FileOutputStream("P90122-184132.jpg"); //创建数组接收读取的内容 byte[] bs=new byte[1024]; int len=0; while((len=in.read(bs))!=-1){ //将读取的内容写入输出 out.write(bs,0,len); } } catch (Exception e) { e.printStackTrace(); } finally { try { if(in!=null){ in.close(); } if(out!=null){ out.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
84.高效缓冲字节流
import java.io.*; public class BufferedCopyDemo2 { public static void main(String[] args) { BufferedInputStream in=null; BufferedOutputStream out=null; try { in=new BufferedInputStream(new FileInputStream("C:\\Users\\cheung\\Desktop\\DCIM\\Selfie\\P80812-152904.jpg")); out=new BufferedOutputStream(new FileOutputStream("P80812-152904.jpg")); byte[] bs=new byte[1024]; //定义一个读取长度标识 int len=0; while((len=in.read(bs))!=-1){ out.write(bs); } } catch (Exception e) { e.printStackTrace(); } finally { try { if(in!=null){ in.close(); } if(out!=null){ out.close(); } } catch (IOException e) { e.printStackTrace(); } } } }