Does anybody know the best way to show the "®" symbol when read from a text file in the assets directory? I've tried replacing the "®" symbol with &® or ® or \u00AE, but none of these work.
Is the only solution to define it as html?
Here's the code I'm using to read the text file from assets:
mTextViewTermsConditions = (TextView) findViewById(R.id.textView_terms_conditions);
StringBuffer stringBufferEula = new StringBuffer();
try
{
InputStream inputStream = getAssets().open("eula");
BufferedReader f = new BufferedReader(new InputStreamReader(inputStream));
String line = f.readLine();
while (line != null)
{
Logger.d(line);
stringBufferEula.append(line);
line = f.readLine();
}
}
catch (IOException e)
{
Logger.e(e.toString());
e.printStackTrace();
}
mTextViewTermsConditions.setText(stringBufferEula);
This is an encoding problem. Make sure that your asset file is stored with UTF-8 encoding.
Answer thanks to Ted Hopp above:
Make sure that the text file is encoded UTF-8.
Related
I am trying to open a file to use for my android application, but I am not sure where to save this file so I can use it?
Also how do I use this file once it is saved in the correct place?
It is a .txt file.
How to do this?
To add a file to your application and then read from it at runtime, put it into your res/raw directory and then you can read it like this (where my_file is the name of your file):
try {
Resources res = getResources();
InputStream in_s = res.openRawResource(R.raw.my_file);
BufferedReader reader = new BufferedReader(new InputStreamReader(in_s));
String line = reader.readLine();
while (line != null) { ... }
} catch (Exception e) {
e.printStackTrace();
}
i have been experiencing problem reading data from text files which resides in my internal storage of my mobile phone. My code is as below:
public void recommend(View view) {
Context context = getApplicationContext();
String filename = "SendLog.txt";
TextView tv = (TextView)findViewById(R.id.textView134);
try {
FileInputStream fis = context.openFileInput(filename);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line).append("\n");
tv.getText();
tv.setText(line);
}
} catch (IOException e) {
e.getStackTrace();
}
I'm not getting any data input to my mobile phone. My "SendLog.txt" file reside in /storage/emulated/0/SendLog.txt directory. Anyone can assist me? I want to read everything from the text file and display in textView134. Anyone can assist?
Thank you in advance!
"/storage/emulated/0/SendLog.txt" is located on the External Storage, not the internal.
Instead of context.openFileInput(filename) you should be using Environment.getExtenalStorageDirectory() for example,
FileInputStream fis = new FileInputStream(new File(Environment.getExtenalStorageDirectory(), filename));
Though you will need to handle possibilities such as the External Storage being unavailable / unmounted.
You will also need the manifest permission to read external storage, or alternately to write it if you plan to do that as well (you only need one of the two, as write implies read).
You are building a string using stringbuilder but instead are settexting line. The result is last line in file, which may be an empty line
I have a csv file in my assets file and would like to read it using opencsv
I successfully to get and read the file but fail in printing it,
and the string are displayed as the screen capture below.
My csv is save as Unicode from excel
My code in reading and logging csv:
AssetManager assetManager = context.getAssets();
try {
InputStream csvStream = assetManager.open("data.csv");
InputStreamReader csvStreamReader = new InputStreamReader(csvStream);
CSVReader csvReader = new CSVReader(csvStreamReader);
String [] nextLine;
Log.d("test","reading csv");
while ((nextLine = csvReader.readNext()) != null) {
// nextLine[] is an array of values from the line
Log.d("test",nextLine.toString() + " etc...");
String[] temp= nextLine.toString().split(",");
for(int i=0; i<nextLine.length;i++){
Log.d("test", nextLine[i]);
}
break;
}
} catch (FileNotFoundException e) {
Log.d("test","fail read csv");
e.printStackTrace();
} catch (IOException e) {
Log.d("test","io exception csv");
e.printStackTrace();
}
the csv file's text
and the screen capture in the logcat:
This depends on the encoding of the file.
With any luck, it's in UTF-8. You need to tell your InputStreamReader to use UTF-8 else it'll probably fall back to ISO 8859_1 (ISO-Latin-1).
You can do this like so:
InputStreamReader csvStreamReader = new InputStreamReader(csvStream, "UTF-8");
If your file is encoded in another format, you'll need to specify that.
Edit: After a re-read, I see you exported from Excel. You'll need to tell Excel to export in UTF-8 instead of whatever it defaults to (likely Windows-1252).
Im developing an app using Andriod Studio and I´ve got some problemns accessing my assets subfolders.
When I try to access a file, say file1, in the assets root folder, like this
AssetManager assetMng = getAssets();
InputStream textoInput;
String path = "file1.txt";
try {
textoInput = assetMng.open(path);
BufferedReader r = new BufferedReader(new InputStreamReader(textoInput));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
} catch (IOException e){
lista.add("Exception: " + e.toString());
e.printStackTrace();
}
I get the text successfuly. But when I change "file1.txt" to "sub\file2.txt" I get an file not found execption.
Anyone has any idea what is going on? Am I missing something?
Thanks!
Android is linux based - use a forward slash not backslash. So, sub/file2.txt
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.