package cross.pauliuyou.io; import java.awt.Component; import java.util.Date; import java.util.HashMap; import javax.swing.JOptionPane; /** * 弹出报警窗口.同一标题的窗口只能弹出一次,关闭以后才可以再弹出 * @author 刘优 * @version 1.0 */ public class MsgBox { private static HashMap isWindowShowingMap = new HashMap(); public static String msgBoxTitleHead = ""; /** * 弹出消息窗口 * @param msgBody 消息体 */ public static void msg(Object msgBody) { show(null,msgBody,"消息窗口",1,false); } /** * 弹出消息窗口 * @param msgBody 消息体 */ public static void msg(Object msgBody,boolean create) { show(null,msgBody,"消息窗口",1,false); } /** * 弹出消息窗口 * @param msgBody 消息体 */ public static void msg(Component parentWindow,String msgBody) { show(parentWindow,msgBody,"消息窗口",1,false); } /** * 弹出消息窗口 * @param msgBody 消息体 */ public static void msg(Component parentWindow,String msgBody,boolean createNew) { show(parentWindow,msgBody,"消息窗口",1,createNew); } /** * 弹出消息窗口 * @param msgBody 消息体 * @param title 消息标题 */ public static void msg(String msgBody,String title) { show(null,msgBody,title,1,false); } /** * 弹出消息窗口 * @param msgBody 消息体 * @param title 消息标题 */ public static void msg(String msgBody,String title,boolean createNew) { show(null,msgBody,title,1,createNew); } public static void msg(Component parentWindow,String msgBody,String title) { show(parentWindow,msgBody,title,1,false); } public static void msg(Component parentWindow,String msgBody,String title,boolean createNew) { show(parentWindow,msgBody,title,1,createNew); } /** * 弹出警告窗口 * @param msgBody 警告消息 */ public static void alert(Object msgBody) { show(null,msgBody,"警告窗口",2,false); } public static void alert(Object msgBody,boolean createNew) { show(null,msgBody,"警告窗口",2,createNew); } /** * 弹出警告窗口 * @param msgBody 警告消息 */ public static void alert(Component parentWindow,String msgBody) { show(parentWindow,msgBody,"警告窗口",2,false); } public static void alert(Component parentWindow,String msgBody,boolean createNew) { show(parentWindow,msgBody,"警告窗口",2,createNew); } /** * 弹出警告窗口 * @param msgBody 警告消息体 * @param title 警告标题 */ public static void alert(String msgBody,String title) { show(null,msgBody,title,2,false); } public static void alert(String msgBody,String title,boolean createNew) { show(null,msgBody,title,2,createNew); } /** * 弹出警告窗口 * @param msgBody 警告消息体 * @param title 警告标题 */ public static void alert(Component parentWindow,Object msgBody,String title) { show(parentWindow,msgBody,title,2,false); } public static void alert(Component parentWindow,Object msgBody,String title,boolean createNew) { show(parentWindow,msgBody,title,2,createNew); } /** * 弹出问题窗口 * @param msgBody 问题消息体 */ public static void question(Object msgBody) { show(null,msgBody,"问题窗口",3,false); } public static void question(Object msgBody,boolean createNew) { show(null,msgBody,"问题窗口",3,createNew); } /** * 弹出问题窗口 * @param msgBody 问题消息体 */ public static void question(Component parentWindow,String msgBody) { show(parentWindow,msgBody,"问题窗口",3,false); } public static void question(Component parentWindow,String msgBody,boolean createNew) { show(parentWindow,msgBody,"问题窗口",3,createNew); } /** * 弹出问题窗口 * @param msgBody 问题消息体 * @param title 问题标题 */ public static void question(String msgBody,String title) { show(null,msgBody,title,3,false); } public static void question(String msgBody,String title,boolean createNew) { show(null,msgBody,title,3,createNew); } /** * 弹出问题窗口 * @param msgBody 问题消息体 * @param title 问题标题 */ public static void question(Component parentWindow,String msgBody,String title) { show(parentWindow,msgBody,title,3,false); } public static void question(Component parentWindow,String msgBody,String title,boolean createNew) { show(parentWindow,msgBody,title,3,createNew); } /** * 弹出错误窗口 * @param msgBody 误消息体 */ public static void error(Object msgBody) { show(null,msgBody,"错误窗口",0,false); } public static void error(Object msgBody,boolean createNew) { show(null,msgBody,"错误窗口",0,createNew); } /** * 弹出错误窗口 * @param msgBody 误消息体 */ public static void error(Component parentWindow,String msgBody) { show(parentWindow,msgBody,"错误窗口",0,false); } public static void error(Component parentWindow,String msgBody,boolean createNew) { show(parentWindow,msgBody,"错误窗口",0,createNew); } /** * 弹出错误窗口 * @param msgBody 错误消息体 * @param title 错误标题 */ public static void error(String msgBody,String title) { show(null,msgBody,title,0,false); } public static void error(String msgBody,String title,boolean createNew) { show(null,msgBody,title,0,createNew); } /** * 弹出错误窗口 * @param msgBody 错误消息体 * @param title 错误标题 */ public static void error(Component parentWindow,String msgBody,String title) { show(parentWindow,msgBody,title,0,false); } public static void error(Component parentWindow,String msgBody,String title,boolean createNew) { show(parentWindow,msgBody,title,0,createNew); } /** * 弹出提示窗口 * @param msgBody 提示消息体 * @param title 提示标题 * @param windowType 提示窗口类型(3-问题窗口,2-警告窗口,1-消息窗口,0-错误窗口) */ public static void show(Component parentWindow,Object msgBody,String title,int windowType,boolean createNew) { String titleInfor = msgBoxTitleHead + title; boolean isShowing = false; Object obj = isWindowShowingMap.get(titleInfor); if (obj != null) { isShowing = ((Boolean)obj).booleanValue(); } if (!isShowing || createNew) { new ShowBoxThread(parentWindow,msgBody,title,windowType,createNew).start(); } } private static class ShowBoxThread extends Thread { private Component parentWindow; private String infor; private String titleInfor; private int theType; private boolean createNew; public ShowBoxThread(Component parentWindow,Object msgBody,String title,int windowType,boolean createNew) { this.parentWindow = parentWindow; this.infor = msgBody.toString(); this.titleInfor = msgBoxTitleHead + title; this.theType = windowType; this.createNew = createNew; } public void run() { setName("MsgBox<" + titleInfor + ">"); if (!createNew) { isWindowShowingMap.put(titleInfor,new Boolean(true)); try { JOptionPane.showMessageDialog(parentWindow,new Date() + " > " + infor,titleInfor,theType); } catch (Exception e) { error("消息窗口的类型参数不正确.(3-问题窗口,2-警告窗口,1-消息窗口,0-错误窗口)"); } isWindowShowingMap.put(titleInfor,new Boolean(false)); } else { try { JOptionPane.showMessageDialog(parentWindow,new Date() + " > " + infor,titleInfor,theType); } catch (Exception e) { error("消息窗口的类型参数不正确.(3-问题窗口,2-警告窗口,1-消息窗口,0-错误窗口)"); } } } } public static void main(String [] args) { //winMsgBox("test1","title1",1); msg("test1","title1"); // question("test2"); // question("test2","title2"); // alert("test3"); // alert("test3","title3"); // error("test4"); // error("test4","title4"); //show(null,"test5","title5",4); } } /** * */ package cross.pauliuyou.io; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.nio.ByteBuffer; import java.util.Date; import java.util.List; import java.util.Vector; /** * 普通文件类描述对象 * * @author 刘优 * */ public class NormalFileInfor extends FileInfor { /** * 文件缓冲区 */ private byte[] fileBuffer; /** * 文件编码 */ private String javaEncode; /** * 文件编码,显示用 */ private String encode; /** * 设置为新编码 */ private String toEncode; /** * 文件修改时间 */ private long modifyTime; /** * 文件修改时间描述串 */ private String modifyTimeString; /** * 正常显示的字串 */ private String normalString; /** * 十六进制显示的字串 */ private String hexString; /** * 文件内容的行的集合 */ private List contentList; /** * 用文件名构造对象 * @param fileName 文件名 * @throws IOException */ public NormalFileInfor(String fileName) throws IOException { this(new File(fileName)); } /** * 用文件构造对象 * @param file * @throws IOException */ public NormalFileInfor(File file) throws IOException { super(file); if (file == null || !isFileOk()) { throw new IOException("File [" + file + "] Not OK"); } // 如果是已经存在的文件 if (!newCreatedFile) { // 读文件内容 readFile(); // 获取文件属性 getFileProperty(); // 如果文件不为空 if (length > 0) { // 检测文件的编码方式 checkEncode(); // 检测文件是否为二进制文件 checkBinary(); } } } /** * 判断文件是否准备好 * @return */ private boolean isFileOk() { // 文件不存在,则创建新文件 if (!file.exists()) { try { file.createNewFile(); newCreatedFile = true; } catch (IOException e) { return false; } } // 必须是文件而且可读可写 if (!file.isFile() || !file.canRead() || !file.canWrite()) { return false; } return true; } /** * 取得文件属性 * */ private void getFileProperty() { length = file.length(); long newValue = 0; if (length < 1024) { lengthString = length + "B"; } else if (length < (1024 * 1024)) { newValue = length / 1024; lengthString = newValue + "KB"; } else if (length < (1024 * 1024 * 1024)) { newValue = length / (1024 * 1024); lengthString = newValue + "MB"; } else { newValue = length / (1024 * 1024 * 1024); lengthString = newValue + "GB"; } modifyTime = file.lastModified(); modifyTimeString = new Date(modifyTime).toString(); propertyString = "大小:" + lengthString + "/t"; propertyString += "编码:" + encode + "/t"; propertyString += "时间:" + modifyTimeString; } /** * 读取文件 * @throws IOException */ private void readFile() throws IOException { FileInputStream fin = new FileInputStream(file); ByteBuffer buffer = ByteBuffer.allocate(fin.available()); fin.read(buffer.array()); fileBuffer = buffer.array(); } /** * */ private void checkEncode() { encode = "UNKNOWN"; BytesEncodingDetect sinodetector = new BytesEncodingDetect(); try { int code = sinodetector.detectEncoding(file); javaEncode = Encoding.javaname[code]; encode = Encoding.nicename[code]; } catch (Exception ex) { ex.printStackTrace(); } } /** * @return */ private void checkBinary() { if (encode.equals("HZ") || encode.equals("OTHER") || encode.equals("ASCII")) { isBinary = false; for (int j = 0; j < length; j++) { byte b = fileBuffer[j]; if (b < 0) { continue; } if (b < 32 && b != 9 && b != 10 && b != 13) { isBinary = true; encode = "BINARY"; break; } } } } /** * @return the fileBuffer */ public byte[] getFileBuffer() { return fileBuffer; } /** * @return the modifyTime */ public long getModifyTime() { return modifyTime; } /** * @return the modifyTimeString */ public String getModifyTimeString() { return modifyTimeString; } /** * @return the encode */ public String getEncode() { return encode; } /** * 重新加载文件 * @return * @throws IOException */ public boolean reload() throws IOException { if (modified) { readFile(); modified = false; normalString = null; hexString = null; return true; } return false; } /** * 保存文件缓冲区数据到文件中 * @throws IOException */ public void save() throws IOException { write(fileBuffer); } /** * 简单读取文件(默认编码) * @return * @throws IOException */ public String readSimple() throws IOException { reload(); return new String(fileBuffer); } /** * 用探测到的编码方式读取文件 */ public String read() throws IOException { return getNormalDisplay(); } /** * 用指定的编码方式读取文件 * */ public String read(String encode) throws IOException { if (this.encode.equals(encode)) { return read(); } toEncode = encode; BufferedReader fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(file),encode)); StringBuffer sb = new StringBuffer(); while (true) { String line = fileReader.readLine(); if (line == null) { break; } sb.append(line).append("/n"); } return sb.toString(); } /** * 用默认的编码来读取文本文件,保存到集合中 * @return * @throws IOException */ public List readToListSimple() throws IOException { if (reload() || contentList == null) { BufferedReader fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); contentList = new Vector(); while (true) { String line = fileReader.readLine(); if (line == null) { break; } contentList.add(line); } } return contentList; } /** * 用探测到的编码来读取文本文件,保存到集合中 * @return * @throws IOException */ public List readToList() throws IOException { BufferedReader fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(file),encode)); List content = new Vector(); while (true) { String line = fileReader.readLine(); if (line == null) { break; } content.add(line); } return content; } /** * 用指定的编码来读取文本文件,保存到集合中 * @param encode 指定编码 * @return * @throws IOException */ public List readToList(String encode) throws IOException { toEncode = encode; BufferedReader fileReader = new BufferedReader(new InputStreamReader(new FileInputStream(file),encode)); List content = new Vector(); while (true) { String line = fileReader.readLine(); if (line == null) { break; } content.add(line); } return content; } /** * 用默认的编码方式写文件 * @param newContent * @throws IOException */ public void writeSimple(String newContent) throws IOException { write(newContent.getBytes()); } /** * 用默认的编码方式写文件 * @param newContent * @throws IOException */ public void writeListSimple(List newContent) throws IOException { PrintWriter fileWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file))); for (int i = 0; i < newContent.size(); i++) { fileWriter.println(newContent.get(i)); } fileWriter.close(); } /** * 用探测到的编码方式写文本文件 * @param newContent * @throws IOException */ public void writeList(List newContent) throws IOException { PrintWriter fileWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file),encode)); for (int i = 0; i < newContent.size(); i++) { fileWriter.println(newContent.get(i)); } fileWriter.close(); } /** * 用指定的编码方式写文本文件 * @param newContent * @param newEncode * @throws IOException */ public void writeList(List newContent,String newEncode) throws IOException { javaEncode = newEncode; encode = newEncode; PrintWriter fileWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file),encode)); for (int i = 0; i < newContent.size(); i++) { fileWriter.println(newContent.get(i)); } fileWriter.close(); } /** * 用探测到的编码方式写文本文件 */ public void write(String newContent) throws IOException { writeText(newContent,javaEncode); } /** * 用指定的编码方式写文本文件 * @param newContent * @param newEncode * @throws IOException */ public void write(String newContent,String newEncode) throws IOException { javaEncode = newEncode; encode = newEncode; writeText(newContent,newEncode); } /** * 用指定的编码方式写文本文件 * @param bytes * @param newEncode */ private void writeText(String text, String newEncode) throws IOException { PrintWriter textWriter = null; if (newEncode != null) { textWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file),newEncode)); } else { textWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file))); } textWriter.print(text); textWriter.close(); modified = true; isSaved = true; } /** * 把指定的缓冲区数据写到文件中, 文件原来的内容将丢失 * @param newBuf * @throws IOException */ public void write(byte[] newBuf) throws IOException { write(newBuf,0,newBuf.length); } /** * 把指定的缓冲区的数据写到文件中 * @param newBuf * @param offset * @param length * @throws IOException */ public void write(byte[] newBuf,int offset,int length) throws IOException { FileOutputStream fout = new FileOutputStream(file); fout.write(newBuf,offset,length); fout.close(); modified = true; isSaved = true; } /** * 取得文件的正常显示字串 */ public String getNormalDisplay() throws IOException { if (reload() || normalString == null) { if (javaEncode != null) { normalString = new String(fileBuffer,javaEncode); } else { normalString = new String(fileBuffer); } } isHexMode = false; return normalString; } /** * 取得文件的十六进制显示字串 */ public String getHexDisplay() throws IOException { if (reload() || hexString == null) { getHexString(); } isHexMode = true; return hexString; } private static char [] hexCharArray = {'0','1','2','3', '4','5','6','7', '8','9','A','B', 'C','D','E','F'}; public static String byteToHexString(byte data) { int high = (data & 0xF0) >> 4; int low = data & 0x0F; return hexCharArray[high] + "" + hexCharArray[low]; } /** * 取得文件的十六进制串 */ private void getHexString() { StringBuffer sb = new StringBuffer(); int lineNumber = 0; sb.append(fillZeroForNumber(lineNumber++ * byteNumberPerLine) + addressSeperator); StringBuffer chars = new StringBuffer(); for (int j = 0; j < fileBuffer.length; j++) { byte b = fileBuffer[j]; sb.append(byteToHexString(b)).append("/t"); if ((char)b == '/n') { chars.append("//n"); } else { chars.append((char)b); } if (j % byteNumberPerLine == byteNumberPerLine - 1) { sb.append(hexValueSeperator + chars); sb.append("/n"); sb.append(fillZeroForNumber(lineNumber++ * byteNumberPerLine) + addressSeperator); chars.delete(0,chars.length()); } } hexString = sb.toString(); } /** * 更改文件编码方式 * @see cross.pauliuyou.io.FileInfor#changeEncode() */ //@Override public void changeEncode() { if (encode.equals("BINARY")) { return; } javaEncode = toEncode; toEncode = null; for (int i = 0; i < Encoding.javaname.length; i++) { if (javaEncode.equals(Encoding.javaname[i])) { encode = Encoding.nicename[i]; break; } } getFileProperty(); } /** 取得文件属性描述字串 * @see cross.pauliuyou.io.FileInfor#getPropertyString() */ //@Override public String getPropertyString() { getFileProperty(); return propertyString; } /** * 把十六进制文本转换为十六进制数据写到文件中 * @see cross.pauliuyou.io.FileInfor#writeHexByString(java.lang.String) */ //@Override public void writeHexByString(String hexString) throws IOException { if (hexString == null || hexString.trim().equals("")) { return; } String [] lines = hexString.trim().split("/n"); byte[] tmp = new byte[byteNumberPerLine]; byte[] buf = new byte[lines.length * 10]; int offset = 0; for (int i = 0; i < lines.length - 1; i++) { hexLineToByteArray(lines[i].trim(),true,tmp); for (int j = 0; j < byteNumberPerLine; j++) { buf[j + offset] = tmp[j]; } offset += byteNumberPerLine; } String lastLine = lines[lines.length - 1].trim(); int realCount = hexLineToByteArray(lastLine,false,tmp); if (realCount > 0) { for (int j = 0; j < realCount; j++) { buf[j + offset++] = tmp[j]; } } write(buf,0,offset); } } /// /** * */ package cross.pauliuyou.io; import java.io.File; import java.io.IOException; /** * @author Administrator * */ public class EmptyInfor extends FileInfor { public EmptyInfor(String fileName) throws IOException { super(fileName); } /** * @param file * @throws IOException */ public EmptyInfor(File file) throws IOException { super(file); } /* (non-Javadoc) * @see cross.pauliuyou.io.FileInfor#changeEncode() */ //@Override public void changeEncode() { // TODO Auto-generated method stub } /* (non-Javadoc) * @see cross.pauliuyou.io.FileInfor#getEncode() */ //@Override public String getEncode() { // TODO Auto-generated method stub return null; } /* (non-Javadoc) * @see cross.pauliuyou.io.FileInfor#getHexDisplay() */ //@Override public String getHexDisplay() throws IOException { // TODO Auto-generated method stub return null; } /* (non-Javadoc) * @see cross.pauliuyou.io.FileInfor#getNormalDisplay() */ //@Override public String getNormalDisplay() throws IOException { // TODO Auto-generated method stub return null; } /* (non-Javadoc) * @see cross.pauliuyou.io.FileInfor#read() */ //@Override public String read() throws IOException { // TODO Auto-generated method stub return null; } /* (non-Javadoc) * @see cross.pauliuyou.io.FileInfor#read(java.lang.String) */ //@Override public String read(String encode) throws IOException { // TODO Auto-generated method stub return null; } /* (non-Javadoc) * @see cross.pauliuyou.io.FileInfor#write(java.lang.String) */ //@Override public void write(String content) throws IOException { // TODO Auto-generated method stub } /* (non-Javadoc) * @see cross.pauliuyou.io.FileInfor#writeHexByString(java.lang.String) */ //@Override public void writeHexByString(String hexString) throws IOException { // TODO Auto-generated method stub } }