在textarea中读取文件时没有输出

时间:2013-05-11 17:50:52

标签: java swing file io jtextarea

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

我的代码:

    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();
        }
    }

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

1 个答案:

答案 0 :(得分: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();
            }
        }