IO流操作.docx

上传人:b****4 文档编号:6225035 上传时间:2023-05-09 格式:DOCX 页数:13 大小:17.88KB
下载 相关 举报
IO流操作.docx_第1页
第1页 / 共13页
IO流操作.docx_第2页
第2页 / 共13页
IO流操作.docx_第3页
第3页 / 共13页
IO流操作.docx_第4页
第4页 / 共13页
IO流操作.docx_第5页
第5页 / 共13页
IO流操作.docx_第6页
第6页 / 共13页
IO流操作.docx_第7页
第7页 / 共13页
IO流操作.docx_第8页
第8页 / 共13页
IO流操作.docx_第9页
第9页 / 共13页
IO流操作.docx_第10页
第10页 / 共13页
IO流操作.docx_第11页
第11页 / 共13页
IO流操作.docx_第12页
第12页 / 共13页
IO流操作.docx_第13页
第13页 / 共13页
亲,该文档总共13页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

IO流操作.docx

《IO流操作.docx》由会员分享,可在线阅读,更多相关《IO流操作.docx(13页珍藏版)》请在冰点文库上搜索。

IO流操作.docx

IO流操作

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.nio.channels.FileChannel;

import java.nio.charset.Charset;

import com.d1xn.core.web.WebConfiguration;

/**

 * @author zsf

 * 

 * 资源操作工具类

 */

public class IOUtil {

    // 表示从本地上传

    public static final byte UPLOAD_BY_LOCAL = 0x01;

    // 直接流上传

    public static final byte UPLOAD_BY_INS = 0x02;

    // 网络下载上传

    public static final byte UPLOAD_BY_URL = 0x03;

    /*

     * 保存文件

     */

    public static boolean uploadFile(String fileName, InputStream ins) {

        boolean errorCode = true;

        if (ins == null) {

            return false;

        }

        fileName = IOUtil.parseFilePath(fileName);

        File DBFFile = new File(fileName);

        byte[] bytes = new byte[4 * 1024];

        try {

            while (!

DBFFile.exists()) {

                File parentFile = DBFFile.getParentFile();

                while (!

parentFile.exists()) {

                    parentFile.mkdirs();

                }

                DBFFile.createNewFile();

            }

            FileOutputStream fileOutput = null;

            fileOutput = new FileOutputStream(DBFFile);

            int length = 0;

            ;

            while (length >= 0) {

                length = ins.read(bytes, 0, 1024);

                if (length >= 0) {

                    fileOutput.write(bytes, 0, length);

                }

            }

            ins.close();

            fileOutput.flush();

            fileOutput.close();

        } catch (Exception e) {

            e.printStackTrace();

            errorCode = false;

        }

        return errorCode;

    }

    public static void copyFileNew(String oldPath, String newPath) {

        try {

            FileChannel in = new FileInputStream(oldPath).getChannel();

            FileChannel out = new FileOutputStream(newPath).getChannel();

            in.transferTo(0, in.size(), out);

        } catch (Exception ex) {

            ex.printStackTrace();

        }

    }

    /**

     * �����ļ�����

     * 

     * @param oldPath

     *            ԭ�ļ����·��

     * @param newPath

     *            ���ļ����·��

     */

