Trying to read an utf-8 encoded file in android...
InputStreamReader reader = new InputStreamReader(assets.open("data.txt"), "UTF-8");
BufferedReader br = new BufferedReader(reader);
String line;
//The line below throws an IOException!!
line = br.readLine();
What's wrong with this code?
It looks like you file is too big, you have to split it onto several files (1048576 bytes maximum for each) or find another way to reduce file size. Here is an article about similar problem http://androidgps.blogspot.com/2008/10/dealing-with-large-resources.html
Related
I need write PDF File, and I use this sample(http://www.vogella.com/tutorials/JavaPDF/article.html) with this version "itextpdf-5.4.1.jar".
This create the PDF file, but when the word has "você" write this "você".
I find this code but has not work:
Document document;
...
...
document.addLanguage("pt-BR");
How set encoding or language to Brasil?
Thanks!
Take a look at my answer to Divide page in 2 parts so we can fill each with different source (this is year another question answered in The Best iText Questions on StackOverflow). In this example, we read a series of text files that are stored in UTF-8. To achieve this, we use this method:
public Phrase createPhrase(String path) throws IOException {
Phrase p = new Phrase();
BufferedReader in = new BufferedReader(
new InputStreamReader(new FileInputStream(path), "UTF8"));
String str;
while ((str = in.readLine()) != null) {
p.add(str);
}
in.close();
return p;
}
If you remove the "UTF8" and if you read that text as if it were ASCII, then you'd get the same behavior you are describing in your question: each byte would be treated as a single character whereas you have characters that require two bytes.
This is not really an iText question. This is a pure encoding question.
This question already has answers here:
How to get FileInputStream to File in assets folder
(2 answers)
Closed 7 years ago.
I know that I can read files from assets using AssetManager, like here
AssetManager assetManager = getAssets();
InputStream is = assetManager.open(filename)
but open() method returns InputStream. So what I should do when I need to work with FileInputStream not with it superclass. Is there a way to get FileInputStream instance by InputStream instance?
No, there is not a way to convert an arbitrary InputStream back to a FileInputStream, as the data source may be of a different nature - something other than a literal File.
Assets are not files on the device, but rather particular chunks of the zip file which is your .apk. The Asset APIs give you access that is file-like in many ways (particularly with regard to input streams), but does not ultimately wrap an individual java.io.File, but rather an engine for extracting data directly from the .apk
In that case you can create a StringBuilder and read whole content of text file in it something like this
StringBuilder buf=new StringBuilder();
InputStream is=getAssets().open(filename);
BufferedReader in= new BufferedReader(new InputStreamReader(is, "UTF-8"));
String str;
while ((str=in.readLine()) != null) {
buf.append(str);
}
in.close();
Note: Things differ based on the file type you try to access; as Chris Stratton wrote as the data source may be of a different nature.
I want to use a text file in my app and I use this code textview1.set Text(R.raw.ludo); but in the app I only see the path of this file I want to use the text that is in it?
The parameter to setText() is a String which is set to it. In your case it is setting it to path of the file in raw folder as that is the value whihc has been assigned to that variable in R.java folder.
If you want to set content, then open the file in your app, read the contents in a string and set that string.
One of the ways to read is:
InputStream inputStream = getResources().openRawResource(R.raw.yourtextfile);
BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(inputStream));
String eachline = bufferedReader.readLine();
while (eachline != null) {
........
}
you need to read the text from file.
Reading this thread can help you :
Reading a plain text file in Java
I'm encountering an odd situation whereby strings that I load from my resource XML file that have Spanish characters in them display correctly in my TextViews, but strings that I'm fetching from a JSON file that I load via HTTP at runtime display the missing char [] boxes
ESPAÑOL for example, when embedded in my XML strings works fine, but when pulled from my JSON is rendered as SPAÃ[]OL, so the Ñ is transformed into a à and a missing char!
I'm not sure at what point I need to intercept these strings and set the correct encoding on them. The JSON text file itself is generated on the server via Node, so, I'm not entirely sure if that's the point at which I should be encoding it, or if I should be encoding the fileReader on the Android side, or perhaps setting the TextView itself to be of some special encoding type (I'm unaware that this is an option, just sort of throwing my hands in the air, really).
[EDIT]
As per ianhanniballake's suggestion I am logging and seeing that the screwy characters are actually showing up in the log as well. However, when I look at the JSON file with a text viewer on the Android file system (it's sitting on the SDCARD) it appears correct.
So, it turned out that the text file was, indeed, encoded correctly and the issue was that I wasn't setting UTF-8 as my encoding on the FileInputStream...
The solution is to read the file thusly:
static String readInput() {
StringBuffer buffer = new StringBuffer();
try {
FileInputStream fis = new FileInputStream("myfile.json");
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
Reader in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
buffer.append((char) ch);
}
in.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
i have the following text in my strings.xml file
\n\nSVG Service Verlags GmbH & Co. KG \n
Schwertfegerstra?e 1-3\n
D-23556 L?beck\n
this is german text.
i need to decode this using utf-8 and then set it as text of a textview.
how do i go about this
thank you in advance.
EDIT:
i have tried the following
String decodedstring = URLDecoder.decode(nodevalue, "UTF-8");
this also does not work. why does this not work?
Some things to check.
Make sure your xml is tagged with the right encoding.
Make sure your xml file is SAVED with the right encoding. Looking at the text you pasted (from a browser?) it looks like the file is already mangled. Schwertfegerstra?e should be Schwertfegerstraße.
When you open the file you need to use an InputStreamReader with the encoding set.
See this page for an example:
http://www.mkyong.com/java/how-to-read-utf-8-encoded-data-from-a-file-java/
The key bit is:
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(fileDir), "UTF8"));