0

这是我将数据写入文件的方式

FileOutputStream fos = openFileOutput("ThisWeekDatabase", Context.MODE_PRIVATE);
String Line;
while ((Line = thisWeekReader.readLine()) != null) {
        fos.write(Line.getBytes()); 
}
fos.close();

这就是我试图读取同一个文件的方式

FileInputStream fIn = openFileInput ("ThisWeekDatabase") ;
InputStreamReader isr = new InputStreamReader ( fIn ) ;
BufferedReader buffreader = new BufferedReader ( isr ) ;
String Line;
while ((Line = buffreader.readLine ()) != null ) {
    if (Line.contains("xyz")){
        doSomething();
    }
}
isr.close ( ) ;

但我得到java.io.FileNotFoundException

4

2 回答 2

0
Thread thread = new Thread() {

    @Override
    public void run() {
        try {
            saveDataFromWeb();// here i write data into a file
        } catch (Throwable e) {
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    findViewById(R.id.viewpager).getContext());
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            sw.toString();
            builder.setMessage(sw.toString());
            AlertDialog alert = builder.create();
            alert.show();
        }
    }
};
if (CONDITION) {
    thread.start();
    parseData("ThisWeekDatabase"); // here I read the data from the 
    parseData("NextWeekDatabase");
}

我发现了错误。这是因为当我尝试读取这些文件时,线程没有完成它的工作(将数据从网络写入文件)。现在我需要一种“等待”线程的方法,以便它在parseData(xyz)需要时完成写入文件。

我将数据写入文件的方式或从文件中读取数据的方式也是错误的,因为当我读出之前写入的文件时,我得到一个长行而不是多个短行。

我明白了

div.lvsdrt {background-color:#e3f1ff; border-top:black 1px dotted;padding:0.1em;}div.lvwer{background-color:#f2f9ff;border-top:black 1px dotted;padding:0.1em;}div.nobreak {page-break-inside:avoid;}

代替

div.lvsdrt {
    background-color:#e3f1ff;
    border-top:black 1px dotted;
    padding:0.1em;
}
div.lvwer {
    background-color:#f2f9ff;
    border-top:black 1px dotted;
    padding:0.1em;
}
div.nobreak {
    page-break-inside:avoid;
}
于 2012-10-05T19:55:16.020 回答
0
if (CONDITION) {
    final CountDownLatch latch = new CountDownLatch(1);
    Thread thread = new Thread() {

        @Override
        public void run() {
            try {
                saveDataFromWeb();// here i write data into a file
            } catch (Throwable e) { // CATCH THROWABLE ??!! WHY ?
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        findViewById(R.id.viewpager).getContext());
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                sw.toString();
                builder.setMessage(sw.toString());
                AlertDialog alert = builder.create();
                alert.show();
            } finally {
                latch.notify();
            }
        }
    };
    thread.start();
    latch.await();
    parseData("ThisWeekDatabase"); // here I read the data from the
    parseData("NextWeekDatabase");
}

至于新线:

while ((line = thisWeekReader.readLine()) != null) {
    fos.write((line + "\n").getBytes());
}

\n只是一个 hack - 如果你在 Windows 上使用\r\n或者更好,请阅读这个

编辑:如果您不能使闩锁工作,请使用:

thread.start();
try {
    thread.join();
} catch (InterruptedException e) {
    // TODO handle
}
parseData("ThisWeekDatabase"); // here I read the data from the
parseData("NextWeekDatabase");
于 2013-05-04T01:37:40.477 回答