分享一个文件编辑器
文件信息封装对象
/**
*
*/
package cross.pauliuyou.io;
import java.io.File;
import java.io.IOException;
/**
* @author 刘优
*
*/
public abstract class FileInfor
{
public static final int byteNumberPerLine = 10;
public static final int addressStringWidth = 12;
public static final String addressSeperator = " :\t";
public static final String hexValueSeperator = " ;\t";
/**
* 文件对象
*/
protected File file;
/**
* 文件名称
*/
protected String name;
/**
* 文件路径
*/
protected String path;
/**
* 是否是新创建的文件
*/
protected boolean newCreatedFile;
/**
* 文件长度
*/
protected long length;
/**
* 长度描述
*/
protected String lengthString;
/**
* 是否是二进制文件
*/
protected boolean isBinary;
/**
* 文件修改标志
*/
protected boolean modified;
/**
* 文件十六进制模式
*/
protected boolean isHexMode;
/**
* 文件属性描述串
*/
protected String propertyString;
/**
* 文件是否被保存
*/
protected boolean isSaved;
/**
* 文件是否被更改
*/
protected boolean isDirty;
/**
* 用文件名构造对象
* @param fileName 文件名
* @throws IOException
*/
public FileInfor(String fileName) throws IOException
{
this.name = fileName;
this.path = fileName;
}
/**
* 用文件构造对象
* @param file 文件对象
* @throws IOException
*/
public FileInfor(File file) throws IOException
{
this.file = file;
this.name = file.getName();
this.path = file.getAbsolutePath();
}
public File getFile()
{
return file;
}
public String getName()
{
return name;
}
public abstract String getEncode();
public boolean isBinary()
{
return isBinary;
}
public String getPath()
{
return path;
}
public void setModified(boolean modified)
{
this.modified = modified;
}
public boolean isModified()
{
return modified;
}
public String getPropertyString()
{
return propertyString;
}
/**
* @return the isHexMode
*/
public boolean isHexMode()
{
return isHexMode;
}
/**
* @param isHexMode the isHexMode to set
*/
public void setHexMode(boolean isHexMode)
{
this.isHexMode = isHexMode;
}
/**
* @return the isSaved
*/
public boolean isSaved()
{
return isSaved;
}
public boolean isDirty()
{
return isDirty;
}
public void setDirty(boolean isDirty)
{
this.isDirty = isDirty;
}
/**
* @return the length
*/
public long getLength()
{
return length;
}
/**
* @return the lengthString
*/
public String getLengthString()
{
return lengthString;
}
/**
* @return the newCreatedFile
*/
public boolean isNewCreatedFile()
{
return newCreatedFile;
}
public static String getSpaces(int count)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < count; i++)
{
sb.append(' ');
}
return sb.toString();
}
public static String fillZeroForNumber(int number)
{
return fillZeroForNumber(number,addressStringWidth);
}
public static String fillZeroForNumber(int number, int width)
{
return fillZeroForNumber(number + "",width);
}
/**
* @param number
* @param width
* @return
*/
public static String fillZeroForNumber(String numberStr, int width)
{
StringBuffer sb = new StringBuffer();
int fillZeros = width - numberStr.length();
for (int i = 0; i < fillZeros; i++)
{
sb.append("0");
}
return sb.append(numberStr).toString();
}
private static int hexCharToInt(int charInput) throws IOException
{
if (charInput >= '0' && charInput <= '9')
{
charInput -= '0';
}
else if (charInput >= 'A' && charInput <= 'F')
{
charInput = charInput - 'A' + 10;
}
else
{
throw new IOException("十六进制数据 [" + (char)charInput + "] 错误.");
}
return charInput;
}
public static byte hexToByte(String hex) throws IOException
{
hex = hex.toUpperCase();
if (hex.length() == 1)
{
hex = "0" + hex;
}
int value0 = hexCharToInt(hex.charAt(0));
int value1 = hexCharToInt(hex.charAt(1));
return (byte)(value0 << 4 | value1);
}
/**
* @param string
* @throws IOException
*/
public static int hexLineToByteArray(String line,byte[] buf) throws IOException
{
return hexLineToByteArray(line,true,buf);
}
/**
* @param string
* @throws IOException
*/
public static int hexLineToByteArray(String line,boolean normal,byte[] buf) throws IOException
{
if (line == null || line.trim().equals(""))
{
throw new IOException("文件行为为空行");
}
int index1 = line.indexOf(addressSeperator);
int index2 = line.indexOf(hexValueSeperator);
if (index1 == -1)
{
throw new IOException("行数据格式有错误");
}
if (index2 == -1)
{
index2 = line.length();
}
line = line.substring(index1 + addressSeperator.length(),index2).trim();
if (line.length() == 0)
{
if (normal)
{
throw new IOException("每行必须包含" + byteNumberPerLine + "个二进制数据");
}
return 0;
}
String[] byteStrings = line.split("\t");
if (normal && byteStrings.length != byteNumberPerLine)
{
throw new IOException("每行必须包含" + byteNumberPerLine + "个二进制数据");
}
if (buf == null || buf.length < byteNumberPerLine)
{
throw new IOException("输入的缓冲区对象为空或太小");
}
for (int i = 0; i < byteStrings.length; i++)
{
buf[i] = hexToByte(byteStrings[i]);
}
return byteStrings.length;
}
/////////////////////////////////////////////////////////////////////////
public abstract String read() throws IOException;
public abstract String read(String encode) throws IOException;
public abstract void write(String content) throws IOException;
public abstract String getNormalDisplay() throws IOException;
public abstract String getHexDisplay() throws IOException;
public abstract void changeEncode();
public abstract void writeHexByString(String hexString) throws IOException;
}
...全文
相关推荐
BT种子编辑器.zip
**创建复合种子**:对于需要分享多个相关文件的场合,用户可以通过BT种子编辑器创建一个复合种子,使下载者一次就能获取所有内容。
NC程序编辑器
描述中的“NC刀路文件编辑器,好用分享”意味着这款编辑器特别注重于刀具路径的管理与编辑,提供了一个用户友好的界面,使得操作更加便捷。用户可以通过它来规划工件的加工路径,调整刀具参数,确保高精度和效率。
收藏一个经典编辑器
标题中的“收藏一个经典编辑器”暗示我们将讨论的是一款历史悠久且功能强大的文本编辑器,它在程序员和IT专业人员中享有盛誉。
vim编辑器使用手册分享.pdf
用户可以通过宏录制与回放来重复复杂的操作序列,多窗口编辑功能则可以在同一个文件的不同部分或不同文件之间自由切换。
百度编辑器uedtior在一个页面实例化多个以及怎么简化编辑器
在本篇文章中,我们将探讨如何在同一个页面上实例化多个百度编辑器,并分享一些简化编辑器配置与管理的方法。首先,实例化多个百度编辑器的关键在于为每个编辑器创建独立的ID和配置。


Java SE
