Why the txt file can not find in the Android device? - android

I am developing in the Android. I found a sample code. It open the file by the following code:
private static final String TEST_DATA_FILE_NAME = "test.txt";
FileInputStream fin = activity.openFileInput(TEST_DATA_FILE_NAME);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
and use String text = br.readLine(); to read.
I use following code to get the file path:
File outFile =activity.getDatabasePath(MESH_DATA_FILE_NAME);
Log.i(TAG, "outFile.getPath() = " + outFile.getPath());
And it show the following log:
outFile.getPath() = /data/data/com.test.app.test/databases/test.txt
I want to get the test.txt and send to computer via some file manager App.
But I can not find the test.txt , I also can not find the above path.
Why I can not fine the file ?
How to get the test.txt file?
Thanks in advance.

If the file is on the SDcard you can read it
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "/somefolder/file.txt";
File myFile = new File(baseDir + File.separator + fileName);
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
String filecontent = aBuffer;
myReader.close();

Related

How do I insert new line in EditText field while reading the text from a file

I am trying to create an android app in which it will take the contents of the file which is in assets, copy the contents to the file in filestorage. Then while displaying, it will read the file in filestorage and will append the lines in edittext.
The problem i am facing is empty lines are not being read.
Following is the code snippet in which i am reading the contents of asset file and copying it in file "current.txt" which will be stored in filestorage.
reader = new BufferedReader(new InputStreamReader(getAssets().open("pravachan.txt")));
ContextWrapper contextWrapper=new ContextWrapper(getApplicationContext());
File directory= contextWrapper.getDir("FileStorage",Context.MODE_PRIVATE);
File myInternalFile=new File(directory,"current.txt");
FileWriter filewriter = new FileWriter(myInternalFile);
BufferedWriter out = new BufferedWriter(filewriter);
String mLine = reader.readLine();
while (mLine != null)
{
//process line
if(mLine.isEmpty())
{
out.write(" ");
String str = System.getProperty("line.separator");
out.write(str);
}
else
out.write(mLine);
mLine = reader.readLine();
}
out.close();
Following is the code snippet in which i am appending the edittext with contents of "current.txt" file
ContextWrapper contextWrapper=new ContextWrapper(getApplicationContext());
File directory= contextWrapper.getDir("FileStorage", Context.MODE_PRIVATE);
File myInternalFile=new File(directory,"current.txt");
FileInputStream fis=new FileInputStream(myInternalFile);
BufferedReader reader=new BufferedReader(new InputStreamReader(fis));
String mLine = reader.readLine();
TextView tv = (TextView)findViewById(R.id.editText);
while(mLine != null)
{
if(mLine.isEmpty()) {
tv.append(" ");
String str = System.getProperty("line.separator");
tv.append(str);
}
else
tv.append(mLine);
mLine = reader.readLine();
}
I don't want empty lines to be skipped.I want the file as it is to be displayed in Edittext. Please Help. Thanks in advance!
I got the answer!
Instead of doing out.write(str) in which str is the line separator, I did out.newline(). It worked!

Reading data from downloaded .txt file downloaded multiple times in Android app

I'm developing an app in which I'm sending a .txt file from one end by attaching it with gmail. Everytime this file is sent, its name is data.txt. When this file is downloaded at the other end, on the first download its name is the same, i.e. data.txt. However, when another file is sent with the same name, the name of the file at the receiveing end becomes data-1.txt, data-2.txt etc. And because of this, I'm not able to read the proper file. Please could someone give me some suggestions to solve this problem? The sending and receiving code is given below: SEND
bSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String fileName = "data";
String toWrite = enterText.getText().toString();
FileOutputStream fos;
try {
String path = Environment.getExternalStorageDirectory().toString();
Log.v("path", path);
File myFile = new File("" + path + "/" + fileName + ".txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(toWrite);
Log.v("file written", toWrite);
myOutWriter.close();
fOut.close();
Uri u1 = null;
u1 = Uri.fromFile(myFile);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "MPPT Configuration Data");
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/html");
startActivity(sendIntent);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
READ:
bRead.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String fileName = "data";
StringBuffer stringBuffer = new StringBuffer();
String aDataRow = "";
String aBuffer = "";
try {
File myFile = new File("/storage/sdcard0/download/" + fileName + ".txt");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
myReader.close();
Log.v("read data", "" + aBuffer);
tvData.setText(aBuffer);
}catch (IOException e2) {
e2.printStackTrace();
}
}
});
I found a possible solution. I can link the file manager (external app) from where the user can pick out whichever file he wants to be read.
Thanks #greenapps fir the idea of displaying a list of files.
You can get all the file by using regex ,then process the file by following way:
1.if only one file found,read it;
2.if more than one file found, compare and read the file which last number is biggest
but this solution still has one problem,if has file data.txt and data-3.txt ,the file we want to read may become data-2.txt,but what we really read is data-3.txt.
Or,maybe you can get the file you want by judging file established time.

uploading docx,doc and pdf file to sharepoint online from native android app

I am developing an android app to remotely access sharepoint online.I am able to upload files using http put method and view them but i am not able to open them.I feel i am not specifying the proper content type.Here's my code:
try {
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard, "gps.doc");
FileInputStream fIn = new FileInputStream(file);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
System.out.println(aBuffer + "content of the file");
HttpPut httpupld = new HttpPut("http://mysite.sharepoint.com/Sites/Documents/gps.doc");
StringEntity entity1 = new StringEntity(aBuffer, "HTTP.UTF_8");
httpDel.setEntity(entity1);
System.out.println(title);
HttpResponse rsp = http2.execute(httpupld);
respcode=rsp.getStatusLine().getStatusCode();
System.out.println(respcode);
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
Any help is appreciated.Thankyou
You can't open (but view) doc in Office Web Apps either on SharePoint Online or if you have it installed on-premises.
Convert your office documents to the office 2007/2010 standards before uploading them, that's docx, xlsx, pptx ...

Problems reading text from file

I'm trying to read some text from a .txt file, here's my code:
String filePath = bundle.getString("filepath");
StringBuilder st = new StringBuilder();
try {
File sd = Environment.getExternalStorageDirectory();
File f = new File(sd, filePath);
FileInputStream fileis = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(
fileis));
String line = new String();
while ((line = buf.readLine()) != null) {
st.append(line);
st.append('\n');
}
Log.i("egor", "reading finished, line is " + line);
} catch (FileNotFoundException e) {
Log.i("egor", "file not found");
} catch (IOException e) {
Log.i("egor", "io exception");
}
reader.setText(st.toString());
The text looks like this:
This is a sample text to test
The .txt file is created in Windows notepad.
And here's what I'm getting:
What's wrong with my code? Thanks in advance.
Is the file in utf-8 (unicode) format? For some reason, Notepad always adds a byte-order mark to unicode files, even when the byte-order is irrelevant. When interpreted as ASCII or ANSI, the BOM will be seen as several characters. It's possible this is what's causing your problem.
If so, the solution is to use a more competent text editor than Notepad, or write code that checks for a BOM first in all unicode files.
If none of this makes sense to you, try googling 'unicode' and 'byte-order mark'.
Wrap a FileReader object in the BufferedReader object instead.
http://download.oracle.com/javase/1.4.2/docs/api/java/io/FileReader.html
File sd = Environment.getExternalStorageDirectory();
File file = new File(sd, filePath);
BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
st.append(line);
st.append("\n");
}
br.close();
Try with the folowing code
File f = new File(str);
FileInputStream fis = new FileInputStream(f);
byte[] mydata1 = new byte[(int) f.length()];
fis.read(mydata1);
System.out.println("...data present in 11file..."+new String(mydata1));

Trying to Open a Text File from Non Default Folder in Android

I'm saving a file to a non-default spot in Android and am trying to be able to open it to load information from it but cannot find a way to specify the path to the file...
Here is how I am saving the file...
File directory = new File(path + "/Android/data/com.etechtour/save_data/");
directory.mkdirs();
extStorageDirectory = path.toString() + "/Android/data/com.etechtour/save_data/";
File file = new File(extStorageDirectory, "pumpItUpGas.txt");
try
{
outStream = new FileOutputStream(file);
OutputStreamWriter out = new OutputStreamWriter(outStream);
out.write("Record#: " + record + ", Nickname: " + nickname + ", Year: " + year + ", Make: " + make + ", Model: " + model);
out.flush();
out.close();
outStream.flush();
outStream.close();
}
Now I've successfully saved the file to this location, but when trying to access it I can't figure out ANYTHING that works to set the path to load the file. I've looked around everywhere online and can't seem to find anything that works except using the default save position. Here's how I'm trying to load but am either forceclosing or getting null pointer exceptions
try
{
String filename = Environment.getExternalStorageDirectory().toString() + "/Android/data/com.etechtour/save_data/pumpItUpGas.txt";
InputStream in = openFileInput("pumpItUpGas.txt");
//FileReader fileReader = new FileReader(file);
//FileInputStream fileInput = new FileInputStream(path);
if (in != null)
//if (fileReader != null)
{
InputStreamReader temp = new InputStreamReader(in);
//InputStreamReader tmp = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(temp);
StringBuffer buf = new StringBuffer();
while ((str = reader.readLine()) != null)
{
buf.append(str);
}
//fileInput.close();
in.close();
return str;
}
}
Any help would be greatly appreciated. Thanks
Use FileInputStream or FileReader with your File object, just like normal Java I/O.

Categories

Resources