Recent Notes
Displaying keyword search results 1 - 10
Created by lokf on January 13, 2012 14:10:42
Last update: January 13, 2012 14:10:42
For some reason I don't know writing to files in Android is very complicated and tedious. Here is some code for those who might need it. Apps should write in the SD in the directory /Android/data/package_name/files/ so that it is deleted with the app uninstall. package randomname; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.nio.ByteBuffer; import android.content.Context; import android.os.Environment; import android.util.Log; public class FileIOLibrary { String packageName; boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; FileIOLibrary(String packageNamep) { this.packageName=packageNamep; } public boolean isExternalStorageAvailable() { updateExternalStorageState(); return mExternalStorageAvailable; } public boolean isExternalStorageWritable() { updateExternalStorageState(); return mExternalStorageWriteable; } /** * writes the current state of SD card to the corresponding variables */ void updateExternalStorageState() {...
Created by Dr. Xi on November 11, 2011 10:05:22
Last update: November 11, 2011 10:12:01
This is an HTML image tag filter using Java regex. It takes a string, finds the img tags, replaces the src attribute with one provided by the filter, then adds a class name to the class attribute.
import java.util.regex.*;
import java.io.*;
...
Test file:
<div id="HTML snippet">
<img src="img/big/txt-m...
Created by voodoo on August 12, 2011 14:43:57
Last update: August 14, 2011 15:19:21
#include <stdio.h>
#include <stdlib.h>
i...
Created by voodoo on August 12, 2011 14:44:45
Last update: August 12, 2011 14:44:45
#include <fstream>
#include <iostream>
i...
Created by alfa on July 15, 2011 13:25:45
Last update: July 15, 2011 13:25:45
Read the whole contents of a file into a String. It's better to read the whole file as bytes and convert to String than to read the file line by line and concatenate the lines.
String getFileContents(String fileName) throws...
Using java.nio :
import java.io.FileInputStream;
import java...
Created by alfa on May 04, 2011 12:08:57
Last update: July 15, 2011 12:31:41
To read a whole file at once into a byte array:
File theFile = new File("test.bin");
byte[] byt...
It is important that the total number of bytes read is used for the while condition. Changing the loop to the following may result in an infinite loop !
int m = 0, n = 0;
while (n >= 0) {
n = i...
Created by Dr. Xi on June 21, 2011 15:54:00
Last update: June 22, 2011 11:33:09
Demo code for CSV parsing with SuperCSV parser .
Java code:
import java.io.*;
import java.util.List;
imp...
Test with a simple CSV file:
psmith01,CLASS2B,Peter Smith 1,YEAR2,1,N,ADVANCED,...
The parser worked correctly:
Line 1 has 11 values:
|psmith01|
|CLASS2B|...
Test with a more complicated CSV file:
"psmith01 abc", "CLASS2B " , " Peter...
The parser messed up on all three lines:
Line 1 has 5 values:
|psmith01 abc|
|CLASS...
Using two lines input:
"psmith01 abc", "CLASS2B " , " Peter...
It generates an exception:
Line 1 has 5 values:
|psmith01 abc|
|CLASS...
Add a new line in item two:
"One", "Two
", "Three"
Result:
Line 2 has 3 values:
|One|
|Two
|
|...
Created by freyo on April 15, 2011 09:08:28
Last update: April 15, 2011 09:08:28
Follow three steps to read a file from Android internal storage:
Call openFileInput() and pass it the name of the file to read. This returns a java.io.FileInputStream .
Read bytes from the file with read() .
Then close the stream with close() .
Sample code:
FileInputStream in = openFileInput("test.txt");
...
Created by freyo on April 15, 2011 09:00:54
Last update: April 15, 2011 09:00:54
Sample code for writing to a file in the internal storage. There are three steps:
Open the file with Context.openFileOutput , which returns java.io.FileOutputStream .
Write to the file.
Close the file.
import java.io.*;
import android.content.Contex...
The second parameter to openFileOutput is the operating mode. Available values are:
Context.MODE_PRIVATE
Context.MODE_APPEND
Context.MODE_WORLD_READABLE
Context.MODE_WORLD_WRITEABLE
The file is saved in /data/data/<package_name>/files .
Created by alfa on April 06, 2011 12:48:44
Last update: April 06, 2011 12:48:44
package com.demo.io;
import java.io.*;
...