Android:打开具有特定路径的文件

时间:2013-03-06 22:09:50

标签: java android fileinputstream fileoutputstream illegalargumentexception

我尝试创建文件^设置一些文本^然后阅读它,但我得到

“java.lang.IllegalArgumentException:File /mnt/sdcard/new.txt包含路径分隔符”

on line(75) - FileInputStream fis = new FileInputStream(new File(Environment.getExternalStorageDirectory()+ File.separator + fileName));

这是我的代码:

String fileName = "new.txt";

file = new File(Environment.getExternalStorageDirectory() + File.separator + fileName);
    Log.d("External Storage :"," "+ Environment.getExternalStorageDirectory() + File.separator + fileName);
    if(!file.exists()){
        try {
            file.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.d("IOException: "," "+e.getMessage());
        }
    }else{
        Log.d("Create error:", "file exist");
    }

    try {
        FileOutputStream fout = openFileOutput(fileName, MODE_WORLD_READABLE );
        OutputStreamWriter osw = new OutputStreamWriter(fout);
        osw.write("its first file and i need to know how it work's");
        fout.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        Log.d("FOS  OSW :", ""+e.getMessage());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.d("osw write :", ""+e.getMessage());
    }

    try {
        FileInputStream fis = new FileInputStream(new File(Environment.getExternalStorageDirectory() + File.separator +fileName));     
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader bf  = new BufferedReader(isr);
        try {
            fis.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            Log.d("fis close :", ""+e1.getMessage());
        }
        try {
            tvRead.setText("text : "+bf.readLine());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.d("to tv Read  :", ""+e.getMessage());
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        Log.d("fis open :", ""+e.getMessage());

    }
}

0 个答案:

没有答案