How to display strange characters in Android - java

Could someone shed me light on why strange characters does not appear in Android listView?
I have a text file with this
And I got this
Here is my code
ArrayList<String> list = new ArrayList<String>();
InputStream inStream = getResources().openRawResource(R.raw.emoticons);
InputStreamReader inputReader = null;
if (inStream != null)
{
try {
inputReader = new InputStreamReader(inStream, "UTF-8");
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
int c, i=0;
//int i=0;
char [] cb = new char[1];
byte [] buf = new byte[100];
String line = "";
int ble = -1;
try {
ble = inputReader.read(cb, 0, 1);
}catch (IOException e){
e.printStackTrace();
}
while (ble > -1)
{
if(cb[0] == '\r' || cb[0] == '\n')
{
try{
line = new String(buf, 0, i, "UTF-8");
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
i=0;
list.add(line);
}
else
{
buf[i++] = (byte) cb[0];
}
try {
ble = inputReader.read(cb, 0, 1);
}catch (IOException e){
e.printStackTrace();
}
}
}
String[] emoticons = list.toArray(new String[list.size()]);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, emoticons);
listview.setAdapter(adapter);

It looks like you are trying read a file line-by-line. One way to do that is using the BufferedReader class:
ArrayList<String> list = new ArrayList<String>();
InputStream inStream = getResources().openRawResource(R.raw.emoticons);
BufferedReader inputReader = null;
if (inStream != null)
{
try {
inputReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
String line;
while ((line = inputReader.readLine()) != null) {
list.add(line);
}
// handle exceptions..
} finally {
if (inputReader != null) inputReader.close();
}
}
String[] emoticons = list.toArray(new String[list.size()]);

Related

Read large file data using buffered reader in android

hi i want to read large remote file into string using buffered reader.but i got half data of remote file.
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputstream),8*1024);
StringBuilder sb = new StringBuilder(999999);
String line;
while ((line = reader.readLine()) != null) {
Log.e("Line is ",line);
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.e("Content: ", sb.toString());
How to get full data of remote file?
It seems there is nothing wrong with your code but you can try it this way:
private String ReceiveData(InputStream inputstream){
StringBuilder sb = null;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader( inputstream),8*1024);
sb = new StringBuilder();
String str;
int numRead = 0;
try {
if (bufferedReader!=null) {
if (bufferedReader.ready()) {
try {
while ((numRead = bufferedReader.read()) >= 0) {
//convert asci to char and then to string
str = String.valueOf((char) numRead);
if ((str != null)&& (str.toString() != "")) {
sb.append(str);
}
if (!bufferedReader.ready()){
//no more characters to read
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
//loop exited, check for null
if (sb != null) {
return sb.toString();
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return "";
}
Hope I helped.

how do i get a single line from a txt file on android studio

My Code to get file content:
private String readTxt(){
InputStream inputStream = getResources().openRawResource(R.raw.text);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
but i want only one specific line on that file to be extracted.
Use BufferedReader instead of ByteArrayOutputStream.
String readLine(int line) throws IOException {
InputStream in = getResources().openRawResource(R.raw.text);
BufferedReader r = new BufferedReader(new InputStreamReader(in));
try {
String lineStr = null;
int currentLine = 0;
while ((lineStr = r.readLine()) != null) {
if (currentLine++ == line) {
return lineStr;
}
}
} finally {
if (r != null) {
r.close();
}
}
throw new IOException("line " + line + " not found");
}

Arabic RSS can't be read in java it gives me symbols

I'm trying to read RSS feed from a URL in java but I don't get Arabic output just bunch of symbols, below is a sample code, it works with English but does not with Arabic...
I tried couple of examples from web and could not solve it.
public static void main(String[] args) {
try {
URL cali = new URL(
"http://services.explorecalifornia.org/rss/tours.php");
URL aljazera = new URL(
"http://www.aljazeera.net/aljazeerarss/3c66e3fb-a5e0-4790-91be-ddb05ec17198/4e9f594a-03af-4696-ab98-880c58cd6718");
InputStream stream = aljazera.openStream();
BufferedInputStream buf = new BufferedInputStream(stream);
StringBuilder sb = new StringBuilder();
while (true) {
int data = buf.read();
if (data == -1) {
break;
} else {
sb.append((char) data);
}
}
System.out.println(sb);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Try to specify the encoding type of the InputStream:
InputStreamReader isr = new InputStreamReader(stream, "UTF-8");
StringBuilder sb = new StringBuilder();
while (true)
{
int data = isr.read();
if (data == -1) {
break;
} else {
sb.append((char) data);
}
}

Buffered reader can't load file?

I'm working on a project for school, where I want to load questions from a file. However, Java is unable to load the file. I am sure the text file is named right and in the right direactory, but here they are:
game>src
game>out>production>game
private static void init(){
BufferedReader br = null;
ArrayList<String> strings= new ArrayList<String>();
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(new File("questions.txt")));
while ((sCurrentLine = br.readLine()) != null) {
strings.add(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
int i = 0;
while(i < strings.size()){
String text = strings.get(i++);
int numAnswers = Integer.parseInt(strings.get(i++));
ArrayList<String> answers = new ArrayList<String>();
for (int i2 = 0; i2 < numAnswers; i2++){
answers.add(strings.get(i++));
}
questions.add(new Question(text, answers));
}
}
Try scr/questions.txt as path.
br = new BufferedReader(new FileReader(new File("src/questions.txt")));

Output only giving me one line

Can anyone point me in the right direction here. I have a method that is supposed to read a file and display the data in that file. I can only get it to display one line. I know it is something simple I am over looking, but my brain is mush and I just keep digging a bigger hole.
public static String readFile(String file) {
String data = "";
if (!new java.io.File(file).exists()) {
return data;
}
File f = new File(file);
FileInputStream fStream = null;
BufferedInputStream bStream = null;
BufferedReader bReader = null;
StringBuffer buff = new StringBuffer();
try {
fStream = new FileInputStream(f);
bStream = new BufferedInputStream(fStream);
bReader = new BufferedReader(new InputStreamReader(bStream));
String line = "";
while (bStream.available() != 0) {
line = bReader.readLine();
if (line.length() > 0) {
if (line.contains("<br/>")) {
line = line.replaceAll("<br/>", " ");
String tempLine = "";
while ((tempLine.trim().length() < 1)
&& bStream.available() != 0) {
tempLine = bReader.readLine();
}
line = line + tempLine;
}
buff.append(line + "\n");
}
}
fStream.close();
bStream.close();
bReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buff.toString();
}
String line = null;
while ((line = bReader.readLine())!=null)
How about doing this with Guava:
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/Files.html
List<String> lines = Files.readLines("myFile.txt", Charset.forName("UTF-8"));
System.out.println(lines);
You'd still have to do a little bit of work to concatenate the <br> lines etc...

Categories