【问题标题】:need serversocket to read a text file into 2d array character by character需要serversocket逐个字符地将文本文件读入二维数组
【发布时间】:2015-03-26 23:52:50
【问题描述】:

我必须让我的套接字将 80x80 文本文件读入 80x80 二维数组。它必须是逐个字符的,并且结果数组必须是文本文件布局的副本,因为它将用于在迷宫中移动鼠标。我尝试使用 readNext 但我意识到它的返回类型是 int 并且 nextLine 会将整行记录到一个索引中。我卡住了,我发现的所有其他帖子都在将单词或整数读入数组。如果您有时间,套接字的协议将是:

     ooo
     wmw
     wpw

所以基本上 o=迷宫外,m=鼠标,w=墙,p=路径 服务器需要在鼠标每次移动后发送鼠标在 80x80 迷宫中位置的 3x3 快照。我很难解决这个问题。所以无论如何这里是我到目前为止得到的服务器套接字的代码。想象一下一个 80x80 字符的文本文件,它以图形方式表示迷宫的路线,因为我无法真正复制和粘贴它。我在代码中只需要使用什么来填充数组以及来回发送协议的想法。我可以让套接字稍后进行通信

public class serverSocket {


private static ServerSocket server = null;
public static void main (System [] args){

    File file = new File("\\MAC\\Users\\Tucker\\mazehardcode.rtf");
    FileReader fr = new FileReader(file);
    char mazeFile [][]=new char[80][80];
    for(int i = 0;i <80;i++){
        for(int j = 0; j<80; j++){
            mazeFile[i][j]= fr.;
        }
    }
}
}

【问题讨论】:

    标签: java arrays sockets text protocols


    【解决方案1】:

    希望我能正确理解您的问题。顺便说一句,您的 rtf 文件是纯文本吗?

        FileReader fr = null;
        try { 
            fr = new FileReader(new   File("\\MAC\\Users\\Tucker\\mazehardcode.rtf"));
            char[][] mazeFile = new char[80][80];
            for (int i = 0; i < 80; i ++) {
                fr.read(mazeFile[i]);
    
                // remove code below, just to verify read correctly
                for (char c : mazeFile[i]) {
                    System.out.println(c);
                }
    
                // skip the \n, you have to skip 2 if in windows due to \r\n
                fr.skip(1);
            }
    
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2012-11-19
      • 2020-07-08
      • 2016-01-17
      • 2013-03-11
      • 2020-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多