如何用java读取文件



我制作了这个迷宫求解程序,除了"文件阅读器"之外,它所有的工作都正常。我打算做的是,这个文件有一个迷宫地图,其中1是墙,0是你可以通过的通道,3是起点,9是终点。我的学校老师告诉我,我不能手动将地图输入到我的程序中,我必须通过一个函数调用文件阅读器将地图文件读取到我的程序中。问题是他从没教过我怎么看文件。我在互联网上搜索了很长时间,却找不到任何有用的东西。我给大家展示一下我的程序和我所做的。在代码中,我写出了我在互联网上找到的关于文件读取的内容,正如您可能知道的那样,它不起作用。我创建的文件读取器函数位于代码的最后。看看你能不能帮我。:)

我试图读取的文件被称为maze1.txt,它有这个:10123 1 1 0 0 1 1 1 1 1 1 1 111,11,11,11,11,111,11,11,11,11,11 0 0 1 1 1 1 1 1 1 1 11 0 1 1 1 1 1 1 0 1 1 1 11 0 1 1 1 1 1 1 1 1 1 0 11 0 0 1 1 1 1 1 1 1 1 111,11,11,11,11,11 1 1 1 0 0 0 0 1 1 1 11 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();            
    }

相关内容

  • 没有找到相关文章

最新更新