I am trying to have my app export a .csv file that can be picked up in the download APP. The way I am doing this is through fileoutput
I get remotedir By calling Environment.getExternalStorageDirectory().getAbsolutePath()
* Download CSV
*/
public static void downloadCSV(String filename, String remoteDir) throws Exception{
File dir = new File( remoteDir + "/download/");
dir.mkdirs();
//FileWriter f = new FileWriter("Download/" + filename + ".csv");
File fileObject = new File(dir, filename);
fileObject.delete();
fileObject.createNewFile();
ObjectOutputStream objectOut = null;
FileOutputStream stream = new FileOutputStream(fileObject);
objectOut = new ObjectOutputStream(new BufferedOutputStream(stream));
/* Irrelevant code */
String csv = Helper.getCSV(table.getColumnList(), view);
objectOut.writeChars(csv);
objectOut.close();
}
Whenever I test it on my phone(HTC One S) I don't see the file anywhere. I want my csv file to pop up in the Downloads app, but I'm not sure which directory that represents.
Thanks
You might have to scan the file with the MediaScanner before it will show up, at least that's how you'll be able to see it from your desktop for finding/debugging. See the MediaScanner docs for details.
Related
How to upload files to my Box Sub-Folder using either by subfolder name or ID
Example say I have 2 subfolders(subfolder1 and subfolder2) in my Box, How to upload files to subfolder2 using java sdk.
Can we upload using any new methods.
Successful in uploading files to Box root folder using the code below
BoxFolder bfolder = BoxFolder.getRootFolder(api);
FileInputStream stream= null;
filePath = "c:\\UploadFile.txt";
stream = new FileInputStream(filePath);
fileName = FilenameUtils.getBaseName(filePath.toString());
bfolder.uploadFile(stream, fileName);
You probably need to enumerate the folders till you find subfolder1, then create a new BoxFolder from that. Something like this (edit for compile errors):
BoxFolder bfolder = BoxFolder.getRootFolder(api);
Iterator<BoxFolder.Info> it = bfolder.getChildren().iterator();
for(BoxFolder.Info i : it){
if(i.getName().equals(subfolder1)){
BoxFolder folder = new BoxFolder(api, i.getID());
FileInputStream stream= null;
filePath = "c:\\UploadFile.txt";
stream = new FileInputStream(filePath);
fileName = FilenameUtils.getBaseName(filePath.toString());
folder.uploadFile(stream, fileName);
break;
}
}
I created a file in internal memory in an activity and want to write on it again on next activity but am getting this error.
in first activity:
String id_file = "tt_id";
String key_file = "tt_key";
FileOutputStream outputStream1;
FileOutputStream outputStream2;
try {
outputStream1 = openFileOutput(id_file, Context.MODE_PRIVATE);
outputStream2 = openFileOutput(key_file, Context.MODE_PRIVATE);
outputStream1.write(id.getBytes());
outputStream2.write(key.getBytes());
outputStream1.close();
outputStream2.close();
but in second activity:
FileOutputStream outputStream1;
FileOutputStream outputStream2;
String id="dfg";
String key="khdfks";
outputStream1 = openFileOutput("tt_id",Context.MODE_PRIVATE);
outputStream2 = openFileOutput("tt_key", Context.MODE_PRIVATE);
outputStream1.write(id.getBytes());
outputStream2.write(key.getBytes());
outputStream1.close();
outputStream2.close();
I've just started with android app development, so any help is appreciated. Thank you in advance
I think you need to specify the created file location before read/write. Also make sure the file exists. Else it will throw a file not found exception. To do so try this,
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "test.txt");
file.createNewFile();
if(file.exists())
{
OutputStream outputStream1 = new FileOutputStream(file);
String id="dfg";
outputStream1.write(id.getBytes());
outputStream1.close();
}
I am trying to load some data saved in my property file in an Android Application.
I have put my property file under the src folder. Every time I try to load data from my file it keeps telling me FileNotFoundException open failed ENOENT (No such file or directory).
My Code is as follows:
This code is to save the file (new create)
File file = new File("src/com/example/testphonegap/SilverAngel.properties");
FileOutputStream fileOut = new FileOutputStream(file);
properties.store(fileOut, "Properties");
fileOut.close();
This code is to load data
properties = new Properties();
InputStream is = null;
// First try loading from the current directory
try {
File f = new File("src/com/example/testphonegap/SilverAngel.properties");
is = new FileInputStream(f);
// Try loading properties from the file (if found)
properties.load(is);
GetPersonaliseSettings();
GetUserSettings();
GetFavSettings();
}
catch ( Exception e ) {
is = null;
}
Can you tell me what I am doing wrong please? Is it where the file is saved or am I missing something in my code?
It's an Android Application, so the file is stored on the Android device and not on your computer.
If you want to save the file on the SD card you can write the following:
String root = Environment.getExternalStorageDirectory().toString();
File dir = new File(root + "/subfolderForYourApp");
dir.mkdirs();
This will create a subfolder for your application on the SD card.
To create a new file in this directory:
File file = new File(dir, "SilverAngel.properties");
FileOutputStream fileOut = new FileOutputStream(file);
properties.store(fileOut, "Properties");
fileOut.close();
Don't forget to add the permission to the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
try this
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("com/example/testphonegap/SilverAngel.properties");
properties.load(inputStream);
make sure you don't add "src" before property file name.
Put it in the assets folder and load it from there.
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("SilverAngel.properties");
properties.load(inputStream);
The following code writes a string to a specific file.
String content = "Text To be written on a File";
File file = new File("c:/file.txt");
FileOutputStream foutput = new FileOutputStream(file);
if (!file.exists()) {
file.createNewFile();
}
byte[] c = content.getBytes();
foutput.write(c);
foutput.flush();
foutput.close();
I want to use this code in a Jbutton so every time the user clicks it, it writes the string to a NEW text file NOT OVERWRITE the existed one. I tried to do but I couldn't get the result.
Thank you in advance.
There's a couple of different ways you can get this result, it really depends on the application. The two easiest ways to do this would to be either:
Append the current timestamp to the file name
Use the File API to create a "temp file" in the directory, which is guarenteed to have a unique name
Option 1:
String baseDir = "c:/";
File newFile = new File(baseDir, "file_" + System.currentTimeMillis() + ".txt");
// do file IO logic here...
Option 2:
String baseDir = "c:/";
File newFile = File.createTempFile("file", ".txt", new File(baseDir));
// do file IO logic here...
If you want to write it to a new file, you have to create a new file. The name of the text file is always file.txt in your case.
Try this:
private int filecounter = 0; // this is the member of your class. Outside the function.
//inside your function
File file = new File("c:/file" + Integer.(filecounter).toString() + ".txt");
// you do something here.
filecounter++;
This way, your files will be stored as file0.txt, file1.txt etc.
I'm using this method to zip and decrypt a file:
AesZipFileEncrypter.zipAndEncrypt
This code:
AesZipFileEncrypter.zipAndEncrypt(new File("C:\Test\Folder\MyFile.txt"), new File("C:\Test\Folder\MyZip.zip"), password, aesEncrypter);
compresses also the folder tree of my file, not just the file. For example:
Adding C:\Test\Folder\MyFile.txt in the created zip file I will find the folders C:\Test\Folder\MyFile.txt also if I would like to have just MyFile.txt in the root folder.
Is it possibile?
This is the solution:
AESEncrypter aesEncrypter = new AESEncrypterBC();
aesEncrypter.init(password, 0);
AesZipFileEncrypter ze=new AesZipFileEncrypter(outputfilename, aesEncrypter);
ze.add(inputfilename,new FileInputStream(inputfilename), password);
ze.close();
In Windows (which it looks like you are in) I ran into the same problem and it seems to depend on where the file is relative to your application. To get around it, I copied the input file to the local directory, ziped and encrypted it, then moved the output file back to the where the output file was intended.
public static File aesEncrypt(String inFileFullPath, String outFileFullPath, String aesPassword) throws IOException{
File inFile = new File(inFileFullPath);
File localInput = new File(inFile.getName());
Files.copy(inFile, localInput);
File outFile = new File(outFileFullPath);
File localOutFile = new File(outFile.getName());
AESEncrypter aesEncrypter = new AESEncrypterBC();
aesEncrypter.init(aesPassword, 255);
AesZipFileEncrypter ze = new AesZipFileEncrypter(localOutFile, aesEncrypter);
ze.add(localInput, aesPassword);
ze.close();
Files.move(localOutFile, outFile);
localInput.delete();
return outFile;
}