BufferedWriter 没有将所有内容写入其输出文件 [英] BufferedWriter not writing everything to its output file

查看:30
本文介绍了BufferedWriter 没有将所有内容写入其输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Java 程序,它从文件中逐行读取一些文本,并将新文本写入输出文件.但在程序完成后,并非我写入 BufferedWriter 的所有文本都出现在输出文件中.这是为什么?

I have a Java program that reads some text from a file, line by line, and writes new text to an output file. But not all the text I write to my BufferedWriter appears in the output file after the program has finished. Why is that?

细节:程序获取一个 CSV 文本文档并将其转换为 SQL 命令以将数据插入表中.文本文件有 10000 多行,类似于以下内容:

The details: the program takes a CSV text document and converts it into SQL commands to insert the data into a table. The text file has more than 10000 lines which look similar to following:

2007,10,9,1,1,1006134,19423882

该程序似乎运行良好,但它只是在创建新 SQL 语句并将其打印到 SQL 文件中的过程中随机停止在文件中.它看起来像:

The program seems to work fine except it just stops in the file randomly half way through creating a new SQL statement having printed it into the SQL file. It looks something like:

insert into nyccrash values (2007, 1, 2, 1, 4, 1033092, 259916);
insert into nyccrash values (2007, 1, 1, 1, 1, 1020246, 197687);
insert into nyccrash values (2007, 10, 9, 1

这发生在大约 10000 行之后,但在文件末尾之前的几百行.中断发生在 1, 之间.但是,字符似乎并不重要,因为如果我将 1 更改为 42,写入新文件的最后一件事是 4,这是从那个整数中截去 2 .所以看起来读者或作者在写/读了一定数量后肯定会死.

This happens after about 10000 lines but several hundred lines before the end of the file. Where the break happens is between a 1 and a ,. However, the characters doesn't seem important because if I change the 1 to a 42 the last thing written to the new file is 4, which is cutting off the 2 from that integer. So it seems like the reader or writer must just be dying after writing/reading a certain amount.

我的Java代码如下:

My Java code is as follows:

import java.io.*;

public class InsertCrashData
{
    public static void main (String args[])
    {
        try
        {   
            //Open the input file.
            FileReader istream = new FileReader("nyccrash.txt");
            BufferedReader in = new BufferedReader(istream);
            //Open the output file.
            FileWriter ostream = new FileWriter("nyccrash.sql");
            BufferedWriter out = new BufferedWriter(ostream);
            String line, sqlstr;

            sqlstr = "CREATE TABLE nyccrash (crash_year integer, accident_type integer, collision_type integer, weather_condition integer, light_condition integer, x_coordinate integer, y_coordinate integer);\n\n"; 
            out.write(sqlstr);

            while((line = in.readLine())!= null)
            {
                String[] esa = line.split(",");
                sqlstr = "insert into nyccrash values ("+esa[0]+", "+esa[1]+", "+esa[2]+", "+esa[3]+", "+esa[4]+", "+esa[5]+", "+esa[6]+");\n";
                out.write(sqlstr);
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

推荐答案

您需要关闭 OutputStream 这将刷新您的其余数据:

You need to close your OutputStream which will flush the remainder of your data:

out.close();

BufferedWriter 的默认缓冲区大小是 8192 个字符,大到足以轻松容纳数百行未写入的数据.

The default buffer size for BufferedWriter is 8192 characters, large enough to easily hold hundreds of lines of unwritten data.

这篇关于BufferedWriter 没有将所有内容写入其输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