IllegalArgumentException: файл содержит разделитель пути Android
Я пытаюсь записать в выходной файл моего HTC One и получить следующее сообщение в LogCat:
11-21 08: 05: 18.228: W / System.err (6609): java.lang.IllegalArgumentException: файл /storage/emulated/0/com.example.pattern1/myfile.txt содержит разделитель пути
Исходный код приведен ниже:
protected void writeToFile(String string){
File patternDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/com.example.pattern1/myfile.txt");
patternDirectory.mkdirs();
FileOutputStream outputStream;
try {
outputStream = openFileOutput(patternDirectory.getAbsolutePath().toString(), Context.MODE_APPEND);
outputStream.write(string.getBytes());
TextView t = (TextView)findViewById(R.id.bottomMidText);
t.setText(patternDirectory.getAbsolutePath().toString());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Буду признателен, если кто-то может помочь определить проблему.
2 ответа
Метод openFileInput не принимает разделители пути.('/')
он принимает только имя файла, который вы хотите открыть / доступ. так что смени заявление
outputStream = openFileOutput(patternDirectory.getAbsolutePath().toString(), Context.MODE_APPEND);
в
outputStream = new FileOutputStream (new File(patternDirectory.getAbsolutePath().toString()), true); // true will be same as Context.MODE_APPEND
Одной из проблем может быть то, что вы делаете: Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/com.example.pattern1/myfile.txt"
Вы создаете каталог с именем myfile.txt