/**
* 异步的磁盘写操作
* @author Administrator
*
*/
public class InputBuffer
{
public static void main(String[] args) throws IOException{
final List<String> list = new ArrayList<String>();//缓冲区
BufferedReader bufferreader = new BufferedReader(new InputStreamReader(System.in));//流
Thread thread = new Thread()
{
public void run()
{
System.out.println("请输入!");
while(true)
{
if(list.isEmpty())
{
try
{
Thread.sleep(3000);
}
catch (InterruptedException e)
{
System.out.println("欢迎下次使用!");
//e.printStackTrace();
}
continue;
}
for (String str : list)
{
try
{
IOUtils.println("log.txt", str);
}
catch (IOException e)
{
e.printStackTrace();
}
}
list.clear();
System.out.println("writing...");
}
}
};
thread.setDaemon(true);
thread.start();
while(true)
{
String s=bufferreader.readLine();
list.add(s);
if(s.equalsIgnoreCase("quit"))
{
thread.interrupt();
bufferreader.close();
break;
}
}
}
}
public class IOUtils {
/**
* 流的复制, 从in里读取数据, 写出到out流中
* @param in
* @param out
* @throws IOException
*/
public static void copy(
InputStream in, OutputStream out)
throws IOException{
//in = new BufferedInputStream(in);
//out = new BufferedOutputStream(out);
//int b;
//while((b=in.read())!=-1){
// out.write(b);
//}
byte[] buf = new byte[1024*1024];//1MB
int count;
while((count=in.read(buf))!=-1){
System.out.println(count);
out.write(buf, 0, count);
}
in.close();
out.close();
}
public static void copy(File in, File out) throws FileNotFoundException, IOException{
copy(new FileInputStream(in), new FileOutputStream(out));
}
public static void copy(String in, String out) throws FileNotFoundException, IOException{
copy(new FileInputStream(in), new FileOutputStream(out));
}
/**
* 序列化对象为一个数组对象
* @param obj
* @return 序列化结果
*/
public static byte[] serial(Serializable obj)
throws IOException{
ByteArrayOutputStream out =
new ByteArrayOutputStream();
ObjectOutputStream oos =
new ObjectOutputStream(out);
oos.writeObject(obj);
oos.close();
return out.toByteArray();
}
/**
* 将一个序列化数据反序列化为一个对象
* @param ary 序列化数据
* @return 对象
* @throws ClassNotFoundException
*/
public static Object unserial(byte[] ary)
throws IOException, ClassNotFoundException{
ObjectInputStream in =
new ObjectInputStream(
new ByteArrayInputStream(ary));
Object o = in.readObject();
in.close();
return o;
}
/**
* 对象的深层复制(克隆)
* @param obj
* @return
* @throws ClassNotFoundException
* @throws IOException
*/
public static Object clone(Serializable obj)
throws IOException, ClassNotFoundException{
return unserial(serial(obj));
}
/**
* 读取文件的全部内容到一个byte数组
* 可以缓存一个"小"文件到堆内存中
*/
public static byte[] read(String filename)
throws IOException{
return read(new File(filename));
}
public static byte[] read(File file)
throws IOException{
//用RAF打开文件
RandomAccessFile raf =
new RandomAccessFile(file, "r");
//安装文件的长度开辟 缓冲区数组(byte数组)
byte[] buf = new byte[(int)raf.length()];
//读取文件的缓冲区
raf.read(buf);
//关闭文件(RAF)
raf.close();
//返回缓冲区数组引用.
return buf;
}
public static byte[] read(InputStream in)
throws IOException{
byte[] ary = new byte[in.available()];
in.read(ary);
in.close();
return ary;
}
/**
* 读取流中到字符数组
* @param in
* @return
*/
public static char[] readChar(Reader in)
throws IOException{
StringBuilder buf = new StringBuilder();
int c;
while((c = in.read())!=-1){
buf.append((char)c);
}
in.close();
return buf.toString().toCharArray();
}
public static char[] readChar(
String filename, String encoding)
throws IOException{
return readChar(new File(filename), encoding);
}
public static char[] readChar(
File file, String encoding)
throws IOException{
return readChar(new FileInputStream(file), encoding);
}
public static char[] readChar(
InputStream in, String encoding)
throws IOException{
return readChar(new InputStreamReader(in, encoding));
}
/**
* 连接byte 数组的全部内容为字符串,
* 以hex(十六进制)形式连接
*/
public static String join(byte[] ary){
if(ary==null || ary.length==0)
return "[]";
StringBuilder buf =
new StringBuilder();
buf.append("[").append(
leftPad(Integer.toHexString(ary[0]&0xff),'0',2));
for(int i=1; i<ary.length; i++){
String hex=Integer.toHexString(ary[i]&0xff);
buf.append(",").append(leftPad(hex,'0',2));
}
buf.append("]");
return buf.toString();
}
public static String toBinString(byte[] ary){
if(ary==null || ary.length==0)
return "[]";
StringBuilder buf =
new StringBuilder();
buf.append("[").append(
leftPad(Integer.toBinaryString(ary[0]&0xff),'0',8));
for(int i=1; i<ary.length; i++){
String hex=Integer.toBinaryString(ary[i]&0xff);
buf.append(",").append(leftPad(hex,'0',8));
}
buf.append("]");
return buf.toString();
}
/**
* 实现leftPad功能, 对字符串实现左填充
* @param str 被填充字符串: 5
* @param ch 填充字符: #
* @param length 填充以后的长度: 8
* @return "#######5"
*/
public static String leftPad(
String str, char ch, int length){
if(str.length() == length){
return str;
}
char[] chs = new char[length];
Arrays.fill(chs, ch);
System.arraycopy(str.toCharArray(), 0, chs,
length - str.length(), str.length());
return new String(chs);
}
/**
* 将text追加到文件 filename的尾部
* 使用系统默认文本编码
* 编码参考: new OutputStreamWriter(out)
* 追加参考: new FileOutputStream(filename, true)
*/
public static void println (
String filename, String text)
throws IOException{
println(new File(filename),text);
}
public static void println(
File file, String text)throws IOException{
println(new FileOutputStream(file,true), text);
}
public static void println(
OutputStream out, String text)throws IOException{
PrintWriter o = new PrintWriter(out);
o.println(text);
o.close();
}
}