zoukankan      html  css  js  c++  java
  • Java字节流文件封装

     /**
     * 字节流封装方法
     */
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    public class FileUtilStream {
    //<!----------------------------------------------------读取文件---------------------------------------------------->
    public static String readFile(String path){
    StringBuffer sb=new StringBuffer();
    //1.创建文件的字节流
    InputStream fis=null;
    try {
    fis=new FileInputStream(path);//InputStream为抽象类,不能实例化
    byte [] bytes=new byte[1024];//数组
    int data=0;
    while ((data=fis.read(bytes))!=-1) {//(data=fis.read(bytes))!=-1表示读到最后了
    //字符串的转换
    String str=new String(bytes,0,data);//后面加,0,data是防止1024出现0000000
    sb.append(str);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    finally{
            //2.关闭字节流
    try {
    if (fis!=null) {
    fis.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return  sb.toString();
    }
    //<!----------------------------------------------------写入文件---------------------------------------------------->
    public static boolean writeFile(String path,String content,boolean flag){
    boolean result=false;
    //1.创建文件的字节流
            OutputStream fos=null;
            try {
                    //2.创建 输出流对象
    fos=new FileOutputStream(path, flag);//flag表示是否覆盖,flag=true表示在末尾添加,flag=false表示覆盖
    //字符串String--->byte[]
    byte[] words=content.getBytes();
    //写入
    fos.write(words, 0, words.length);
    result=true;
    } catch (Exception e) {
    }
    finally{
    try {
    if (fos!=null) {
    fos.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return result;
    }
    //<!----------------------------------------------------文件的复制---------------------------------------------------->
    public static boolean copyFile(String frompath,String toPath){
    boolean result=false;
    // 文件的复制:先读取要复制的文件,再写入新文件
    //1.读取文件
    InputStream fis=null;
    //2.写入文件
    OutputStream fos=null;
    try {
    //3.读取
    fis=new FileInputStream(frompath);
    //4.写入
    fos=new FileOutputStream(toPath);
    //5.边读边写
    StringBuffer content=new StringBuffer();
    //【读】
    byte [] bytes=new byte[1024];//数组
    int data=0;
    while ((data=fis.read(bytes))!=-1) {//(data=fis.read(bytes))!=-1表示读到最后了
    //字符串的转换
    String str=new String(bytes,0,data);//后面加,0,data是防止1024出现0000000
    content.append(str);
    }
           //【写】
    //字符串String--->byte[]
    byte[] words=content.toString().getBytes();
    //写入
    fos.write(words, 0, words.length);
    result=true;
    } catch (Exception e) {
    }
    finally{
    try {
    if (fis!=null) {
    fis.close();
    }
    if (fos!=null) {
    fos.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return result;
    }
    }
  • 相关阅读:
    02.HTML中使用JavaScript--JavaScript高级程序设计(笔记)
    01.JavaScript简介——JavaScript高级程序设计(笔记)
    09.领导力发展的案例摘录——卓越领导者
    07.必须修正致命弱点摘录——卓越领导者
    06.卓越领导者拥有多项优势摘录——卓越领导者
    05.领导者必须契合与组织摘录——卓越领导者
    04.何为”领导素质“——卓越领导者
    03.简化领导力摘录——卓越领导者
    02.卓越领导者创造非凡——卓越领导者
    01.揭开领导力的神秘面纱摘录——卓越领导者
  • 原文地址:https://www.cnblogs.com/JonaLin/p/11018238.html
Copyright © 2011-2022 走看看