0

我已修改此示例以读取文本区域中的 txt 文件,但似乎在文本区域中没有输出。

我的代码:

    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();

    BufferedReader br = null;

    try {

        String line;
        br = new BufferedReader(new FileReader(file));

        while ((line = br.readLine()) != null) {
            System.out.println(line);
            stringTokenizer = new StringTokenizer(line, " ");
            while (stringTokenizer.hasMoreElements()) {
                String token = stringTokenizer.nextElement().toString();
            }
        }
        System.out.println("Done");
        textArea.read( br, null );

    } 
    catch (IOException r) {
        r.printStackTrace();
    } 
    finally {
        try {
            if (br != null) br.close();
        } 
        catch (IOException ex) {
            ex.printStackTrace();
        }
    }

我似乎在控制台中也没有任何输出。

4

1 回答 1

0

你的答案在这里:

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();

        BufferedReader br = null;

        try {

            String line;
            br = new BufferedReader(new FileReader(file));
            textArea.read(br, null);

            while ((line = br.readLine()) != null) {
                System.out.println(line);
                StringTokenizer stringTokenizer = new StringTokenizer(line,
                        " ");
                while (stringTokenizer.hasMoreElements()) {
                    String token = stringTokenizer.nextElement().toString();
                }
            }
            System.out.println("Done");

        } catch (IOException r) {
            r.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
于 2013-05-11T18:33:15.807 回答