从文本文件中读取数据

时间:2012-10-07 13:40:31

标签: java android

我正在尝试读取文本文件并将这些文本文件中的数据插入到URL中; 但我有这个错误“java.lang.illegalargumentexception包含一个路径分隔符文件”

这是我使用

的读取方法

>

public  String ReadFile (String Path){
    String     res  =    null;
    try {
        String filePath = android.os.Environment.getExternalStorageDirectory().getPath() + "/" + Path;

        File file = new File(filePath);
        if(file.exists()){
           InputStream       in = openFileInput(filePath);

           if (in != null) {
            // prepare the file for reading
             InputStreamReader input = new InputStreamReader(in);
             BufferedReader buffreader = new BufferedReader(input);

              res = "";
              String line;
            while (( line = buffreader.readLine()) != null) {
                res += line;
              }
              in.close();

              }else{
            }
        }else{
               Toast.makeText(getApplicationContext(), "The File" + Path +  " not Found" ,Toast.LENGTH_SHORT).show();
        }
    } catch(Exception e){
           Toast.makeText(getApplicationContext(),e.toString() +   e.getMessage(),Toast.LENGTH_SHORT).show();
    }
    return res;
}



  >  String sendername = ReadFile ("SenderName.txt");
     String AccountName = ReadFile ("AccountName.txt");
     String AccountPassword = ReadFile ("AccountPassword.txt");
     String MsgText = ReadFile ("MsgText.txt");

谢谢,

1 个答案:

答案 0 :(得分:0)

- 虽然此错误并未指明,但您仍然获得了在Manifest.xml文件中读取外部存储的权限

尝试这样的事情......

public void readFile(String path){


File f = new File(path);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);

String read = new String();

String temRead = new String();

while((temRead = br.readLine())!=null){

     read = read + temRead;

 }



}