无法打开通过openFileOutput保存的openFileInput的文件

时间:2012-10-05 17:49:04

标签: android filestream

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

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

2 个答案:

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

我发现了错误。这是因为当我尝试读取这些文件时,线程没有完成它的工作(将数据从Web写入文件)。现在我需要一种方法来“等待”线程,以便在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;
}

答案 1 :(得分: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只是一个黑客攻击 - 如果您在Windows上使用\r\n或更好,请阅读this

编辑:如果你无法使用闩锁工作:

thread.start();
try {
    thread.join();
} catch (InterruptedException e) {
    // TODO handle
}
parseData("ThisWeekDatabase"); // here I read the data from the
parseData("NextWeekDatabase");