    public static void copyFile(String oldPath, String newPath) {

        File f = new File(oldPath);

        if (!

f.exists())

            return;

        FileOutputStream fileOutput = null;

        FileInputStream fileinput = null;

        byte[] bytes = new byte[4 * 1024];

        File nf = new File(newPath);

        try {

            if (!

nf.exists()) {

                File pf = nf.getParentFile();

                while (!

pf.exists()) {

                    pf.mkdirs();

                }

                pf.createNewFile();

            }

            fileinput = new FileInputStream(f);

            fileOutput = new FileOutputStream(nf);

            int length = 0;

            while (length >= 0) {

                length = fileinput.read(bytes, 0, 1024);

                if (length >= 0) {

                    fileOutput.write(bytes, 0, length);

                }

            }

        } catch (Exception e) {

            copyFile(oldPath, newPath);

            e.printStackTrace();

        } finally {

            try {

                if (fileinput !

= null)

                    fileinput.close();

                if (fileOutput !

= null) {

                    fileOutput.flush();

                    fileOutput.close();

                }

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

    /**

     * 将指定文件输出至输出流

     * 

     * @param fileFullName

     *            完整物理路径

     * @param out

     * @return

     * @throws IOException

     */

    public static boolean fileOutput(String fileFullName, OutputStream out)

            throws IOException {

        BufferedInputStream bis = null;

        BufferedOutputStream bos = null;

        try {

            bis = new BufferedInputStream(new FileInputStream(fileFullName));

            bos = new BufferedOutputStream(out);

            byte[] buff = new byte[4096];

            int bytesRead;

            while (-1 !

= (bytesRead = bis.read(buff, 0, buff.length))) {

                bos.write(buff, 0, bytesRead);

            }

        } catch (FileNotFoundException fnfex) {

            return false;

        } catch (final IOException e) {

            // System.out.println ( "IOException." + e );

        } finally {

            if (bis !

= null)

                bis.close();

            if (bos !

= null)

                bos.close();

        }

        return true;

    }

    /*

     * 得到文件的内容

     */

    public static OutputStream getFile(String fileName, OutputStream ops) {

        if (ops == null)

            return null;

        fileName = IOUtil.parseFilePath(fileName);

        File DBFFile = new File(fileName);

        byte[] bytes = new byte[4 * 1024];

        try {

            FileInputStream fileInput = null;

            fileInput = new FileInputStream(DBFFile);

            int length = 0;

            ;

            while (length >= 0) {

                length = fileInput.read(bytes, 0, 1024);

                if (length >= 0) {

                    ops.write(bytes, 0, length);

                }

            }

            fileInput.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

        return ops;

    }

    /*

     * 创建文件目录()

     */

    public static boolean createSingleFolder(String calalogName) {

        boolean errorCode = false;

        calalogName = IOUtil.parseFilePath(calalogName);

        File file = new File(calalogName);

        errorCode = file.mkdir();

        return errorCode;

    }

    /*

     * 创建文件目录()

     */

    public static boolean createMulFolder(String calalogName) {

        boolean errorCode = false;

        calalogName = IOUtil.parseFilePath(calalogName);

        File file = new File(calalogName);

        errorCode = file.mkdirs();

        return errorCode;

    }

    /*

     * 创建文件(假定文件的父亲目录已经存在) @param fileName 文件的全路径名

     */

    public static boolean createSingleFile(String fileName) {

        boolean errorCode = true;

        /* 判断该文件的目录是否存在 */

        if (fileName == null)

            return false;

        int lastSeparator = fileName.lastIndexOf(File.separator);

        if (lastSeparator !

= -1) {

            String parentCal = fileName.substring(0, lastSeparator);

            File file = new File(parentCal);

            if (!

file.isDirectory()) {

                createMulFolder(fileName.substring(0, lastSeparator));

            }

        }

        File file = new File(IOUtil.parseFilePath(fileName));

        try {

            file.createNewFile();

        } catch (IOException e) {

            e.printStackTrace();

            errorCode = false;

        }

        return errorCode;

    }

    /*

     * 删除目录 @param 被删除的目录

     */

    public static boolean delFolder(File calalog) {

        boolean errorCode = true;

        if (!

calalog.isDirectory()) {

            calalog.delete();

        } else {

            File[] entries = calalog.listFiles();

            int sz = entries.length;

            for (int i = 0; i < sz; i++) {

                if (entries[i].isDirectory()) {

                    delFolder(entries[i]);

                } else {

                    entries[i].delete();

                }

            }

            calalog.delete();

        }

        return errorCode;

    }

    // TO DO

    public static boolean dragFolder(String oldName, String newName) {

        return dragFile(oldName, newName);

    }

    // TO DO

    public static boolean dragFile(String oldName, String newName) {

        boolean errorCode = false;

        oldName = IOUtil.parseFilePath(oldName);

        newName = IOUtil.parseFilePath(newName);

        File oldFile = new File(oldName);

        File newFile = new File(newName);

        errorCode = oldFile.renameTo(newFile);

        return errorCode;

    }

    /*

     * 通过文件系统得到文件的内容

     */

    public static String getXmlFile(String path) {

        StringBuffer fileStringBuffer = new StringBuffer();

        path = IOUtil.parseFilePath(path);

        try {

            FileReader fis = new FileReader(path);

            BufferedReader bReader = new BufferedReader(fis);

            String tempLine = null;

            do {

                tempLine = bReader.readLine();

                fileStringBuffer.append(tempLine);

            } while (tempLine !

= null && tempLine.length() > 0);

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        return fileStringBuffer.toString();

    }

    /*

     * 

     * @author zsf

     * 

     */

    public static String parseFilePath(String filePath) {

        if (filePath == null)

            return null;

        char[] pathCharArray = filePath.toCharArray();

        StringBuffer pathBuffer = new StringBuffer();

        Character char1 = new Character('\\');

        Character char2 = new Character('/');

        for (int i = 0; i < pathCharArray.length; i++) {

            if (char1.equals(new Character(pathCharArray[i]))

                    || char2.equals(new Character(pathCharArray[i]))) {

                pathBuffer.append(File.separator);

            } else {

                pathBuffer.append(pathCharArray[i]);

            }

        }

        // filePath

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2