Save json files in android - unity3d

I have my files saved on resources folder and when I try to write other thing it does not work. Could anyone help me?
public void SaveGameData()
{
PlayerSavedData aux = new PlayerSavedData();
aux.allSavedPlayerData = SavePlayerInformation.playerDataList.ToArray<PlayerData> ();
string dataAsJson = JsonUtility.ToJson (aux);
string filePath = Application.persistentDataPath + "playerInformation.json";
File.WriteAllText (filePath, dataAsJson);
}

This is wrong
string filePath = Application.persistentDataPath + "playerInformation.json";
Try this instead
string filePath = Path.Combine(Application.persistentDataPath,"playerInformation.json");
Also note that you need WRITE_EXTERNAL_STORAGE permission

The Resoures folder simply doens't exist any more afer your build. The assets in the Resources folder get packed into the game's archive for assets.
Better you put file in StreamingAssets folder.

Related

Flutter how to remove file extension when i need to put file name into variable

I use p.basename from flutter path library to get file name, but i got it with extension, how to remove extension? also to get path I use getApplicationDocumentsDerectory from path_provider library
File newFile = File(filepath);
String name = p.basename(newFile.path);
this is code I use to get file name
You just use 'basenameWithoutExtension' method like basename.
File newFile = File(filepath);
String name = p.basename(newFile.path);
String nameWithoutExtension = p.basenameWithoutExtension(newFile.path);

Unzip all file without it's folder using Java

Is it possible to unzip all files from the zip folder without its folder?
Example:
zipfolder.zip has two subfolders called folder1(having files like 1.txt, 2.xlsx, 3.pdf) and folder2(having files like 4.txt, 5.pdf)
Note: The source can any type of archive files like .zip, .rar, .tar, .7-zip etc
This is my code:
String sevenZipLocation = "C:\\Program Files\\7-Zip\\7z.exe";
String src = source filepath (zip file)
String target = output path (output path)
String[] command={sevenZipLocation,"x",src,"-o"+target,"-aou","-y"};
ProcessBuilder p = new ProcessBuilder( command );
Process process = p.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
#SuppressWarnings("unused")
String line;
while ((line = br.readLine()) != null){
System.out.println("line1 "+line);
}
process.waitFor();
When I execute this code the output like
unzip folder ----- folder1(having files like 1.txt, 2.xlsx, 3.pdf) and folder2(having files like 4.txt, 5.pdf)
But I want to extract the only file from all folders and the output like
1.txt, 2.xlsx, 3.pdf, 4.txt, 5.pdf in the output path.
Is there any command for that. Thanks.
All you need to change:
String[] command={sevenZipLocation,"e",src,"-o"+target,"-aou","-y","*.*","-r"};
PS. I don't think Java is the best choice to run OS commands. You'll be wasting a lot of time. But if you insist, don't forget there might be an errorstream too.

Ionic 3 Cordova File plugin gives error for copyFile operation

I am trying to copy a file from one dir to another using the copyFile(path, fileName, newPath, newFileName) function. It gives an error like {"code":13, "message":"input is not a directory"}. The documentation has only 12 error code and no 13th. I'd like to know what i did wrong please.
Here is a sample of my actual code.
this.path = "file:///storage/emulated/0/TheFolder/thefile.ext";
this.newPath = "file:///storage/emulated/0/NewFolder";
this.fileCtrl.copyFile(this.path, fileName, this.newPath, newFileName)
this.path must be a directory but your are showing some file name
change your code as follows
this.path = "file:///storage/emulated/0/TheFolder";
this.newPath = "file:///storage/emulated/0/NewFolder";
this.fileCtrl.copyFile(this.path, YOUR_EXISTING_FILE_NAME, this.newPath, NEW_FILE_NAME);
path -Base FileSystem
fileName - Name of file to copy
newPath - Base FileSystem of new location
newFileName - New name of file to copy to (leave blank to remain the same)

Upload Servlet with custom file keys

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

How to read a directory with using InputStream in eclipse plugin developement

I'm developing an eclipse plug-in and I need to traverse a directory and whole content of the directory. I found the method which reads a file in plug-in (bundleresource) as InputStream.
InputStream stream = Activator.class.getResourceAsStream("/dir1/dir2/file.ext");
this method works for files only. I need a way to read directories, list subdirectories and files like File.io.
Thanks.
Do you want to read a resource directory of your plugin? Otherwise you have to traverse a directory and open one stream per file:
String path = "c:\\temp\\";
File directory = new File(path);
if (directory.isDirectory()) {
String[] list = directory.list();
for (String entry : list) {
String absolutePath = path + entry;
System.out.println("processing " + absolutePath);
File file = new File(absolutePath);
if (file.isFile()) {
FileInputStream stream = new FileInputStream(file);
// use stream
stream.close();
}
}
}
If you want to traverse subdirectories as well you should wrap this into a recursive method, check if file is a directory and call the recursive method in this case.