繁体   English   中英

如何在Java中读取文件?

[英]How to read files in java?

我制作了这个迷宫求解器程序,除“文件阅读器”外,所有程序均有效。 我打算做的是,该文件具有一个迷宫贴图,其中1表示墙,0表示可以通过,3表示开始,9表示终点。 我的学校老师告诉我,我无法手动将地图输入程序中,而我必须通过函数调用文件读取器将地图文件读入程序中。 事实是,他从未教过我如何读取文件。 我已经在互联网上搜索了很长时间,找不到任何有效的方法。 到目前为止,我将向我展示我的程序。 在代码中,我写出了我在Internet上发现的有关文件读取的内容,并且您可能知道它不起作用。 我所做的文件阅读器功能在代码的最后。 看看你能不能帮我。 :)

我尝试读取的文件称为maze1.txt,它包含以下内容:10 12 3 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1

这是我的程序:

import java.awt.event.*;
import java.util.Scanner;
import java.awt.*;
import java.io.FileReader;
import java.io.FileNotFoundException;

import javax.swing.*;
public class Mazegraphic extends JPanel implements  ActionListener,KeyListener{
static char comefrom;
static int width=1000,height=1000;  
static int originalrow;
static int originalcolumn;
int row,column;
static int[][] originalmaze;

static int[][] Maze= new int [100][100];

static int x=0,y=0;
static int xx=0, yy=0;
static int xend=0, yend=0;
static boolean passagepoint = true;
static boolean playmaze = true;
static boolean gogogo= false;
static JFrame f; 

public void init (){
    this.addKeyListener(this);  
    setFocusable(true);  
//  input();


    row= originalrow+2;
    column= originalcolumn+2;
    comefrom = 'n';

    // add imaginary wall
    for (int a=0;a<row; a++){
        Maze[a][0] = 1;
        Maze[a][column-1] = 1;
    }
    for (int b=0; b<column; b++){
        Maze[0][b] =1;
        Maze[row-1][b] = 1;
    }
    for (int a=0;a<originalrow; a++){ 
        for (int b=0; b<originalcolumn; b++){
            Maze[a+1][b+1]=originalmaze[a][b];
            }
        }



    for (int a=0;a<row; a++){ //find starting point
        for (int b=0; b<column; b++){
            if (Maze[a][b]==3){
                x=a;y=b;

            }
        }
    }

    for (int a=0;a<row; a++){ //find ending point
        for (int b=0; b<column; b++){
            if (Maze[a][b]==9){
                xend=a;yend=b;
            }
        }
    }
    if (xend==0 && yend==0) playmaze = false;
}
public void paintComponent (Graphics g){
    int i=0,j=0;
    for (i=0;i<originalrow;i++){

        for (j=0;j<originalcolumn;j++){
            if (Maze[i+1][j+1]==1){
                g.setColor(Color.blue);
                g.fillRect(j*30+30,i*30+30,30,30);
            }
            if (Maze[i+1][j+1]==0){
                g.setColor(Color.white);
                g.fillRect(j*30+30,i*30+30, 30, 30);
            }
            if (Maze[i+1][j+1]==3){
                g.setColor(Color.yellow);
                g.fillRect(j*30+30,i*30+30, 30, 30);
            }
            if (Maze[i+1][j+1]==9){
                g.setColor(Color.green);
                g.fillRect(j*30+30,i*30+30, 30, 30);
            }
            if (Maze[i+1][j+1]==6){
                g.setColor(Color.yellow);
                g.fillRect(j*30+30,i*30+30, 30, 30);
            }

        }


        if (playmaze==false){
            if (xend==0 && yend==0){
            g.setColor(Color.red);
            g.setFont(new Font("Rosewood Std Regular", Font.PLAIN, 100)); 
            g.drawString("Can not reach finish",150,750);
            g.drawString("point!!!",500,900);
            }
            else {
                g.setColor(Color.green);
                g.setFont(new Font("Rosewood Std Regular", Font.PLAIN, 100)); 
                g.drawString("You reach finish point!!!",50,750);
            }
        }

    }

}
public static void main(String [] args){
    Mazegraphic m = new Mazegraphic();
    f=new JFrame();
    m.init(); 
    f.setBackground(Color.white);    
    f.add(m);
    f.setSize(width+1000,height+500);
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.repaint();  
    Timer t=new Timer(20,m);       
    t.start();
}


public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}


public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    if (e.getKeyCode()==KeyEvent.VK_SPACE){
        gogogo=true;
    repaint();
    }
    repaint();
}


