Android studio文件写入不起作用

时间:2018-03-25 10:24:42

标签: java android

我有一个小问题。我试图写文件然后阅读它,但它无法正常工作。这是代码

 public void SaveData(String data){
    try {
        FileOutputStream fieloutputsteam = openFileOutput("basketball.txt",MODE_PRIVATE);
        fieloutputsteam.write(data.getBytes());
        fieloutputsteam.close();
        Log.d("debug1","Save completed");
        Log.d("debug1","Data: "+ data+"");
    }
    catch(Exception e)
    {
        Log.d("debug1","Error on saving");
    }
}
public void ReadFile() {
    String data2 = null;
    try {
        FileInputStream fileinputsteam = openFileInput("basketball.txt");
        InputStreamReader isr = new InputStreamReader(fileinputsteam);
        BufferedReader br = new BufferedReader(isr);
        StringBuffer sb = new StringBuffer();
        while ((data2 = br.readLine()) != null) {
            sb.append(data2 + "\n");
        }
        Log.d("debug1","Reading complete");
        Log.d("debug1","data: "+data2+"");

    } catch (Exception e) {
        Log.d("debug1","Error on read");
    }
}

日志输出为:

03-25 10:11:53.814 12149-12149/com.example.remix3030303.myapplication D/debug1: Save completed
03-25 10:11:53.814 12149-12149/com.example.remix3030303.myapplication D/debug1: Data: 1,1,0,0,0,0
03-25 10:11:53.819 12149-12149/com.example.remix3030303.myapplication D/debug1: Reading complete
03-25 10:11:53.820 12149-12149/com.example.remix3030303.myapplication D/debug1: data: null

我不知道为什么读取后的数据为空。 我在Android Studio 3.0.1上 我还将此添加到我的清单中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_USER_DICTIONARY"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

1 个答案:

答案 0 :(得分:0)

注意循环条件:

public default GraphTraversal<S, Map<Object, Long>> suggestedFriends(Long limit) {
    return out("friendship")
        .aggregate("x")
        .as("myFriends")
        .out("friendship")
        .as("friendsOfFriends")
        .where(P.without("x"))
        .groupCount()
        .by("email")
        .order(Scope.local)
        .by(Column.values, Order.decr)
        .limit(Scope.local, limit);
}

当它停止时,while ((data2 = br.readLine()) != null) { sb.append(data2 + "\n"); } 必须为data2,您应该记录null

StringBuffer sb