我是android開發新手。我試圖在Android設備上用數字編寫一個文件。我可以將文件存儲在內部存儲器中,但目前我無法在此文件上寫入。Android內部存儲文件寫入無法正常工作
這是我的代碼。 有任何建議透露給我。
int count=0;
FileOutputStream outputStream;
BufferedReader input=null;
File file=new File(getCacheDir(),"example.txt");
if(file.exists()==true){
try {
input = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
StringBuilder builder=new StringBuilder();
while((line=input.readLine())!=null){
builder.append(line);
}
String g=builder.toString();
Toast.makeText(getApplicationContext(),"Count - " + g,Toast.LENGTH_LONG).show();
}catch (IOException e){
}
}else {
try {
count=count+1;
outputStream = new FileOutputStream(file);
outputStream.write(count);
outputStream.close();
Toast.makeText(getApplicationContext(),"File - "+file.getPath().toString(),Toast.LENGTH_LONG).show();
}catch (IOException e){
Toast.makeText(getApplicationContext(),"Error creating this file",Toast.LENGTH_LONG).show();
}
}
感謝
@ChiragSavsani謝謝。 –
現在工作嗎? –
@ChiragSavsani - 仍然不起作用 –