我如何将字节数组(以 JSON 中的字符串形式发送)转换为 .csv 文件。
到目前为止我所做的是
将 JSON 接收转换为 unit8list
并使用 .csv 文件创建一个新文件并将内容写入 file.writeAsBytes(bytes)
我正在获取 .csv 文件,但数据是在单单元格中写入的(作为在收到的 JSON 响应中收到的字符串)
// response from json
DownloadResponse value = DownloadResponse.fromJson(jsonDecode(response.body));
List<int> bytes = utf8.encode(value.object);
Uint8List bytes8 = Uint8List.fromList(bytes);
print("-------------- List<int> ------------");
print(bytes);
print("-------------- END List<int> ------------");
print("\n\n-------------- List<Unit8> ------------");
print(bytes8);
print("\n\n-------------- END List<Unit8> ------------");
print("location supply!!");
File file = new File('/storage/emulated/0/Download/${currentFile.fileName}');
File filety = new File('/storage/emulated/0/Download/file2.csv');
print("creating file");
var file2 = await file.writeAsBytes(bytes);
var file3 = await filety.writeAsBytes(bytes8);
var parent = file2.parent;
var parent2 = file2.parent;
print("parant: $parent");
print("parant2: $parent2");
main.dart
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:ext_storage/ext_storage.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as path;
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(
MaterialApp(
title: 'Reading and Writing Files',
home: MyApp(),
),
);
}
class MyApp extends StatefulWidget {
@override
_AppState createState() => _AppState();
}
class _AppState extends State<MyApp> {
String data;
String byteStr = "MWNvbnRhaW5lcl9udW1iZXIsY2F0ZWdvcnksZW50cnlfbW9kZSxleGl0X21vZGUsZW50cnlfdGltZSxleGl0X3RpbWUsY29udGFpbmVyX3NpemUsaXNfcmVmcmlnZXJhdGVkLHdhZ29uX251bWJlcixsb2FkaW5nX3RpbWUsZGVzdGluYXRpb24sc2hpcHBpbmdfbGluZSx2aWFfbm8sdmVzc2VsX25hbWUsaXNvX2NvZGUsZW1wdHlfY250cixkcGRfZHBlLGVycm9yCkJNT1U2ODkzNDgwLEksLCwsLCwsLCwsTV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TE2LEksLCwsLCwsLCwsTVNLLDIxMDEwNiwsLCwsU2hpcHBpbmcgbGluZSBjb2RlIGRvZXMgbm90IGV4aXN0cwpUQ05VMTQ5NDE4MixJLCwsLCwsLCwsLE1TSywyMTAxMDYsLCwsLFNoaXBwaW5nIGxpbmUgY29kZSBkb2VzIG5vdCBleGlzdHMKVENOVTE0OTQxODIsSSxWLFIsMzEwMTIwMjEgMjE6NTY6MjYsMDMwMjIwMjEgMTU6NTM6MjAsNDAsTiwsMzEwMTIwMjEgMjE6NTY6MjYsSUNESUxQLVQsTVNLLDIxMDEwNiwsLE4sWSxOZXh0IGRlc3RpbmF0aW9uIGRvZXMgbm90IGV4aXN0cwpUR0JVMzIxODc3NCxFLCwsLCwsLCwsLE1TQywyMTAyMDEsLCwsLFNoaXBwaW5nIGxpbmUgY29kZSBkb2VzIG5vdCBleGlzdHMKVEdCVTMyMTg3NzQsRSxSLCwwMzAyMjAyMSAxNjoxMjowMCwsMjAsTiwsLE9URkFDLE1TQywyMTAyMDEsLCxOLE4sTmV4dCBkZXN0aW5hdGlvbiBkb2VzIG5vdCBleGlzdHMK";
Future<File> moveFile(File sourceFile, String newPath) async {
try {
/// prefer using rename as it is probably faster
/// if same directory path
return await sourceFile.rename(newPath);
} catch (e) {
/// if rename fails, copy the source file
final newFile = await sourceFile.copy(newPath);
return newFile;
}
}
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
print(directory.path);
return directory.path;
}
Future<String> dirpath() async {
var path = await ExtStorage.getExternalStoragePublicDirectory(ExtStorage.DIRECTORY_DOWNLOADS);
print("download: "+path);
return path;
}
Future<File> get _localFile async {
final path = await _localPath;
final extpath = await dirpath();
File src = File('$path/counter.txt');
String desc = '$extpath/counter.txt';
var status = await Permission.storage.status;
if (!status.isGranted) {
await Permission.storage.request();
var file = await moveFile(src,desc);
print("file::: ${file.path}");
}
// Permission.storage.request();
File('$extpath/counter.txt');
return File('$path/counter.txt');
}
Future<String> readContent() async {
try {
final file = await _localFile;
// Read the file
String contents = await file.readAsString();
// Returning the contents of the file
return contents;
} catch (e) {
// If encountering an error, return
return 'Error!';
}
}
Future<File> writeContent() async {
final file = await _localFile;
// Write the file
return file.writeAsString('Hello Folk');
}
@override
void initState() {
super.initState();
askedPermission();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Reading and Writing data')),
body: Center(
child: Column(
children: [
Text(
'Data read from a file: \n $data',
),
SizedBox(height: 20,),
FlatButton(onPressed: (){
setState(() {
// write();
writeCsv();
});
}, child: Text("write")),
],
))
);
}
Future<void> askedPermission() async {
var status = await Permission.storage.status;
print("start Status $status");
if (!status.isGranted) {
await Permission.storage.request();
status = await Permission.storage.status;
if(status.isGranted){
print("location supply!!");
File file = new File('/storage/emulated/0/Download/file.text');
print("creating file");
var file2 = await file.writeAsString("dhddhhd");
var parent = file2.parent;
print("parant: $parent");
/* writeContent();
readContent().then((String value) {
setState(() {
data = value;
});
});*/
}
else{
print("No Permission to write File !!");
}
}
print("end Status $status");
}
Future<void> write() async {
print("location supply!!");
File file = new File('/storage/emulated/0/Download/file.text');
print("creating file");
var file2 = await file.writeAsString("dhddhhd");
var parent = file2.parent;
print("parant: $parent");
}
Future<void> writeCsv() async {
List<int> bytes = utf8.encode(byteStr);
Uint8List bytes8 = Uint8List.fromList(bytes);
print("-------------- List<int> ------------");
print(bytes);
print("-------------- END List<int> ------------");
print("\n\n-------------- List<Unit8> ------------");
print(bytes8);
print("\n\n-------------- END List<Unit8> ------------");
print("location supply!!");
File file = new File('/storage/emulated/0/Download/file.csv');
File filety = new File('/storage/emulated/0/Download/file2.csv');
print("creating file");
var file2 = await file.writeAsBytes(bytes);
var file3 = await filety.writeAsBytes(bytes8);
var parent = file2.parent;
print("parant: $parent");
}
}
答案 0 :(得分:0)
我可以通过使用 base64 解码来创建 CSV 文件。 如果有人需要,请在此处留下答案。
var decode = base64.decode(JsonResponseString);
var csvStr = utf8.decode(decode);
print(csvStr.toString());
print("creating file...");
File file =
new File('/storage/emulated/0/Download/${currentFile.fileName}.csv');
file.writeAsString(csvStr);