I am trying to create and then write a simple file called "thisisafile.txt" in Android. After I run the application, file does not exist in internal storage.
import java.io.*;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String fileName = "thisisafile.txt";
writeFile(fileName, "Hello World!");
}
public static void writeFile(String fileName, String text) {
File file = new File (fileName);
try {
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(text);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Is the file written? If then where, because I can't find it. If not, what am I doing wrong in the code?
For Read <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> For Write <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Need Read / Write Permission On Manifest, Allow Storage Permission.
private void writeToFile(String data,Context context) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("config.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
Referrence:
https://developer.android.com/training/permissions/requesting#java https://stackoverflow.com/a/44155064/5207684
Use the following path for a file. It will write file to your root folder of storage.
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file= new File(extStorageDirectory, "config.txt");
writeToFile("File content".getBytes(), file);
writeToFile
public static void writeToFile(byte[] data, File file) throws IOException {
BufferedOutputStream bos = null;
try {
FileOutputStream fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(data);
}
finally {
if (bos != null) {
try {
bos.flush ();
bos.close ();
}
catch (Exception e) {
}
}
}
}
add external storage writing AND reading permissions in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
After Android 6.0 WRITE_EXTERNAL_STORAGE permission must request in run time
requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE}, 1);
and then can handle the result of permission request in onRequestPermissionsResult() method inside activity called from it.
Example:
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
// Permission has already been granted
}
Check on real device instead of Emulator.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.