public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}


public void actionPerformed(ActionEvent e) {

    if (gogogo==true && playmaze== true) {


        while (Maze[x][y]!=9) {
            playmaze = true;


            if ((comefrom=='n')&&(Maze[x][y-1]!=1)){
                comefrom='e';
                y=y-1;
                if (Maze[x][y]==9) {
                    playmaze=false;
                    break;
                }
                Maze[x][y]=6;

            }
            if (comefrom=='w'&&Maze[x+1][y]!=1){
                comefrom='n';
                x=x+1;
                if (Maze[x][y]==9) {
                    playmaze=false;
                    break;
                }
                Maze[x][y]=6;

            }
            if (comefrom=='s'&&Maze[x][y+1]!=1){
                comefrom='w';
                y=y+1;
                if (Maze[x][y]==9) {
                    playmaze=false;
                    break;
                }
                Maze[x][y]=6;

            }
            if (comefrom=='e'&&Maze[x-1][y]!=1){
                comefrom='s';
                x=x-1;
                if (Maze[x][y]==9) {
                    playmaze=false;
                    break;
                }
                Maze[x][y]=6;

            }
            if (checknext()==false){ // change direction
                switch (comefrom) {
                case 's':comefrom='e';
                         break;
                case 'w':comefrom='s';
                        break;
                case 'n':comefrom='w';
                        break;
                case 'e':comefrom='n';
                        break;
                }
            /*  if (comefrom=='s'){
                    comefrom='e';
                }
                if (comefrom=='w'){
                    comefrom='s';
                }
                if (comefrom=='n'){
                    comefrom='w';
                }
                if (comefrom=='e'){
                    comefrom='n';
                }*/
            }
            else {
                if (Maze[x][y]==9) {
                    Maze[x][y]=6;
                    playmaze=false;
                    break;      
                    }
                else {
                        Maze[x][y]=6;
                        playmaze = true;
                }


                /*          for (int i=0;i<Maze.length; i++){

                for (int j=0; j<Maze[i].length; j++){

                    System.out.print( Maze[i][j] );

                }

                System.out.println();

            }

            System.out.println( );
                 */

                repaint();
            }
        }
        playmaze =false;

        repaint();
    }
}
public static boolean checknext(){
    if (comefrom=='n') {
        if(Maze[x][y-1]==1){
            if (Maze[x+1][y]!=1){
                x=x+1;
                return true;
            }
            else {
                return false;
            }
        }
    }
    if (comefrom=='e') {
        if(Maze[x-1][y]==1){
            if (Maze[x][y-1]!=1){
                y=y-1;
                return true;
            }
            else {
                return false;
            }
        }
    }
    if (comefrom=='s') {
        if(Maze[x][y+1]==1){
            if (Maze[x-1][y]!=1){
                x=x-1;
                return true;
            }
            else {
                return false;
            }
        }
    }
    if (comefrom=='w') {
        if(Maze[x+1][y]==1){
            if (Maze[x][y+1]!=1){
                y=y+1;
                return true;
            }
            else {
                return false;
            }
        }
    }
    return false;
}

public static void input(){

    try{
        FileReader Afr=new FileReader("maze1.txt"); //this is my file reader and it is not working
        Scanner input = new Scanner(Afr);
        originalrow = input.nextInt();
        originalcolumn = input.nextInt();
        originalmaze = new int [originalrow][originalcolumn];
        for (int a=0; a<originalrow;a++){
            for(int b=0;b<originalcolumn;b++){
                originalmaze[a][b]=input.nextInt();
            }
        }
    }
    catch(FileNotFoundException e){
        System.err.println("file not found");
    }
}

}

这是一个完美的示例,您应该可以以此为基础。 这是一个文件的读取行。 如果您希望将它们全部在一起,则可以将它们一个接一个地添加到字符串数组中,也可以根据需要添加它们。 FileReader会逐字符读取文件,因此通常将其包装在BufferedFileReader中,以便您可以逐行处理,也可以根据需要进行处理

// The name of the file to open.
    String fileName = "temp.txt";

    // This will reference one line at a time
    String line = null;

    try {
        // FileReader reads text files in the default encoding.
        FileReader fileReader = 
            new FileReader(fileName);

        // Always wrap FileReader in BufferedReader.
        BufferedReader bufferedReader = 
            new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }    

        // Always close files.
        bufferedReader.close();            
    }

暂无
暂无

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

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