JAVA printwriter将缓冲区大小从8192个字节增加

时间:2012-04-23 23:44:08

标签: java buffer printwriter

我正在尝试创建一个映射某些部分语音标记的二进制组合的文件,由于某种原因,我编写的java程序在8192字节后停止。我假设这是最大缓冲区大小或什么?我怎么能改变它?

我的代码:

    try {
        Scanner in = new Scanner(new FileInputStream(file.getPath()));
        PrintWriter out = new PrintWriter(new FileOutputStream("S2.gr"));
        createS2(in, out);
    } catch (Exception e) {
        System.out.println("There was an error trying to open the files: " + e.getMessage());
    }


private static void createS2(Scanner in, PrintWriter out) {
    String[] pos = in.useDelimiter("\\A").next().split("\\n");
    out.println("1\tS2");
    for (String x : pos) {
        out.println("1\tS2\t_" + x);    
    }
    for (String x : pos) {
        String temp = ("1\t_" + x + "\t" + x);
        out.println(temp);
        for (String y : pos) {
            out.println(temp + " _" + y);
        }
    }
    for (String x : pos) {
        System.out.println(x);
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个......

    Scanner in = null;
    PrintWriter out = null;
    try {
        in = new Scanner(new FileInputStream(file.getPath()));
        out = new PrintWriter(new FileOutputStream("S2.gr"));
        createS2(in, out);
    } catch (Exception e) {
        System.out.println("There was an error trying to open the files: " + e.getMessage());
    }
    finally {
        if(in != null) in.close();
        if(out != null) out.close();
    }

注意:如果您使用的是Java 7,那么有一项新功能可以让您更轻松。 e.g。

     try (Scanner in = new Scanner(new FileInputStream(file.getPath()));
          PrintWriter out = new PrintWriter(new FileOutputStream("S2.gr")))
     {
        createS2(in, out);
     } catch (Exception e) {
        System.out.println("There was an error trying to open the files: " + e.getMessage());
     }

注意:我目前没有可用的JDK 7,因此第二个示例可能不是100%正确,但我们的想法是新语言功能可以为您管理关闭资源。