📄 iotools.java
字号:
/*
Copyright (c) 2001-2003 sync4j project
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The name "sync4j" must not be used to endorse or promote products
derived from this software without prior written permission.
4. Products derived from this software may not be called "sync4j", nor
may "sync4j" appear in their name, without prior written permission.
In addition, we request (but do not require) that you include in the
end-user documentation provided with the redistribution and/or in the
software itself an acknowledgement equivalent to the following:
"This product includes software developed by the
sync4j project."
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE SYNC4J AUTHORS OR THE PROJECT
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
package com.ultrapower.tools;
import java.io.*;
/**
* Container of utility methods for io access
*
* @author Stefano Fornari @ Funambol
* @version $Id: IOTools.java,v 1.3 2003/12/15 16:58:28 luigiafassina Exp $
*/
public class IOTools {
/**
* Reads a file into a byte array given its filename
*
* @param file the filename (as java.io.File)
*
* @return the content of the file as a byte array
*
* @throws java.io.IOException;
*/
static public byte[] readFileBytes(File file)
throws IOException {
FileInputStream fis = null;
byte[] buf = new byte[(int)file.length()];
try {
fis = new FileInputStream(file);
fis.read(buf);
fis.close();
} finally {
if (fis != null) {
fis.close();
}
}
return buf;
}
/**
* Reads a file into a byte array given its filename
*
* @param filename the filename (as java.lang.String)
*
* @return the content of the file as a byte array
*
* @throws java.io.IOException;
*/
static public byte[] readFileBytes(String filename)
throws IOException {
return readFileBytes(new File(filename));
}
/**
* Reads a file into a String given its filename
*
* @param file the filename (as java.io.File)
*
* @return the content of the file as a string
*
* @throws java.io.IOException;
*/
static public String readFileString(File file)
throws IOException {
return new String(readFileBytes(file));
}
/**
* Reads a file into a String given its filename
*
* @param filename the filename (as java.lang.String)
*
* @return the content of the file as a string
*
* @throws java.io.IOException;
*/
static public String readFileString(String filename)
throws IOException {
return readFileString(new File(filename));
}
/**
* Writes the given string to the file with the given name
*
* @param str the string to write
* @param file the file name as a java.io.File
*
* @throws java.io.IOException
*/
static public void writeFile(String str, File file)
throws IOException {
writeFile(str.getBytes(), file);
}
/**
* Writes the given string to the file with the given name
*
* @param str the string to write
* @param filename the file name as a java.lang.String
*
* @throws java.io.IOException
*/
static public void writeFile(String str, String filename)
throws IOException {
writeFile(str.getBytes(), new File(filename));
}
/**
* Writes the given bytes to the file with the given name
*
* @param buf the bytes to write
* @param filename the file name as a java.lang.String
*
* @throws java.io.IOException
*/
static public void writeFile(byte[] buf, String filename)
throws IOException {
writeFile(buf, new File(filename));
}
/**
* Writes the given bytes to the file with the given name
*
* @param buf the bytes to write
* @param file the file name as a java.io.File
*
* @throws java.io.IOException
*/
static public void writeFile(byte[] buf, File file)
throws IOException {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(buf);
fos.close();
} finally {
if (fos != null) {
fos.close();
}
}
}
/**
* Returns a <i>FilenameFilter</i> that accepts only the files of the given
* type (extension).
*
* @param type the type (the file extension) of the files to select. NULL
* means all files, the empty string means files without extension
* The filtering is case-insensitive
*
* @return the filter
*/
public static FilenameFilter getFileTypeFilter(String type) {
return new FileTypeFilter(type);
}
// -------------------------------------------------------------------------
/**
* This class is a <i>FilenameFilter</i> that accepts only the files of the
* specified type (extension). The filtering is case-insensitive,
*/
public static class FileTypeFilter implements FilenameFilter {
private String type;
/**
* Creates the filter on the given type.
*
* @param type the type (the file extension) of the files to select. NULL
* means all files, the empty string means files without
* extension. The filtering is case-insensitive
*/
public FileTypeFilter(final String type) {
this.type = type.toUpperCase();
}
public boolean accept(File dir, String name) {
if (type == null) {
return true;
}
if (type.length() == 0) {
return (name.indexOf('.') < 0);
}
return (name.toUpperCase().endsWith(type));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -