Android – reading and writing text files

On Android you have couple of possibilities of saving the data, e.g. shared preferences or SQLite database. Sometimes you may wish to save the data to a text file – so as to enable your users to edit the data in Notepad, to send it to their friends etc (e.g. in my flexible dice roller users are able to send each other dice they have created).

How to do that?

Preparation

Add permissions to the Manifest file as the data will be saved on the SD card:

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

Writing a text file on Android

Check if the SD card is available for writing:

if (Environment.MEDIA_MOUNTED.equals(
	Environment.getExternalStorageState())) {
	// Writing possible
 } else {
	// Writing not possible
 }

Perform writing:

try {
	File folder = new File(Environment.getExternalStorageDirectory(), "NAME_OF_YOUR_FOLDER");
	
	if (!folder.exists()) {
		// If the folder does not exist, create it
		folder.mkdirs();
	}

	File file = new File(folder, "NAME_OF_YOUR_FILE");
	FileWriter fileWriter = new FileWriter(file);
				
	fileWriter.append("TEXT_TO_WRITE_TO_FILE");
	fileWriter.flush();
	fileWriter.close();
				
	// Writing successful
} catch (IOException e) {
	// Writing failed
}

If you want to create a new line in the file add “\n” to your string, e.g.

fileWriter.append("Line 1\nLine2\nLine3")

However, if you expect that the users might want to edit the file in Windows, use “\r\n”, e.g. (Windows has different text encoding and will display text in a single line if separated only with “\n”)

fileWriter.append("Line 1\r\nLine2\r\nLine3")

Reading a text file on Android

Check if the SD card is available for reading:

if (Environment.MEDIA_MOUNTED.equals(Environment
	.getExternalStorageState())
	|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment
	.getExternalStorageState())) {
	// Reading possible
} else {
	//Reading not possible
}

Perform reading:

BufferedReader bufferedReader = null; 
try {
	File folder = new File(Environment.getExternalStorageDirectory(), "NAME_OF_YOUR_FOLDER");
	
	if (!folder.exists()) {
		// Folder does not exist - do something
	}
	
	File file = new File(folder,"NAME_OF_YOUR_FILE");
	bufferedReader = new BufferedReader(new FileReader(file));  
		    
	String line;   
	// Read line by line
	while ((line = bufferedReader.readLine()) != null) {
		// Do something with "line"
	}       
	bufferedReader.close();
}catch (IOException e) {
	
	// Reading failed
	// Close bufferedReader to prevent memory leak
	if (bufferedReader!=null) {
		try {
			bufferedReader.close();
		} catch (IOException e1) {
			// Closing failed
		}
	}
}

←Table of contents
←My apps, games and other projects

Advertisements
Android – reading and writing text files

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s