【问题标题】:seek method RandomAccessFile寻找方法 RandomAccessFile
【发布时间】:2015-04-21 19:16:32
【问题描述】:

我有一个任务,我需要显示 200 个随机字符,然后询问用户他们想替换什么字母,然后替换所有这些字母。我生成了随机字符,但在替换字母时遇到了麻烦。有人可以帮助我朝着正确的方向前进吗?以下是我的其他一些问题:

  • 我是否对 seek 方法使用了 for 循环,以便它可以找到所有这些字母?
  • 我还需要显示每个字母的位置。我会使用文件指针吗?我也会把它放在一个循环中吗?

这是我的代码:

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() 循环来解决这个问题。
  • 刚刚包含我的代码
  • 是什么阻止您将整个文件读入字符串,用正则表达式解析它,然后将其存储到文件中?
  • 我在执行 Integer.ParseInt() 时不断收到错误消息。当 seek() 用于 int 时,我如何找到所有字母? @user3360241 抱歉,我好像问了很多,但我从决赛中精疲力尽,我什至无法思考。

标签: java seek randomaccessfile file-pointer


【解决方案1】:

尝试将 for-loop (j

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(位置)。它会在两个中寻找,因为我使用了 char 并且那是两个字节。
  • 我将解决方案更改为不逐字节遍历文件,因为不同的编码可能对字符使用不同的字节大小(Java 内部表示不一定等于文件表示)。我建议您使用某种 IDE(Eclipse 或 IntelliJ)并逐步执行此代码以确保它逐个读取字符,并正确比较它们。
  • 你的代码成功了!我必须修复其中的一些错误,但之后我的程序运行良好!非常感谢
猜你喜欢
  • 1970-01-01
  • 2011-09-24
  • 1970-01-01
  • 1970-01-01
  • 2011-03-09
  • 2016-06-17
  • 2021-10-17
  • 2021-02-12
  • 2012-10-13
相关资源
最近更新 更多