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

📄 files.java

📁 一个自然语言处理的Java开源工具包。LingPipe目前已有很丰富的功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * LingPipe v. 3.5 * Copyright (C) 2003-2008 Alias-i * * This program is licensed under the Alias-i Royalty Free License * Version 1 WITHOUT ANY WARRANTY, without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the Alias-i * Royalty Free License Version 1 for more details. * * You should have received a copy of the Alias-i Royalty Free License * Version 1 along with this program; if not, visit * http://alias-i.com/lingpipe/licenses/lingpipe-license-1.txt or contact * Alias-i, Inc. at 181 North 11th Street, Suite 401, Brooklyn, NY 11211, * +1 (718) 290-9170. */package com.aliasi.util;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.CharArrayWriter;import java.io.File;import java.io.FileFilter;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.OutputStreamWriter;import java.io.Serializable;import java.util.ArrayList;/** * Static utility methods for processing files. * * @author  Bob Carpenter * @version 3.0 * @since   LingPipe1.0 */public class Files {    /**     * Forbid instance construction.     */    private Files() {        /* do nothing */    }    /**     * Writes the specified bytes to the specified file.     *     * @param bytes Bytes to write to file.     * @param file File to which characters are written.     * @throws IOException If there is an underlying I/O exception.     */    public static void writeBytesToFile(byte[] bytes, File file)        throws IOException {        FileOutputStream out = new FileOutputStream(file);        out.write(bytes);        Streams.closeOutputStream(out);    }    /**     * Returns the array of bytes read from the specified file.     *     * @param file File from which to read bytes.     * @return Bytes read from the specified file.     * @throws IOException If there is an underlying I/O exception.     */    public static byte[] readBytesFromFile(File file)        throws IOException {        FileInputStream in = new FileInputStream(file);        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();        Streams.copy(in,bytesOut);        Streams.closeInputStream(in);        return bytesOut.toByteArray();    }    /**     * Writes the characters to the specified file, encoded     * using the specified character set.     *     * @param chars Characters to write to file.     * @param file File to which characters are written.     * @param charset Character set used to encoded characters.     * @throws IOException If there is an underlying I/O exception.     */    public static void writeCharsToFile(char[] chars,                                        File file, String charset)        throws IOException {        FileOutputStream out = new FileOutputStream(file);        OutputStreamWriter writer = new OutputStreamWriter(out,charset);        writer.write(chars);        Streams.closeWriter(writer);    }    /**     * Writes the characters to the specified file, encoded     * using UTF-8 Unicode.     *     * @param chars Characters to write to file.     * @param file File to which characters are written.     * @throws IOException If there is an underlying I/O exception.     */    public static void writeCharsToFile(char[] chars, File file)        throws IOException {        writeCharsToFile(chars,file,Strings.UTF8);    }    /**     * Writes the string to the specified file, encoded using UTF-8     * Unicode.     *     * @param s String to write to file.     * @param file File to which characters are written.     * @throws IOException If there is an underlying I/O exception.     */    public static void writeStringToFile(String s, File file)        throws IOException {        writeCharsToFile(s.toCharArray(),file);    }    /**     * Writes the string to the specified file, encoded using the     * specified character set.     *     * @param s String to write to file.     * @param file File to which characters are written.     * @param charset Character set to use for encoding.     * @throws IOException If there is an underlying I/O exception.     */    public static void writeStringToFile(String s, File file, String charset)        throws IOException {        writeCharsToFile(s.toCharArray(),file,charset);    }    /**     * Reads all of the bytes from the specified file and convert     * them to a character array using the specified character set.     *     * @param file File from which to read input.     * @param charset Charset to decode bytes in file.     * @return Characters in the file.     * @throws IOException If there is an underlying I/O exception.     * @throws UnsupportedEncodingException If the charset is not     * supported.     */    public static char[] readCharsFromFile(File file, String charset)        throws IOException {        CharArrayWriter charWriter = new CharArrayWriter();        FileInputStream in = null;        InputStreamReader inReader = null;        BufferedReader bufferedReader = null;        try {            in = new FileInputStream(file);            inReader = new InputStreamReader(in,charset);            bufferedReader = new BufferedReader(inReader);            Streams.copy(bufferedReader,charWriter);        } finally {            Streams.closeReader(bufferedReader);            Streams.closeReader(inReader);            Streams.closeInputStream(in);        }        return charWriter.toCharArray();    }    /**     * Reads all of the bytes from the specified file and convert     * them to a character array using UTF-8 unicode.     *     * @param file File from which to read input.     * @return Characters in the file.     * @throws IOException If there is an underlying I/O exception.     */    public static char[] readCharsFromFile(File file)        throws IOException {        return readCharsFromFile(file,Strings.UTF8);    }    /**     * Reads all of the bytes from the specified file and convert     * them to a string using the specified character set.     *     * @param file File from which to read input.     * @param charset Charset to decode bytes in file.     * @return Characters in the file.     * @throws IOException If there is an underlying I/O exception.     * @throws UnsupportedEncodingException If the charset is not supported.     */    public static String readFromFile(File file, String charset)        throws IOException {        return new String(readCharsFromFile(file,charset));    }    /**     * Reads all of the bytes from the specified file and convert     * them to a string using Unicode UTF-8.     *     * @param file File from which to read input.     * @return Characters in the file.     * @throws IOException If there is an underlying I/O exception.     */    public static String readFromFile(File file) throws IOException {        return readFromFile(file,Strings.UTF8);    }    /**     * Reads a serialized object from the specified file.     *     * @param file File from which to read object.     * @return Object read from file.     * @throws IOException If there is an I/O error reading.     * @throws ClassNotFoundException If the serialized object's class     * is not on the classpath.     */    public static Object readObjectFrom(File file)        throws ClassNotFoundException, IOException {        FileInputStream fileIn = null;        BufferedInputStream bufIn = null;        ObjectInputStream objIn = null;        try {            fileIn = new FileInputStream(file);            bufIn = new BufferedInputStream(fileIn);            objIn = new ObjectInputStream(bufIn);            return objIn.readObject();        } finally {            Streams.closeInputStream(objIn);            Streams.closeInputStream(bufIn);            Streams.closeInputStream(fileIn);        }    }    /**     * Writes the specified object to the specified file.     *     * @param object Object to write.     * @param file File to which object is serialized.     * @throws IOException If there is an underlying I/O error     * writing.     */    public static void writeObjectTo(Serializable object, File file)        throws IOException {        FileOutputStream fileOut = null;        BufferedOutputStream bufOut = null;        ObjectOutputStream objOut = null;

⌨️ 快捷键说明

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