How to open a file using its absolute path?
await File('c:/Users/rozerro/AndroidStudioProjects/rxdart_streams/assets/sonet.txt')
.openRead()
.transform(utf8.decoder)
.transform(LineSplitter())
.toList();
as well as
c:\\Users\\rozerro\\AndroidStudioProjects\\rxdart_streams\\assets\\sonet.txt
The error
E/flutter ( 4943): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)]
Unhandled Exception: FileSystemException: Cannot open file, path =
'c:/Users/rozerro/AndroidStudioProjects/rxdart_streams/assets/sonet.txt'
(OS Error: No such file or directory, errno = 2)
You cannot do that. You cannot access a file on your local PC from an app deployed on your smartphone.
Since your text file is in a folder named assets, I assume it is some kind of file you want to have deployed with your app. In flutter that is called an "asset" and you can read up on how to achieve that in the documentation about assets and images.
In short, your pubspec.yaml file needs to have an entry for it:
flutter:
assets:
- assets/sonet.txt
And then you can access it in your source with this method:
final textFromFile = await rootBundle.loadString('assets/sonet.txt');
Related
File('/data/user/0/com.abc/cache/full.jpg')
.writeAsBytesSync(class_image.encodeJpg(data));
I found out that Android works with this path. But in ios, I found out that it doesn't work with the error below.
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: FileSystemException: Cannot open file, path = '/data/user/0/com.abc/cache/full.jpg' (OS Error: No such file or directory, errno = 2)
I want to use the same folder for both. Is this possible? If not, the workaround is to manage the ios folder separately?
File('/data/user/0/com.abc/cache/full.jpg') I have observed that you have specified path manually. The path you speicified works only for android but for ios it is completely different.So instead of specifying manually use path provider and create platform specified path then use it
import 'package:path_provider/path_provider.dart';
Future<Directory?> getLocalDirectory() async {
return Platform.isAndroid ? await path.getTemporaryDirectory(): await path.getApplicationSupportDirectory(); }
The storage directory of each platform is different
You can obtain the local storage directory separately through the path_provider plugin, and then create your path.
like this
Future<Directory?> getLocalDirectory() async {
return Platform.isAndroid
? await path.getExternalStorageDirectory()
: await path.getApplicationSupportDirectory();
}
I want to be able to create a new file and write a string into it. Examples that I can find all seems to pertain to writing to an existing file.
String fileName = 'myFile.txt';
String contents = 'hello';
void writeToFile(String fileName, String contents){
File outFile = File('content://com.android.providers.media.documents/document/document/document_root/' + fileName);
outFile.writeAsString(contents);
}
This results in the following error, as expected
Unhandled Exception: FileSystemException: Cannot open file, path = 'content://com.android.providers.media.documents/document/document/document_root/myFile.txt' (OS Error: No such file or directory, errno = 2)
How can I create a new file in which I can write my contents?
Are you sure that the path exists? writeAsString does create the file if it doesn't exists, but it doesn't create the full folder structure up to the file you're trying to write to. Also, you might not have the rights to write in the path you've specified.
Anyway, you'd better use this plugin to get the folders' path instead of hard-coding them.
i'm trying to create/write a file to a specific directory. i already have my code but the problem is i always got an error saying:
flutter: FileSystemException: Cannot open file, path = '/var/mobile/Containers/Data/Application/AF14244D-E8C4-4B9A-8005-D7CA7CC3520B/Documents/cache/files/abcb-1234567890' (OS Error: No such file or directory, errno = 2)
here's my code:
Directory directory = Platform.isAndroid ? await getExternalStorageDirectory() : await getApplicationDocumentsDirectory();
String path = directory.path + '/cache/files';
File file = new File('$path/$fileID');
final results = await post(...);
file.writeAsBytesSync(results);
what am i missing?
additional question: if anyone would know how can i access created date of the file that i have just created? - reason for this is to delete them after a set date or like an expiry date.
appreciate any help. thank you in advance. =)
Are you sure this path exists?
I belive your code is executing part getApplicationDocumentsDirectory()
That mean you are not on android.
Can you access this path using other app (some file explorer etc)?
The interesting part is here:
'/cache/files' maybe this folder doesn't exist.
Here is on my Windows:
var dir = io.Directory.fromUri(Uri.directory('UNO\\DUE\\'));
dir.createSync(recursive: true);
var file = io.File('${dir.absolute.path}\demo.txt');
file.writeAsStringSync('ABCDEFHJ');
Without dir.createSync(recursive: true); without recursive parameter I have exactly same problem.
E/flutter (13503): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: FileSystemException: Cannot open file, path = 'lib/certs/ca.pem' (OS Error: No such file or directory, errno = 2)
SecurityContext clientContext = new SecurityContext()
..setTrustedCertificates('assets/certs/ca.pem');
var client = new HttpClient(context: clientContext);
var request = await client.getUrl(
Uri.parse('192.168.0.132', 10000));
var response = await request.close();
assets:
- assets/
- assets/certs/
all the files are inside lib folder and inside that lib folder there is certs folder and inside the certs folder all the pem and certificates are there and i need to use it for encryption. it cannot find it but in the actual main function in flutter. if i read file from a different dart code file then it is working but i want that in my project.enter image description here
Move your certs folder from lib folder to asset folder which locate in the your app root directory. If you do not have an asset folder create one in root directory.
Here is the setup in side of my
pubspec.yaml
...
path_provider: ^0.4.1
assets:
- assets/
- assets/a.txt
...
I try to find way to access to the "assets" directory to read content of the text file named "a.txt" where it should read in relative way as "assets/a.txt".
Here is my code in my
main.dart:
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
String fPath = '${appDocDir.parent.path}/assets/a.txt';
new File(fPath).readAsString().then((String contents) {
print("FILE:..... "+contents);
});
And here I got this error:
[VERBOSE-2:shell.cc(184)] Dart Error: Unhandled exception:
FileSystemException: Cannot open file, path = '/var/mobile/Containers/Data/Application/EE2A0E91-6543-4CE3-A82B-3B69BB892BFC/assets/a.txt' (OS Error: No such file or directory, errno = 2)
Seem like the ... EE2A0E91-6543-4CE3-A82B-3B69BB892BFC/ is not the root directory of the app to concat with my file assets/a.txt.
How can I find the exact absolute path of my a.txt file in order to read it using File() class?
You need to use AssetBundle to read assets.
For example https://docs.flutter.io/flutter/services/AssetBundle/load.html or https://docs.flutter.io/flutter/services/AssetBundle/loadString.html
See also https://flutter.io/docs/development/ui/assets-and-images#loading-text-assets