如何读取java中的文件?
How to read files in java?
我制作了这个迷宫解算器程序,除了 "file reader" 之外,它都可以工作。我打算做的是该文件有一个迷宫地图,1 作为墙,0 作为通行证,3 是起点,9 是终点线。我的学校老师告诉我,我不能手动将地图输入我的程序,我必须通过函数调用文件 reader 将地图文件读入我的程序。问题是他从来没有教过我如何阅读文件。我已经在互联网上搜索了很长时间,但找不到任何实际有效的方法。到目前为止,我会向你们展示我的程序以及我所做的。
在代码中,我写下了我在 Internet 上找到的有关文件读取的内容,您可能知道它不起作用。我创建的文件 reader 函数位于代码的最后。看看你能不能帮我。 :)
我正在尝试读取的文件名为 maze1.txt,其中包含:
10
12
3 1 1 0 0 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 0 1 1 1 1 1 1 0 1 1 1
1 0 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 1 1 1 1 1 1 1
1 1 1 1 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();
}
我制作了这个迷宫解算器程序,除了 "file reader" 之外,它都可以工作。我打算做的是该文件有一个迷宫地图,1 作为墙,0 作为通行证,3 是起点,9 是终点线。我的学校老师告诉我,我不能手动将地图输入我的程序,我必须通过函数调用文件 reader 将地图文件读入我的程序。问题是他从来没有教过我如何阅读文件。我已经在互联网上搜索了很长时间,但找不到任何实际有效的方法。到目前为止,我会向你们展示我的程序以及我所做的。 在代码中,我写下了我在 Internet 上找到的有关文件读取的内容,您可能知道它不起作用。我创建的文件 reader 函数位于代码的最后。看看你能不能帮我。 :)
我正在尝试读取的文件名为 maze1.txt,其中包含: 10 12 3 1 1 0 0 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 0 1 1 1 1 1 1 0 1 1 1 1 0 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 1 1 1 1 1 1 1 1 1 1 1 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();
}