I am uploading a file in dropbox by this method:
public void upload() {
FileInputStream inputStream = null;
try {
File file = new File(Environment.getExternalStorageDirectory()
.toString() + "/write.txt");
inputStream = new FileInputStream(file);
Entry newEntry = mDBApi.putFile("/write.txt", inputStream,
file.length(), null, null);
Log.i("DbExampleLog", "The uploaded file's rev is: " + newEntry.rev);
} catch (DropboxUnlinkedException e) {
// User has unlinked, ask them to link again here.
Log.e("DbExampleLog", "User has unlinked.");
} catch (DropboxException e) {
Log.e("DbExampleLog", "Something went wrong while uploading.");
} catch (FileNotFoundException e) {
Log.e("DbExampleLog", "File not found.");
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
}
}
}
}
but when already this file exists in the folder then the file get renamed to write(1).txt
but I want that if the file already exists in the dropbox share folder then it will be replaced. What should I do now?
You can use mDBApi.putFileOverwrite instead of mDBApi.putFile
Related
i have saved some data in a .txt in the app file directory using this way, the name is in FILE_NAME variable which is string.
public void save()
{
mEditText = findViewById(R.id.data);
String text = mEditText.getText().toString();
FileOutputStream fos = null;
try {
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(output.getBytes());
Toast.makeText(this, "Saved to " + getFilesDir() + "/" + FILE_NAME,
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Now i want to get the data from this .txt file and i'm trying to find a way to do it. It has a lot of lines of information in so it will be convenient if i can send the file directly to my email, upload it on google drive or any other way to get it completely.
I am attempting to save to long term file storage in android as well as create a new file in the process. This code keeps crashing with minimal helpful logcat.
Thanks.
public void save (String text) {
FileOutputStream fos = null;
try {
fos = openFileOutput("logfile.txt", MODE_PRIVATE);
fos.write(text.getBytes());
} catch (FileNotFoundException e)
{} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null)
{
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
I expect it to create a file called logfile.txt and print text to it but instead it crashes.
Try something alike this, in order to get a FileOutputStream from a File in tmp / private storage:
// File file = File.createTempFile("logfile", ".txt");
File file = new File(getFilesDir(), "logfile.txt");
FileOutputStream fos = new FileOutputStream(file);
The resulting path should be /data/data/tld.domain.package/files/logfile.txt.
file.getAbsolutePath() has the value.
See Save a file on internal storage.
This properties file in PROJECT/resources/properties.properties can be read and show its content with:
public void showFileContent(String fileName){
File file = new File (fileName);
FileInputStream input = null;
if(file.exists()){
int content;
try {
input = new FileInputStream(fileName);
while ((content = input.read()) != -1) {
System.out.print((char) content);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (input != null) {
try {
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}else{
System.out.println("Error : properties File " + fileName + " not found");
}
}
But it fails with a null pointer exception at properties.load with that code
public Properties getProperties(String fileName, Properties properties){
File file = new File (fileName);
InputStream input = null;
if(file.exists()){
try {
input = new FileInputStream(fileName);
properties.load(input);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}else{
System.out.println("Error : properties File " + fileName + " not found");
}
return properties;
}
even when input is set to
input = this.getClass().getClassLoader().getResourceAsStream(fileName)
anyone knows why that can be for a properties text file at the same path for both methods ?
Since the first code snippet works, it seems properties is passed as null to the getProperties() method, resulting in NullPointerException.
Ideally, we shouldn't be passing the properties at all. We just need to create a new object and return it.
i have a problem with download a textfile. I have no file contents after the dowload, the downloaded file is empty. Android and Core Api
#Override
protected Boolean doInBackground(Void... params) {
FileOutputStream outputStream = null;
try {
File fileDown = new File(LOCAL_PATH_DOWNLOAD);
outputStream = new FileOutputStream(fileDown);//
DropboxAPI.DropboxFileInfo info = mApi.getFile(DROPBOX_FILE_DIR_DOWNLOAD, null, outputStream, null);
return false;
} catch (Exception e) {
System.out.println("Something went wrong: " + e);
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
}
}
}
return false;
}
What do i wrong? Thanks for help
i use above code to do this
File file= new File("/sdcard/New_csv_file.csv");
OutputStream out= null;
boolean result=false;
try {
out = new BufferedOutputStream(new FileOutputStream(file));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
DropboxFileInfo info = mApi.getFile("/photos/New_csv_file.csv", null, out, null);
Log.i("DbExampleLog", "The file's rev is: " + info.getMetadata().rev);
Intent JumpToParseCSV=new Intent(context,ParseCSV.class);
JumpToParseCSV.putExtra("FileName", file.getAbsolutePath());
Log.i("path", "FileName"+ file.getAbsolutePath());
((Activity) context).finish();
context.startActivity(JumpToParseCSV);
result=true;
} catch (DropboxException e) {
Log.e("DbExampleLog", "Something went wrong while downloading.");
file.delete();
result=false;
}
return result;
Like in the method i have attached i have used practiceData.txt i am getting same results while using just practiceData in file constructor so is it ok to use file without any extension or txt is better?
private void saveData(String data) {
File file = new File(this.getFilesDir(), "practiceData.txt");
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(data.getBytes());
saveStatus = "Data was successfully saved.";
} catch (java.io.IOException e) {
e.printStackTrace();
saveStatus = "Error occurred: " + e.toString();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
It doesn't matter what file extension you use, it just tells the OS how to open the file. So yes, you can use no extension and it will work just as well.
If you intend the file to be opened manually via another application, it may be helpful to use a standard extension however.