i am reading a file using the following code
`FileReader fr=new FileReader("/mnt/sdcard/content.csv");
BufferedReader in = new BufferedReader(fr);
String reader = "";
while ((reader = in.readLine()) != null){
String[] RowData = reader.split(",");
id = RowData[0];
path = RowData[1];`
and i have also tried using the opencsv class but with both the method i am only able to read the last entry in the file..
what am i missing?can someone explain to me?
FileReader fr=new FileReader("/mnt/sdcard/playlist_record.csv");
BufferedReader in = new BufferedReader(fr);
String reader = "";
while ((reader = in.readLine()) != null){
String[] RowData = reader.split(",");
id = RowData[0];
path = RowData[1];
type= RowData[2];
update= RowData[3];
server= RowData[4];
t1.setText(id);
// t1.append(path);
// t1.append(type);
// t1.append(server);
}
in.close();
this is my code i am using to read the file
Related
Need to parse this file (mixed latin & arabic):
1|حِيمِ
2|الَمِينَ
The file was saved as UTF8 in notepad++, and put in android asset folder.
Expected result: for line1, the entries are "1" and "حِيمِ" (split by "|").
AssetManager manager = context.getAssets();
InputStream inStream = null;
inStream = manager.open("file.txt");
BufferedReader buffer = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
String line = "";
while ((line = buffer.readLine()) != null) {
String lineEnc = URLEncoder.encode(line, "UTF-8");
String[] columns = lineEnc.split("%7C");
if (columns.length>=3) {
Toast toast = Toast.makeText(context, "Line: " + columns[0] + " and " + columns[1], Toast.LENGTH_LONG);
toast.show();
}
}
Actual Result:
columns[0] = "1" ok, but
columns[1] = "%D8%AD%D9..." not Ok, expected "حِيمِ".
How to fix this, or is there better way? Please help. Thanks in advance.
Solved, changing:
while ((line = buffer.readLine()) != null) {
String lineEnc = URLEncoder.encode(line, "UTF-8");
String[] columns = lineEnc.split("%7C");
into
while ((line = buffer.readLine()) != null) {
String[] columns = line.split("\\|");
cant anything find or read about this problem ( it's problem only for me but maybe someone know way to fix it.
i have function which reading from asset folder
public String[] loadFromAsset() throws IOException
{
String TEMPBUFFER = null;
String[] temp;
temp = new String[60];
int tempc = 0;
BufferedReader bReader = new BufferedReader(new InputStreamReader(cont.getAssets().open("myquests.txt")));
String line="";// = bReader.readLine();
StringBuilder sb = new StringBuilder();
while (line != null) {
line = bReader.readLine();
sb.append(line).append("TABTAB");
tempc++;
}
bReader.close();
//convert SB to array here
saveCount(tempc-1);
return temp;
}
Need return array of strings like temp[50] = (1,2,3,4,5,...n+1)
But i can't find a way to convert from stringBuilder to array :(
Please,maybe anyone know it. Tell me
Just need return SB as a array (like temp[]) or convert SB to Array here like sb->convert->temp[]
To convert stringbuilder to string array do the following:
string sbString = sb.toString();
String[] ary = sbString.split("TABTAB");
temp = sb.toString().split("TABTAB"); will split the string and return an array of strings for each line. But I'm not completely sure that this is what you are trying to do...
Hello I get some xml file
They are on UTF-8 so i follow some sample and my code look like this
String text = "";
String str;
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(Path), "UTF-8"));
while ((str = in.readLine()) != null) {
text += str;
}
return text;
And then i try to parse the code with the dom parser
Document doc = parser.getDomElement(result);
And this fail
I have check my xml file with a hexeditor
I have the following charcode before "<": ef bb bf
What have i miss? why getDomElement tell me
Unexpected token (position:TEXT #1:2)
text += str + "\n";
If there was a line break in a tag:
<img
src="smile.jpg"/>
you could get:
<imgsrc="smile.jpg">
And some other cases.
StringBuilder text = new StringBuilder();
try (BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(Path), "UTF-8"))) {
String str;
while ((str = in.readLine()) != null) {
text.append(str).append("\n");
}
} // Does an in.close()
return text.toString();
I would like to connect to a Website, filter some content and then put it in a String but I don´t know how to do this.
public void zahlenLaden (View view) throws Exception {
URL oracle = new URL("http://www.blabla.de");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
//What I have to write here?
}
Declare a String to output to before the while loop:
String output = "";
Then just append to that String in each iteration:
output += inputLine + "\n"; (don't forget the omitted newline)
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine + "\n");
}
then just do sp.toString();
Nikola's answer is OK, just an improvement on the use of StringBuilder:
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine).append("\n");
}
I'm trying to get a csv file from http://download.finance.yahoo.com/d/quotes.csv?s=msft&f=sl1p2 then parse it so that I can get the price and the price changed into an object that sets both properties. Is there a way that I can do this with the android libraries?
Edit: Here's the current state of the union (not working):
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(uri);
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = null;
while ((line = reader.readLine()) != null){
result += line + "\n";
String[] RowData = result.split("\n");
String name = RowData[0];
String price = RowData[1];
String change = RowData[2];
stock.setPrice(Double.parseDouble(price));
stock.setTicker(name);
stock.setChange(change);
}
Try something like this:
//--- Suppose you have input stream `is` of your csv file then:
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] RowData = line.split(",");
date = RowData[0];
value = RowData[1];
// do something with "data" and "value"
}
}
catch (IOException ex) {
// handle exception
}
finally {
try {
is.close();
}
catch (IOException e) {
// handle exception
}
}
Hope this helps.
For the first part:
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://download.finance.yahoo.com/d/quotes.csv?s=msft&f=sl1p2");
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
For the second part, Harry is right, just follow his code, or use some libraries: http://commons.apache.org/sandbox/csv/
CSVReader reader = new CSVReader(** Insert your Reader here **);
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
A better CSV parser handles quoted fields
import android.content.Context;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CSVReader {
private class StringDArray {
private String[] data=new String[0];
private int used=0;
public void add(String str) {
if (used >= data.length){
int new_size= used+1;
String[] new_data=new String[new_size];
java.lang.System.arraycopy( data,0,new_data,0,used);
data=new_data;
}
data[used++] = str;
}
public int length(){
return used;
}
public String[] get_araay(){
return data;
}
}
private Context context;
public CSVReader(Context context){
this.context=context;
}
public List read(InputStream inputStream){
List resultList = new ArrayList();
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String csvLine;
final char Separator = ',';
final char Delimiter = '"';
final char LF = '\n';
final char CR = '\r';
boolean quote_open = false;
while ((csvLine = reader.readLine()) != null) {
//String[] row = csvLine.split(",");// simple way
StringDArray a=new StringDArray();
String token="";
csvLine+=Separator;
for(char c:csvLine.toCharArray()){
switch (c){
case LF: case CR:// not required as we are already read line
quote_open=false;
a.add(token);
token="";
break;
case Delimiter:
quote_open=!quote_open;
break;
case Separator:
if(quote_open==false){
a.add(token);
token="";
}else{
token+=c;
}
break;
default:
token+=c;
break;
}
}
if(a.length()>0 ) {
if(resultList.size()>0){
String[] header_row =(String[]) resultList.get(0);
if(a.length()>=header_row.length) {
String[] row = a.get_araay();
resultList.add(row);
}
}else{
String[] row = a.get_araay();
resultList.add(row);//header row
}
}
}
inputStream.close();
}catch (Exception e){
Toast.makeText(context,"Error : " + e.getMessage(), Toast.LENGTH_LONG).show();
}
return resultList;
}
}
Usage
File file=new File(path);
CSVReader csvReader=new CSVReader(activity.this);
List csv=csvReader.read( new FileInputStream(file));
if(csv.size()>0){
String[] header_row =(String[]) csv.get(0);
if(header_row.length>1){
String col1=header_row[0];
String col2=header_row[1];
}
}
Toast.makeText(activity.this,csv.size() + " rows", Toast.LENGTH_LONG).show();
Sample data used
ID,Name
1,Test Item 1
"2","Test Item 2"
"3","Test , Item 3"
4,Test Item 4