寻找方法 RandomAccessFile

3
我有一个任务,需要显示200个随机字符,然后询问用户要替换哪个字母,并替换所有这些字母。我已经生成了随机字符,但在替换字符方面遇到了麻烦。有人能帮忙指导一下吗?以下是我还有的几个问题:
  • 我是否需要使用for循环来查找所有这些字母的位置?
  • 我还需要显示每个字母的位置。我是否应该使用filepointer?我是否也需要将它放在一个循环中?

以下是我的代码:

import java.io.*;
import java.util.Scanner;

public class Alphabet {
    public static char getRandomCharacter(char ch1, char ch2) {
        return (char) (ch1 + Math.random() * (ch2 - ch1 + 1));
    }

    public static char getRandomUpperCaseLetter() {
        return getRandomCharacter('A', 'Z');
    }

    public static void main(String[] args) throws IOException {

        try (RandomAccessFile raf = new RandomAccessFile("Alphabet.dat", "rw")) {
            raf.setLength(0);

            for (int row = 0; row < 10; ++row){
                for (int col = 0; col < 10; ++col) {
                    raf.writeChar(getRandomUpperCaseLetter());
                }
            }

            raf.seek(0);
            for (int row = 0; row < 10; ++row){
                for (int col = 0; col < 10; ++col) {
                    System.out.print(raf.readChar() +" ");
                }
                System.out.println();
            }

            Scanner Console = new Scanner(System.in);
            System.out.println("Current length of file is: "
                    + raf.length());
            System.out.print("Replace Characters: ");
            String letter = Console.next();
            System.out.print("With Character: ");
            String ch = Console.next();

                for(int j = 0; j < raf.length(); ++j){
                    raf.seek((raf.length()-1)*2);
                    raf.writeChars(ch);
                    System.out.print("Position" + raf.getFilePointer());
                }

            raf.writeChars(ch);
            raf.seek(0);
            for (int row = 0; row < 10; ++row){
                for (int col = 0; col < 10; ++col) {
                }
                System.out.println();
            }
        }
    }
}

你有尝试过任何代码吗?如果有的话,能否发布一下?我会使用 while() 循环来完成这个任务。 - oakTree
只包含我的代码 - Masha Mcintyre
什么阻止你将整个文件读入字符串,用正则表达式进行解析,然后将其存储到文件中呢? - John
当我执行Integer.ParseInt()时,我一直收到错误提示。当seek()是int类型时,我该如何找到所有字母?@user3360241 对不起,看起来我要求太多了,但我已经因为期末考试而精疲力尽,甚至无法思考。 - Masha Mcintyre
1个回答

1
尝试使用以下内容的while循环替换for循环(j < raf.length):
long currPointer = 0;
while(currPointer < raf.length()) {
  long currPointer = raf.getFilePointer(); // save current cursor position
  char currentChar = raf.readChar(); // read current char

  if (currentChar == letter) { // if char equals that to be replaced
     raf.seek(currPointer); // step cursor one step back
     raf.writeChar(ch); // replace char
  }

  currPointer = raf.getFilePointer() // store the position of the cursor 

}
编辑:现在该文件是按字符遍历,而不是逐字节遍历。鉴于各种字符编码可能不使用恒定数量的字节表示每个字符,这是最简单的方法。

基本上:

LOOP through all characters in file
    IF current character equals that to be replaced
         step cursor back by one (otherwise you'd be overwriting the next character)
         replace character

仅出于好奇,你究竟想通过这个实现什么:

raf.seek((raf.length()-1)*2);

我尝试让用户输入一个奇数。在此之前,我使用了raf.seek(position)。由于我使用了char,而char是两个字节,所以它会自动跳过 every other position. - Masha Mcintyre
我修改了解决方案,不再逐字节遍历文件,因为不同的编码可能对字符使用不同的字节大小(Java内部表示不一定等于文件表示)。我建议您使用某种集成开发环境(如Eclipse或IntelliJ)并逐步执行此代码,以确保它逐个字符地读取并正确比较它们。 - Robin Nabel
你的代码成功了!我不得不修复一些错误,但之后我的程序运行得非常好!非常感谢。 - Masha Mcintyre

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接