简体   繁体   English

Java从.bin文件转换为.txt加载

[英]Java Convert from .bin file to .txt loading

How would I go about changing this from loading .bin to .txt. 我该如何将其从加载.bin更改为.txt。 I would like the information on the .txt file be put into the hashmap just like .bin file. 我希望将.txt文件上的信息像.bin文件一样放入哈希图中。

    public static int currency;
    public HashMap<Integer, Shop> cars = new HashMap<Integer, Car>();    
public void load() {
            try {
                @SuppressWarnings("resource")
                RandomAccessFile carFile = new RandomAccessFile("data/cars.bin", "r");
                int carsAmt = carFile.readShort();
                for (int carId = 0; carId < carAmt; carId++) {
                    int carId = carFile.readShort();
                    int currency = carFile.readShort();
                    int[] cars = new int[carFile.readByte()];
                    int[] amounts = new int[cars.length];
                    boolean isTruck = carFile.read() == 1;
                    for (int carData = 0; carData < cars.length; carData++) {
                        cars[carData] = carFile.readShort();
                        amounts[carData] = carFile.readInt();
                    }
                    cars.put(carId, new Car(carId, currency, isTruck, cars, amounts, true));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("Loaded " + cars.size() + " Cars");
        }

bin and txt are just parts of the name. bintxt只是名称的一部分。 However, by convention, a txt file contains data formatted using one of many character encodings . 但是,按照惯例, txt文件包含使用许多字符编码之一格式化的数据。 Common encodings are UTF-8 and the extensions of ASCII. 常见的编码是UTF-8和ASCII扩展名。 An "encoding" is just a mapping of a sequence of bytes to a character. “编码”只是字节序列到字符的映射。 For example, in UTF-8, a maps to the 8 bit sequence 01100001 ( 61 in hexadecimal). 例如,在UTF-8中, a映射到8位序列01100001 (十六进制为61 )。 Realistically, all files are stored as a series of 0 s and 1 s, but txt files usually only store sequences according to their encoding. 实际上,所有文件都以0 s和1 s的序列存储,但是txt文件通常仅根据其编码存储序列。 By contrast, a bin file would contain data stored in a "binary" format; 相比之下, bin文件将包含以“二进制”格式存储的数据。 that is, the data in them does not match any kind of text encoding. 也就是说,它们中的数据与任何类型的文本编码都不匹配。 In your case, the bin file just contains a set of numbers stored as their binary representations (which is a sequence of bits or 0s and 1s). 在您的情况下, bin文件仅包含一组以其二进制表示形式存储的数字(它是位或0和1的序列)。

The answer to your question is that you're going to have to rewrite this block of code. 问题的答案是,您将不得不重写此代码块。 Since you now want to read a text file instead of using the binary values directly, you'll need a reader that can decode your txt file into String s, and then you'll also need to turn those String s into numeric values. 由于您现在要读取文本文件而不是直接使用二进制值,因此需要一个可以将txt文件解码为String的阅读器,然后还需要将这些String转换为数字值。 (This step is called "parsing".) (此步骤称为“解析”。)

You will need to know the encoding of your txt file to be able to read it correctly. 需要知道txt文件的编码,以便能够正确读取它。 RandomAccessFile has a method to read UTF-8 encoding text, but if it's in another encoding, you'll need to look elsewhere. RandomAccessFile有一种读取UTF-8编码文本的方法,但是如果它是另一种编码,则需要在其他地方查找。 See this answer for some information on using BufferedReader . 有关使用BufferedReader一些信息,请参见此答案 You may also find the Scanner class useful. 您可能还会发现Scanner类很有用。 Scanner can parse the values for you as it reads them in. Scanner可以在读取值时为您解析这些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM