如何从 Java 中的 .txt 文件中读取字符串?

How can I read strings from a .txt file in Java?

如果我启动它,我的代码总是会产生异常。

package wrs;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class mainWRS{
static ArrayList<String> al1 = new ArrayList<String>();

static JFrame frame1 = new JFrame("Game");
static JPanel panel1 = new JPanel();
static JLabel label1 = new JLabel();

static JButton buttona = new JButton("a");
static JButton buttonb = new JButton("b");
static JButton buttonc = new JButton("c");
static JButton buttond = new JButton("d");
static JButton buttone = new JButton("e");
static JButton buttonf = new JButton("f");
static JButton buttong = new JButton("g");
static JButton buttonh = new JButton("h");
static JButton buttoni = new JButton("i");
static JButton buttonj = new JButton("j");
static JButton buttonk = new JButton("k");
static JButton buttonl = new JButton("l");
static JButton buttonm = new JButton("m");
static JButton buttonn = new JButton("n");
static JButton buttono = new JButton("o");
static JButton buttonp = new JButton("p");
static JButton buttonq = new JButton("q");
static JButton buttonr = new JButton("r");
static JButton buttons = new JButton("s");
static JButton buttont = new JButton("t");
static JButton buttonu = new JButton("u");
static JButton buttonv = new JButton("v");
static JButton buttonw = new JButton("w");
static JButton buttonx = new JButton("x");
static JButton buttony = new JButton("y");
static JButton buttonz = new JButton("z");

static Random rand = new Random();

static boolean end = false;

static String Word = "";

static mainWRS helper = new mainWRS();

static ArrayList<Integer> lp = new ArrayList<Integer>();

static ArrayList<String> used = new ArrayList<String>();

static char[] cw,cdisp;

static File file = new File("C:\Users\Jan\Desktop\eTest.txt");
static FileReader fr = new FileReader(file);
static BufferedReader br = new BufferedReader(fr);

public static void main(String[]args){                    // <-- line 70
    setup();
}

private static void setup() {
    frame1.setSize(500, 500);
    frame1.getContentPane().setBackground(Color.decode("#00FF00"));
    frame1.setLocation(0, 0);
    frame1.setVisible(true);
    frame1.setResizable(false);

    panel1.setLayout(null);
    panel1.setBackground(Color.decode("#00FF00"));

    label1.setSize(480, 50);
    label1.setLocation(10, 175);
    label1.setForeground(Color.BLACK);
    label1.setFont(label1.getFont().deriveFont(18f));

    int bpx = 5;
    int bpy = 220;

    buttona.setSize(50,50);
    buttona.setLocation(bpx, bpy);
    bpx += 55;
    buttonb.setSize(50,50);
    buttonb.setLocation(bpx, bpy);
    bpx += 55;
    buttonc.setSize(50,50);
    buttonc.setLocation(bpx, bpy);
    bpx += 55;
    buttond.setSize(50,50);
    buttond.setLocation(bpx, bpy);
    bpx += 55;
    buttone.setSize(50,50);
    buttone.setLocation(bpx, bpy);
    bpx += 55;
    buttonf.setSize(50,50);
    buttonf.setLocation(bpx, bpy);
    bpx += 55;
    buttong.setSize(50,50);
    buttong.setLocation(bpx, bpy);
    bpx += 55;
    buttonh.setSize(50,50);
    buttonh.setLocation(bpx, bpy);
    bpx += 55;
    buttoni.setSize(50,50);
    buttoni.setLocation(bpx, bpy);
    bpy += 55;
    bpx = 5;
    buttonj.setSize(50,50);
    buttonj.setLocation(bpx, bpy);
    bpx += 55;
    buttonk.setSize(50,50);
    buttonk.setLocation(bpx, bpy);
    bpx += 55;
    buttonl.setSize(50,50);
    buttonl.setLocation(bpx, bpy);
    bpx += 55;
    buttonm.setSize(50,50);
    buttonm.setLocation(bpx, bpy);
    bpx += 55;
    buttonn.setSize(50,50);
    buttonn.setLocation(bpx, bpy);
    bpx += 55;
    buttono.setSize(50,50);
    buttono.setLocation(bpx, bpy);
    bpx += 55;
    buttonp.setSize(50,50);
    buttonp.setLocation(bpx, bpy);
    bpx += 55;
    buttonq.setSize(50,50);
    buttonq.setLocation(bpx, bpy);
    bpx += 55;
    buttonr.setSize(50,50);
    buttonr.setLocation(bpx, bpy);
    bpy += 55;
    bpx = 30;
    buttons.setSize(50,50);
    buttons.setLocation(bpx, bpy);
    bpx += 55;
    buttont.setSize(50,50);
    buttont.setLocation(bpx, bpy);
    bpx += 55;
    buttonu.setSize(50,50);
    buttonu.setLocation(bpx, bpy);
    bpx += 55;
    buttonv.setSize(50,50);
    buttonv.setLocation(bpx, bpy);
    bpx += 55;
    buttonw.setSize(50,50);
    buttonw.setLocation(bpx, bpy);
    bpx += 55;
    buttonx.setSize(50,50);
    buttonx.setLocation(bpx, bpy);
    bpx += 55;
    buttony.setSize(50,50);
    buttony.setLocation(bpx, bpy);
    bpx += 55;
    buttonz.setSize(50,50);
    buttonz.setLocation(bpx, bpy);
    bpx += 55;

    ActionListener actionListenera = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("a");
        }
    };
    buttona.addActionListener(actionListenera);

    ActionListener actionListenerb = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("b");
        }
    };
    buttonb.addActionListener(actionListenerb);

    ActionListener actionListenerc = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("c");
        }
    };
    buttonc.addActionListener(actionListenerc);

    ActionListener actionListenerd = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("d");
        }
    };
    buttond.addActionListener(actionListenerd);

    ActionListener actionListenere= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("e");
        }
    };
    buttone.addActionListener(actionListenere);

    ActionListener actionListenerf= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("f");
        }
    };
    buttonf.addActionListener(actionListenerf);

    ActionListener actionListenerg= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("g");
        }
    };
    buttong.addActionListener(actionListenerg);

    ActionListener actionListenerh= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("h");
        }
    };
    buttonh.addActionListener(actionListenerh);

    ActionListener actionListeneri= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("i");
        }
    };
    buttoni.addActionListener(actionListeneri);

    ActionListener actionListenerj= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("j");
        }
    };
    buttonj.addActionListener(actionListenerj);

    ActionListener actionListenerk= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("k");
        }
    };
    buttonk.addActionListener(actionListenerk);

    ActionListener actionListenerl= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("l");
        }
    };
    buttonl.addActionListener(actionListenerl);

    ActionListener actionListenerm= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("m");
        }
    };
    buttonm.addActionListener(actionListenerm);

    ActionListener actionListenern= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("n");
        }
    };
    buttonn.addActionListener(actionListenern);

    ActionListener actionListenero= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("o");
        }
    };
    buttono.addActionListener(actionListenero);

    ActionListener actionListenerp= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("p");
        }
    };
    buttonp.addActionListener(actionListenerp);

    ActionListener actionListenerq= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("q");
        }
    };
    buttonq.addActionListener(actionListenerq);

    ActionListener actionListenerr= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("r");
        }
    };
    buttonr.addActionListener(actionListenerr);

    ActionListener actionListeners= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("s");
        }
    };
    buttons.addActionListener(actionListeners);

    ActionListener actionListenert= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("t");
        }
    };
    buttont.addActionListener(actionListenert);

    ActionListener actionListeneru= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("u");
        }
    };
    buttonu.addActionListener(actionListeneru);

    ActionListener actionListenerv= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("v");
        }
    };
    buttonv.addActionListener(actionListenerv);

    ActionListener actionListenerw= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("w");
        }
    };
    buttonw.addActionListener(actionListenerw);

    ActionListener actionListenerx= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("x");
        }
    };
    buttonx.addActionListener(actionListenerx);

    ActionListener actionListenery= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("y");
        }
    };
    buttony.addActionListener(actionListenery);

    ActionListener actionListenerz= new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            check("z");
        }
    };
    buttonz.addActionListener(actionListenerz);



    panel1.add(buttona);
    panel1.add(buttonb);
    panel1.add(buttonc);
    panel1.add(buttond);
    panel1.add(buttone);
    panel1.add(buttonf);
    panel1.add(buttong);
    panel1.add(buttonh);
    panel1.add(buttoni);
    panel1.add(buttonj);
    panel1.add(buttonk);
    panel1.add(buttonl);
    panel1.add(buttonm);
    panel1.add(buttonn);
    panel1.add(buttono);
    panel1.add(buttonp);
    panel1.add(buttonq);
    panel1.add(buttonr);
    panel1.add(buttons);
    panel1.add(buttont);
    panel1.add(buttonu);
    panel1.add(buttonv);
    panel1.add(buttonw);
    panel1.add(buttonx);
    panel1.add(buttony);
    panel1.add(buttonz);

    panel1.add(label1);
    frame1.add(panel1);

    aLFill();
    nWord();
}


private static void nWord() {
    int max = al1.size()-1;
    int min = 0;
    int randomNum = rand.nextInt((max - min) + 1) + min;
    String disp = "";
    label1.setText(al1.get(randomNum));
    for (int i = 0; i < al1.get(randomNum).length(); i++) {
        disp += ".";
    }
    game(al1.get(randomNum), disp);
}

private static void game(String Word1, String disp) {
    Word = Word1;
    cw = Word.toLowerCase().toCharArray();
    cdisp = disp.toCharArray();
    display(disp);
}

private static void check(String letter) {
    String disp = "";
    for (int i = 0; i < Word.length(); i++) {
        disp += ".";
    }
    used.add(letter);
    for (int i = 0; i < used.size(); i++) {
        if (Word.toLowerCase().contains(used.get(i))) {
            for (int j = 0; j < cw.length; j++) {
                if (cw[j] == letter.charAt(0)) {
                    lp.add(j);
                }
            }
            for (int j = 0; j < lp.size(); j++) {
                cdisp[lp.get(j)] = Word.charAt(lp.get(j));
            }
            disp = "";
            for (int j = 0; j < cdisp.length; j++) {
                disp += cdisp[j];
            }
        }
    }
    display(disp);
}

private static void display(String disp) {
    label1.setText(disp);
}

private static void aLFill() {

    String zeile1 = br.readLine();
    al1.add("Hallo");
    al1.add("Test");
    al1.add("Computersimulation");
    al1.add("Elefant");
}
}

它产生错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
at wrs.mainWRS.main(mainWRS.java:70)

我做错了什么?

//已编辑 这里只说一段代码是怎么产生Error的。

package sof;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class wrs{
    static File file = new File("C:\Users\Jan\Desktop\Test.txt");
    static FileReader filereader = new FileReader(file);
    static BufferedReader bufferedreader = new BufferedReader(filereader);

public static void main(String[]args){                  //<-- Line 12
    }
}

新的错误是:

线程"main"中出现异常java.lang.Error:未解决的编译问题:

在 sof.wrs.main(wrs.java:12)

您需要使用双 '\' 而不是这样的:

static File file = new File("C:\Users\Jan\Desktop\eTest.txt");

您需要使用这些“/”之一而不是像这样的“\”:

static File file = new File("C:/Users/Jan/Desktop/eTest.txt");