I have data come from a device to my flutter application via bluetooth.
I have lists that are displayed one by one on alerts. I want to collect data on all the alerts and then save it on csv file.
The problem is that there are lot of files created and every file contain juste one list this mean that for every alert there is a file created.
this is the code:
setupMode.generateCsv((message) async {
List<List<String>> data = [
["No.", "X", " Y", "Z", "Z"],
[message.data[1], message.data[2], message.data[3], message.data[4]],
];
String csvData = ListToCsvConverter().convert(data);
final String directory = (await getApplicationSupportDirectory()).path;
final path = "$directory/csv-${DateTime.now()}.csv";
print(path);
final File file = File(path);
await file.writeAsString(csvData);
print("file saved");
print(csvData.length);
});
}
I want to collect all the data and store it on one file.
In order to write into one file, there are two things you need to change:
File name:
final path = "$directory/csv-${DateTime.now()}.csv";
This creates a new file every time you call it. You should use the same file name, e.g.:
final path = "$directory/csv-alert-data.csv";
The second change is the write type:
await file.writeAsString(csvData,mode:FileMode.append);
This will append the new data to the end of the existing CSV file.
Related
I found this script that will upload files from google forms response to a folder using the first response (name of applicant) to name the folder.
function onFormSubmit(e) {
const folderId = "###"; // ID of the destination folder
const form = FormApp.getActiveForm();
const formResponses = form.getResponses();
const itemResponses = formResponses[formResponses.length - 1].getItemResponses();
// Prepare the folder.
const destFolder = DriveApp.getFolderById(folderId);
const folderName = itemResponses[0].getResponse();
Logger.log(folderName)
const subFolder = destFolder.getFoldersByName(folderName);
const folder = subFolder.hasNext() ? subFolder.next() : destFolder.createFolder(folderName);
// Move files to the folder.
itemResponses[1].getResponse().forEach(id => DriveApp.getFileById(id).moveTo(folder));
}
I tried using it but it will only upload one file - the one correspnding to the specific question number in the line itemResponses[1].getResponse()
How Do i modify this script so it will upload all of the files in the response regardless of their position without creating multiple folders?
i.e. is it possible to set a range of questions that all the files will upload?
Also, i want to modify the script so that name of the folder will also consist of the response sequence number as a prefix to the name of responder, how is that possible?
I am gathering accelerometer data from my phone using the sensors package, adding that data to a List<AccelerometerEvent>, and then combining that data into a (csv) String so I can use file.writeAsString() to save this data as a csv file. The problem I am having is that it takes too long to combine the data into a string.
For example:
List length : 28645
Milliseconds to combine into csv string: 113580
Code:
for (AccelerometerEvent event in history) {
dataString = dataString + '${event.timestamp},${event.x},${event.y},${event.z}\n';
}
What would be a more efficient way to do this?
Should I even combine the data into a string, or is there a better way to save this data to a file?
Thanks
Create a file object
write first line with column names, and after that each row (after \n) will be an event
See: FileMode.append
Will add new strings without replacing existing string in file
File file = File('events.csv');
file.writeAsStringSync('TIMESTAMP, X, Y, Z\n', mode: FileMode.append);
for (AccelerometerEvent event in history) {
final x = event.x;
final y = event.y;
final z = event.z;
final timestamp = event.timestamp;
String data = '$timestamp, $x, $y, $z';
file.writeAsStringSync('$data\n', mode: FileMode.append);
}
I am getting the data from my JSON file through this code:
var content = await rootBundle.loadString("json/story1.json");
decodedContent = json.decode(content);
And I would like to be able to store an integer inside the story1.json file.
I already have the path of the Excel file, but whenever I call save method, the program writes new content to UTC.xlsx file.
string pathString = "C:\Users\ADMIN-PC\Documents\SUMMER 2018\PRN192\CreateFile\SubFolder\UTC.xlsx";
ef.Save(pathString);
var path = #"C:\Users\ADMIN-PC\Documents\SUMMER 2018\PRN192\CreateFile\SubFolder\UTC.xlsx";
// Load the workbook:
var workbook = ExcelFile.Load(path);
// Manipulate the workbook …
// … and save it over the original
workbook.Save(path);
I have built a Server that you can upload files to and download, using Eclipse, servlet and jsp, it's all very new to me. (more info).
Currently the upload system works with the file's name. I want to programmatically assign each file a random key. And with that key the user can download the file. That means saving the data in a config file or something like : test.txt(file) fdjrke432(filekey). And when the user inputs the filekey the servlet will pass the file for download.
I have tried using a random string generator and renameTo(), for this. But it doesn't work the first time, only when I upload the same file again does it work. And this system is flawed, the user will receive the file "fdjrke432" instead of test.txt, their content is the same but you can see the problem.
Any thoughts, suggestions or solutions for my problem?
Well Sebek, I'm glad you asked!! This is quite an interesting one, there is no MAGIC way to do this. The answer is indeed to rename the file you uploaded. But I suggest adding the random string before the name of the file; like : fdjrke432test.txt.
Try this:
filekey= RenameRandom();
File renamedUploadFile = new File(uploadFolder + File.separator+ filekey+ fileName);
item.write(renamedUploadFile);
//remember to give the user the filekey
with
public String RenameRandom()
{
final int LENGTH = 8;
StringBuffer sb = new StringBuffer();
for (int x = 0; x < LENGTH; x++)
{
sb.append((char)((int)(Math.random()*26)+97));
}
System.out.println(sb.toString());
return sb.toString();
}
To delete or download the file from the server you will need to locate it, the user will input the key, you just need to search the upload folder for a file that begins with that key:
filekey= request.getParameter("filekey");
File f = new File(getServletContext().getRealPath("") + File.separator+"data");
File[] matchingFiles = f.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith(filekey);
}
});
String newfilename = matchingFiles[0].getName();
// now delete or download newfilename