簡體   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