android:内部文件读取

时间:2013-09-28 11:11:22

标签: java android file

我正在尝试将一些数据写入内部存储。问题是我读它的时候。它什么都不读。 Logcat说它写了一些东西。但是当我读到它时,Log cat说它是空的。这是我的文件管理器:

public class FileManager {  
    File dir;
    File driveFile;
    BufferedWriter writer=null;
    BufferedReader reader=null;

    public FileManager(Context context,String name){
        dir = new File(context.getFilesDir(),"drives");
        dir.mkdirs();
        driveFile = new File(dir,name+".txt");
        Log.d("FileManager",driveFile.getAbsolutePath());
    }

    public void open() throws IOException{
        writer = new BufferedWriter(new FileWriter(driveFile));
        reader = new BufferedReader(new FileReader(driveFile));
    }

    public void close() throws IOException{
        writer.close();
        reader.close();
    }

    public boolean writeTrackPoints(Drive drive){
        TrackPoint [] trackPoints=drive.getTrackPoints();
        Log.d("FileManager", "writing....");
        for(int i=0;i<trackPoints.length;i++){
            try {
                String line = trackPoints[i].getParsingString()+"\r\n";
                writer.write(line);
                writer.flush();
                Log.d("FileManager", "Writing" + line);
            } catch (IOException e) {
                return false;
            }           
        }       
        return true;
    }

    public boolean readTrackPoints(Drive drive){
        String line=null;
        Log.d("FileManager", "reading....");
        try {
            while((line = reader.readLine())!=null){
                Log.d("FileManager", "Reading: "+line);
                drive.addTrackPoint(new TrackPoint(line));
            }
        } catch (IOException e) {
            Log.d("FileManager", e.getMessage());
            return false;
        }       
        return true;
    }
}

我无法检查文件是否为空,因为我的设备没有root。我认识到在阅读时cyclus并没有进入。我一次打开两个流不是问题吗?

1 个答案:

答案 0 :(得分:1)

我已经解决了。方法是打开/关闭的问题。也许不可能在一瞬间打开两个流。这是编辑过的FileManager:

public class FileManager {

    File dir;
    File driveFile;
    BufferedWriter writer=null;
    BufferedReader reader=null;
    public FileManager(Context context,String name){
        dir = new File(context.getFilesDir(),"drives");
        dir.mkdirs();
        driveFile = new File(dir,name+".txt");
        Log.d("FileManager",driveFile.getAbsolutePath());
    }

    public void writeTrackPoints(Drive drive) throws IOException{
        writer = new BufferedWriter(new FileWriter(driveFile));
        TrackPoint [] trackPoints=drive.getTrackPoints();
        for(int i=0;i<trackPoints.length;i++){
            String line = trackPoints[i].getParsingString()+"\r\n";
            writer.write(line);
            writer.flush();
        }
        writer.close();
    }

    public void readTrackPoints(Drive drive) throws IOException{
        reader = new BufferedReader(new FileReader(driveFile));
        String line=null;
        while((line = reader.readLine())!=null){
            drive.addTrackPoint(new TrackPoint(line));
        }
        reader.close();
    }
}