0

我正在尝试使用 RandomAccessFile 写入现有文件,但是对 writer.writeUTF() 的调用会用不可打印字符或换行符覆盖写入偏移之前的两个字符。我真的不知道是什么导致了这个问题,并且我进行了多次搜索,但没有任何结果。

        File mapObjects = new File(args[0] + "/data/maps/objects");
        ArrayList<String> warpNames = new ArrayList<String>();
        ArrayList<Integer> offsets = new ArrayList<Integer>();
        for (File mapObject : mapObjects.listFiles()) {
            try {
                Scanner reader = new Scanner(mapObject);
                int offset = 0;
                String ln = new String();
                System.out.println(mapObject.getPath());
                while (!ln.contains("def_warps_to")) { // will loop until it finds the definition of the warp name
                    offset += ln.length();
                    ln = reader.nextLine();
                }
                offset += 28;
                warpNames.add(ln.substring(14)); // adds the "warps_to" token to the list
                offsets.add(offset);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        Collections.shuffle(warpNames); // randomize
        for (String s : warpNames)
            System.out.println(s);
        int i = 0; // iterator of warpNames and offsets
        for (File mapObject : mapObjects.listFiles()) {
            try {
                RandomAccessFile writer = new RandomAccessFile(mapObject, "rw");
                writer.seek(offsets.get(i));
                writer.writeUTF(warpNames.get(i)); // overwrites "warps_to" token with randomized one
                i++;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
4

1 回答 1

0

我修好了,我所要做的就是打电话

writer.writeBytes(warpNames.get(i))

代替

writer.writeUTF(warpNames.get(i))
于 2021-11-08T00:42:04.063 回答