Android Reader Read readTextFile(String fileName)

Here you can find the source of readTextFile(String fileName)

Description

read text from a specified text file store in temporary path

Parameter

Parameter Description
fileName file name specified

Return

text file content

Declaration

public static String readTextFile(String fileName) 

Method Source Code

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main{
    public static final String tmpdir = System
            .getProperty("java.io.tmpdir") + File.separator;
    /**/*from w  ww  . ja v  a2s.c o m*/
     * read text from a specified text file store in temporary path
     * @param fileName file name specified
     * @return text file content
     */
    public static String readTextFile(String fileName) {
        try {
            File file = new File(FileUtil.tmpdir + fileName + ".txt");
            FileReader reader = new FileReader(file);
            BufferedReader in = new BufferedReader(reader);
            String line;
            StringBuffer sb = new StringBuffer();
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            in.close();
            return sb.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. copy(Reader input, OutputStream output)
  2. copy(Reader input, Writer output)
  3. readContents(Reader in)
  4. readFile(String filename)
  5. readFileReader(String filePath)
  6. slurpLines(final Reader reader)
  7. slurpValidLines(final Reader reader)
  8. toList(Reader reader)
  9. nextString(Reader r)