什么是Flat File

"Flat"which means it has no structure for indexing and there are usually no structural relationships between the records. 

一个flat file即可以是纯文本文件(plain text file),也可以是二进制文件(binary file)。


纯文本文件

plain text is the data (e.g. file contents) which represent only characters of readable material but not its graphical representation nor other objects (images, etc.). It may also include a limited number of characters that control simple arrangement of text, such as line breaks or tabulation characters. 

The encoding has traditionally been either ASCII, sometimes EBCDIC. Unicode-based encodings such as UTF-8 and UTF-16 are gradually replacing the older ASCII derivatives limited to 7 or 8 bit codes.


二进制文件

Binary files are usually thought of as being a sequence of bytes, which means the binary digits (bits) are grouped in eights. Binary files typically contain bytes that are intended to be interpreted as something other than text characters. Compiled computer programs are typical examples; indeed, compiled applications are sometimes referred to, particularly by programmers, as binaries. But binary files can also mean that they contain images, sounds, compressed versions of other files, etc. — in short, any type of file content whatsoever.
Some binary files contain headers, blocks of metadata used by a computer program to interpret the data in the file. The header often contains a signature or magic number which can identify the format. For example, a GIF file can contain multiple images, and headers are used to identify and describe each block of image data. The leading bytes of the header would contain text like GIF87a or GIF89a that can identify the binary as a GIF file. If a binary file does not contain any headers, it may be called a flat binary file.

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的Java代码实现,具体实现可以根据需求进行修改: ``` import java.io.*; public class DBTest { RandomAccessFile file; int recordSize = 100; // 一个记录的大小 int numRecords; // 记录的数量 public DBTest() throws IOException { file = new RandomAccessFile("products.db", "rw"); // 打开文件 numRecords = (int) (file.length() / recordSize); // 计算记录的数量 } // 添加一条记录 public void addRecord(String name) throws IOException { if (name.length() > 50) { name = name.substring(0, 50); // 只取前50个字符 } StringBuffer buffer = new StringBuffer(name); buffer.setLength(50); // 填充到50个字符 file.seek(file.length()); // 定位到文件末尾 file.writeChars(buffer.toString()); // 写入记录 numRecords++; // 记录数量加1 } // 获取一条记录 public String getRecord(int index) throws IOException { if (index < 0 || index >= numRecords) { return null; } file.seek(index * recordSize); // 计算记录的位置 StringBuffer buffer = new StringBuffer(); for (int i = 0; i < 50; i++) { char c = file.readChar(); // 读取字符 if (c != '\0') { buffer.append(c); // 添加到字符串中 } } return buffer.toString().trim(); // 去掉多余的空格 } // 关闭文件 public void close() throws IOException { file.close(); } public static void main(String[] args) throws IOException { DBTest db = new DBTest(); db.addRecord("Product A"); db.addRecord("Product B"); db.addRecord("Product C"); System.out.println(db.getRecord(0)); // 输出第一条记录 db.close(); } } ``` 上述代码实现了一个简单的产品数据库程序,使用RandomAccessFile类和一个平面文件来存储和检索记录。每个记录由一个字符串名称组成,最多可以包含50个字符。程序包含了添加记录、获取记录和关闭文件的方法。在程序的main方法中,我们向数据库中添加了三条记录,并输出了第一条记录的名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值