JAVA IO流学习-----字符流,字节流

it2026-02-03  1

1.通过字符流实现文本文件的复制

/** * 字符流处理文本文档复制 */ public static void copyText(String srcPath,String descPath){ FileReader fr = null; FileWriter fw = null; try { fr = new FileReader(new File(srcPath)); fw = new FileWriter(new File(descPath)); char[] chars = new char[15]; int len; while ((len = fr.read(chars)) != -1) { //read()返回读取的字符个数 如果为-1,表示没有数据 fw.write(chars, 0, len); } System.out.println("复制成功"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(fr != null){ try { fr.close(); } catch (IOException e) { e.printStackTrace(); }finally { if(fw != null){ try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } } } } /** * 带有缓冲区的字符流处理文本文档复制 */ public static void copyTextWithBuffer(String srcPath,String descPath){ BufferedReader br = null; BufferedWriter bw = null; try { FileReader fr = new FileReader(new File(srcPath)); FileWriter fw = new FileWriter(new File(descPath)); br = new BufferedReader(fr); bw = new BufferedWriter(fw); char[] chars = new char[15]; int len; while((len = br.read(chars)) != -1){ bw.write(chars,0,len); } System.out.println("复制成功"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(br != null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); }finally { if(bw != null){ try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } } }

 2.使用字节流对非文本文档进行复制操作

/* 字节流处理非文本文档 */ public static void copyTxt(String srcPath,String descPath) { long begin = System.currentTimeMillis(); FileInputStream ism = null; FileOutputStream fos = null; try { ism = new FileInputStream(new File(srcPath)); fos = new FileOutputStream(new File(descPath)); byte[] bytes = new byte[15]; int len; while ((len = ism.read(bytes))!= -1){ fos.write(bytes,0,len); } System.out.println("复制成功"); } catch (IOException e) { e.printStackTrace(); } finally { if (ism != null) { try { ism.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } } long end = System.currentTimeMillis(); System.out.println("不带缓冲流的文件复制耗时:"+(end-begin)+"ms"); } /** * 字节缓冲流实现非文本文档复制 */ public static void copyTextWithBuffer(String srcPath,String descPath){ long begin = System.currentTimeMillis(); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { InputStream is = new FileInputStream(new File(srcPath)); OutputStream os = new FileOutputStream(new File(descPath)); bis = new BufferedInputStream(is); bos = new BufferedOutputStream(os); byte[] bytes = new byte[15]; int len; while ((len = bis.read(bytes)) != -1){ bos.write(bytes,0,len); } System.out.println("复制成功"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(bis != null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); }finally { if(bos != null){ try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } } long end = System.currentTimeMillis(); System.out.println("带缓冲区的文档复制耗时为:"+(end-begin)+"ms"); }

 

最新回复(0)