1

私はこれに数日間立ち往生しています.txtファイルを開くための良い方法が見つかりません。それを複数行の文字列に変換し、テキストビューとして設定する必要があります。誰か助けてくれませんか?

ファイルの場所については、次を使用しています。

String saveLoc = Environment.getExternalStorageDirectory()+"/My Documents/";
public String title;
public String Ftype = ".txt";

(saveLoc+title+Ftype) //is the file location.

通常、データをファイル入力ストリームに読み込むことができますが、それを使って何かをしようとすると、アプリを実行することさえできないエラーが大量に発生します。

4

3 に答える 3

0

Apache Commons IO FileUtils.readLines() を使用します

readLines public static List readLines(File file, Charset encoding) throws IOException Reads the contents of a file line by line to a List of Strings. The file is always closed. Parameters: file - the file to read, must not be null encoding - the encoding to use, null means platform default Returns: the list of Strings representing each line in the file, never null Throws: IOException - in case of an I/O error Since: 2.3

于 2013-05-21T17:20:07.790 に答える
0
private static String readFile(String path) throws IOException 
{
    FileInputStream stream = new FileInputStream(new File(path));
    try {
            FileChannel fc = stream.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
            /* Instead of using default, pass in a decoder. */
            return Charset.defaultCharset().decode(bb).toString();
        }

    catch (IOException e) 
    {
            e.printStackTrace();
    }

    finally 
    {
           stream.close();
    }
}
于 2013-05-21T17:21:22.493 に答える
0
private String readTxt(){

InputStream inputStream = new FileInputStream("Text File Path Here");

     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

     int i;
  try {
   i = inputStream.read();
   while (i != -1)
      {
       byteArrayOutputStream.write(i);
       i = inputStream.read();
      }
      inputStream.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

     return byteArrayOutputStream.toString();
    }
于 2013-05-21T17:18:36.953 に答える