文件的保存位置有要求
String path ="/data/data/" + this.getPackageName() +"/demo.txt";
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Test();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void Test() throws IOException {
final String tag = "testFile";
String path ="/data/data/" + this.getPackageName() +"/demo.txt";
FileWriter fileWriter = new FileWriter(path);
for( int i = 0; i <102; i++){
fileWriter.write(String.valueOf(i));
}
fileWriter.flush();
fileWriter.close();
FileReader fileReader = new FileReader(path);
char[] buf = new char[24];
int num;
while(((num = fileReader.read(buf)) != 0)){
System.out.println("---" + new String(buf,0,num));
}
fileReader.close();
}
}
结果
com.example.java.m1031a I/System.out: ---012345678910111213141516
com.example.java.m1031a I/System.out: ---171819202122232425262728
com.example.java.m1031a I/System.out: ---293031323334353637383940
com.example.java.m1031a I/System.out: ---414243444546474849505152
com.example.java.m1031a I/System.out: ---535455565758596061626364
com.example.java.m1031a I/System.out: ---656667686970717273747576
com.example.java.m1031a I/System.out: ---777879808182838485868788
com.example.java.m1031a I/System.out: ---899091929394959697989910
com.example.java.m1031a I/System.out: ---0101