Android examples for java.io:Text File
read text File by char set
import android.app.Activity; import android.content.Context; import android.content.CursorLoader; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.provider.MediaStore; import android.text.TextUtils; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class Main{ /**/*from w w w . j a v a 2s.c o m*/ * read file * * @param filePath path * @param charsetName The name of a supported {@link java.nio.charset.Charset * </code>charset<code>} * @return if file not exist, return null, else return content of file * @throws RuntimeException if an error occurs while operator BufferedReader */ public static StringBuilder readFile(String filePath, String charsetName) { File file = new File(filePath); StringBuilder fileContent = new StringBuilder(""); if (!file.isFile()) { return null; } BufferedReader reader = null; try { InputStreamReader is = new InputStreamReader( new FileInputStream(file), charsetName); reader = new BufferedReader(is); String line; while ((line = reader.readLine()) != null) { if (!fileContent.toString().equals("")) { fileContent.append("\r\n"); } fileContent.append(line); } reader.close(); return fileContent; } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } } } } }