I have a textfile in /sdcard/applit/mytext.txt
I want to push it to parse cloud.Googled a lot but No profit.Please explain completely.Thanx.
private void txtPusher(File dir) throws IOException {
File outputFile;
outputFile=newFile(dir,"MyText\t"+ParseUser.getCurrentUser().getUsername()+".txt");
byte [] b;
b=FileUtils.readFileToByteArray(outputFile);
file=new ParseFile("MyText\t"+ParseUser.getCurrentUser().getUsername()+".txt",b);
file.saveInBackground();
TextPusher Tpusher=new TextPusher(file);
Tpusher.execute();
}
Here dir is the directory I am passing to txtPusher function.I want to know wether output file is that file which I am going to push or another directory or it is creating a new file.but my file is not getting pushed.If i am wrong please share the right way to push the textfile
You can read the contents of a text (.txt) file using the following:
private String readFile(String fileName) {
//Find the directory for the SD Card using the API
File sdcard = new File(Environment.getExternalStorageDirectory() + File.separator + "Inventory_Files/Version/");
// Get the text file
File file = new File(sdcard, fileName);
// Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
} catch (IOException e) {
//You'll need to add proper error handling here
}
return text.toString();
}
As for sending this data to the cloud, this looks to be very well documented on their site.
Related
I am trying to read file.txt file which stored on my SD Card but am unable read it on my app. I provided
"android.permission.READ_EXTERNAL_STORAGE"
permissions on manifest. Not sure where it went wrong. Please see my code below what i tried.
//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"file.txt");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
}
//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);
//Set the text
tv.setText(text);
I created a file on my pc, and I want my app to read from it.
How do I read from that file in my app?
Thanks
Put the file in assets/ (e.g., app/src/main/assets/ in a typical Android Studio project). Then use open() on an AssetManager to get an InputStream on that content. You can get an AssetManager from your Activity or other Context by calling getAssets().
Move your file to card and add path instead of "file.txt"
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"file.txt");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}catch (IOException e) {
//You'll need to add proper error handling here
}
//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);
//Set the text
tv.setText(text);
it example best if you want to read text
This is a continuation from this post but basically I can't access the files that are inside of a folder on my device storage but I can access files that are outside of any folder.
For example displayCurrentBodyTempArray function works when temp.txt is referenced but not when boyTempTest.txt is referenced that is inside the download folder. And as I don't know how to NOT download something, over bluetooth(which I have to do), and have it go to that folder I am confused. I just can't access that folder. Any help is appreciated!
bodyTempInfo function
ArrayList<Double> currentBodyTemp;
public void displayCurrentBodyTempArray(View view) throws FileNotFoundException {
ArrayList<Double> list = new ArrayList<>();
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
//Read text from file
File file = new File(sdcard, "bodyTempTest-1.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
list.add(Double.parseDouble(line));
text.append('\n');
}
br.close();
}
catch(IOException e) {
}
currentBodyTemp = list;
displayBodyTemp("Your Body Temperature Readings are:" + text);
}
displayBodyTemp function
private void displayBodyTemp(String bodyTemp) {
TextView textView = (TextView) findViewById(R.id.bodyTempInfoArray);
textView.setText(bodyTemp);
}
Device Storage screenshot of temp.txt, which is accessible with that code, and bodyTempTest.txt inside of download folder which isn't accessible with that code.
temp.txt
folder
inside folder
I have a text file called words.txt in folder raw within android res folder. I don't know how to get its path to make a new file with the given path. I use the code below, but seems it just doesn't work:
File file = new File("res/raw/words.txt").getAbsoluteFile();
String filePath = file.getAbsolutePath();
File wordFile = new File(filePath);
You can read the raw/words.txt as follows:
// The InputStream opens the resourceId and sends it to the buffer
InputStream is = this.getResources().openRawResource(R.raw.words);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String readLine = null;
try {
// While the BufferedReader readLine is not null
while ((readLine = br.readLine()) != null) {
Log.d("TEXT", readLine);
}
// Close the InputStream and BufferedReader
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
Note: this code must be inside an Activity class, as this.getResources() refers to Context.getResources()
You can't write to your own app's res folder. It is not modifiable.
You can create a file here though:
http://developer.android.com/reference/android/content/Context.html#getFilesDir%28%29
If you want to use words.txt as a baseline, you can read from it and copy it to a modifiable file at runtime.
how to read a specific file from sdcard. i have pushed the file in sdcard through DDMS and i am trying to read it though this way but this give me exception. can anybody tell me how to point exactly on that file?
my code is this.
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
FileInputStream iStream = new FileInputStream(path);
You are trying to read a directory... what you need is the file! Do something like this... then, you can read the file as you want.
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
To read any file(CSV in my case) from External Storage, we need a path for it,once you have path you can do like this...
void readFileData(String path) throws FileNotFoundException
{
String[] data;
File file = new File(path);
if (file.exists())
{
BufferedReader br = new BufferedReader(new FileReader(file));
try
{
String csvLine;
while ((csvLine = br.readLine()) != null)
{
data=csvLine.split(",");
try
{
Toast.makeText(getApplicationContext(),data[0]+" "+data[1],Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Log.e("Problem",e.toString());
}
}
}
catch (IOException ex)
{
throw new RuntimeException("Error in reading CSV file: "+ex);
}
}
else
{
Toast.makeText(getApplicationContext(),"file not exists",Toast.LENGTH_SHORT).show();
}
}
/*
csv file data
17IT1,GOOGLE
17IT2,AMAZON
17IT3,FACEBOOK*/