分享一个文件编辑器

pauliuyou pauliuyou 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
文件信息封装对象


/**
*
*/
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;

}

...全文

相关推荐

评论 27
收藏 已收藏
分享
返回顶部
pauliuyou
Java SE 62,620 成员 307,251 内容