⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileutils.java

📁 java 写的一个新闻发布系统
💻 JAVA
字号:
////                                   ____.//                       __/\ ______|    |__/\.     _______//            __   .____|    |       \   |    +----+       \//    _______|  /--|    |    |    -   \  _    |    :    -   \_________//   \\______: :---|    :    :           |    :    |         \________>//           |__\---\_____________:______:    :____|____:_____\//                                      /_____|////                 . . . i n   j a h i a   w e   t r u s t . . .////////  FileUtils//  EV      19.12.2000//  MAP     24.01.2002  Files are stored into UTF-8 format.//package org.jahia.utils;import org.jahia.exceptions.JahiaException;import java.io.*;                       // File access methodspublic class FileUtils {    /***        * constructor        * EV    19.12.2000        *        */    private FileUtils()    {        JahiaConsole.println( "FileUtils", "***** Starting fileUtils *****" );    }    /***        * getInstance        * EV    19.12.2000        *        */    public static FileUtils getInstance()    {        if (_fileUtils == null) {            _fileUtils = new FileUtils();        }        return _fileUtils;    }    /***        * composeJahiaFileName        * EV    18.11.2000        * MAP   24.01.2002 : What's about these limitations ?        * FIXME     : each jahia site should have its own file directory        * FIXME     : no more than 100 files per directory        *        */    public String composeJahiaFileName( String jahiaDiskPath, int jahiaID, int pageID, int fieldID )    {        String fileName = "";        fileName += jahiaDiskPath + File.separator;        fileName += Integer.toString( jahiaID ) + "-";        fileName += Integer.toString( pageID ) + "-";        fileName += Integer.toString( fieldID );        fileName += ".jahia";        return fileName;    }    /***        * fileExists        * EV    18.11.2000        * called by loadContents        *        */    public boolean fileExists( String fileName )    throws JahiaException    {        try {            File theFile = new File( fileName );            return theFile.exists();        } catch (SecurityException se) {            String errorMsg = "Security error in readFile : " + se.getMessage();            JahiaConsole.println( "FileUtils", errorMsg );            JahiaConsole.println( "FileUtils", "Cannot read files -> BAILING OUT" );            throw new JahiaException( "Cannot access to jahia files",                                      errorMsg, JahiaException.FILE_ERROR, JahiaException.CRITICAL );        }    }    /**     * Read a text file in UTF-8 format.     * Avalaible from Jahia Edition 3. The old Jahia ASCII file are also readable     * but are saved in UTF-8 format.     *     * EV    18.11.2000     * MAP   31.01.2002 Try the UTF-8 format before the the ASCII format     *     * @param String fileName : the absolute path file name.     * @return : the String file _content.     */    public String readFile( String fileName )    throws JahiaException    {        if (!tryToReadFile(fileName, UTF8)) {            JahiaConsole.println("FileUtils.readFile", "Cannot read files in UTF-8 format trying ASCII");            if (!tryToReadFile(fileName, ASCII)) {                JahiaConsole.println("FileUtils.readFile", "Cannot read files -> BAILING OUT");                throw new JahiaException( "Cannot access to jahia files",                                          "Error in readFile : " + fileName,                                          JahiaException.FILE_ERROR, JahiaException.ERROR );            }        }        return _content;    }    /**     * Write a String content to a UTF-8 file format.     *     * EV    18.11.2000     * MAP   24.01.2002 Write into UTF-8 file format     *     * @param String fileName : the absolute path file name.     * @param String fileContent : the String file content     */    public void writeFile( String fileName, String fileContent )    throws JahiaException    {        try {            BufferedWriter out = new BufferedWriter(                                 new OutputStreamWriter(                                 new FileOutputStream(fileName), "UTF-8"));            out.write( fileContent );            out.close();        } catch (IOException ie) {            String errorMsg = "Error in writeFile : " + fileName +                              "\nIOException : " + ie.getMessage();            JahiaConsole.println( "FileUtils", errorMsg );            JahiaConsole.println( "FileUtils", "Cannot write files -> BAILING OUT" );            throw new JahiaException(   "Cannot access to jahia files",                                        errorMsg, JahiaException.FILE_ERROR, JahiaException.CRITICAL );        }    }    /***        * deleteFile        * EV    07.02.2001        *        */    public boolean deleteFile( String fileName )    {        File theFile = new File( fileName );        return theFile.delete();    }    /***        * copyStream        * EV    30.11.2000        * called by download        *        */    public void copyStream( InputStream ins, OutputStream outs)    throws IOException    {        int writeBufferSize = 4096;        byte[] writeBuffer = new byte[writeBufferSize];        BufferedInputStream bis = new BufferedInputStream(ins, writeBufferSize );        BufferedOutputStream bos = new BufferedOutputStream(outs, writeBufferSize );        int bufread;        while((bufread = bis.read(writeBuffer )) != -1) {            bos.write(writeBuffer, 0, bufread);        }        bos.flush();        bos.close();        bis.close();    }    /**     * Try to read a file in an ASCII or UTF-8 format.     * MAP  31.1.2002     * @param   String fileName : The absolute path file name     * @param   int format : The file format supposing to read;                1 = UTF-8, any value = ASCII     * @return  True if success, false if not.     */    private boolean tryToReadFile(String fileName, byte format) {        try {            BufferedReader in;            if (format == UTF8) {                in = new BufferedReader(                     new InputStreamReader(                     new FileInputStream(fileName), "UTF-8"));            } else {                in = new BufferedReader(                     new InputStreamReader(                     new FileInputStream(fileName)));            }            _content = "";                        StringBuffer buff = new StringBuffer();            char[] stringToRead = new char[4096];            int count=-1;            while ((count=in.read(stringToRead, 0, 4096)) != -1) {                buff.append(stringToRead, 0, count);            }            _content = buff.toString();            buff = null;            /*            char[] stringToRead = new char[4096];            int count=-1;            while ((count=in.read(stringToRead, 0, 4096)) != -1) {                _content += new String(stringToRead, 0, count);            }            */            in.close();            return true;        } catch (IOException ie) {            return false;        }    }    private final byte ASCII = 0;    private final byte UTF8 = 1;    private String _content;    private static FileUtils _fileUtils = null;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -