i'm trying read in text file of words array or string able access each word. separating words if comes in 1 long string no issue have having real problems reading file. using android studio , file under assets folder (app/app/src/main/assets/wordseasy.txt) far have following code:
public string[] getwords(string difficulty){ arraylist<string> wordlist = new arraylist<string>(); string[] words = new string[wordlist.size()]; string wordlist = getquestions() //location of error //haven't finished here consist of transferring words in string array return words; } private static string getquestions(context ctx,string file_name) { assetmanager assetmanager = ctx.getassets(); bytearrayoutputstream outputstream = null; inputstream inputstream = null; try { inputstream = assetmanager.open(file_name); outputstream = new bytearrayoutputstream(); byte buf[] = new byte[1024]; int len; try { while ((len = inputstream.read(buf)) != -1) { outputstream.write(buf, 0, len); } outputstream.close(); inputstream.close(); } catch (ioexception e) { } } catch (ioexception e) { } return outputstream.tostring(); } i don't know how context in situation , appreciate help. if know alternative way read file string or array please share it.
check this, working me
import android.content.context; import android.content.res.assetmanager; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.widget.textview; import android.widget.toast; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; import java.util.list; public class mainactivity extends appcompatactivity { list<string> wordlist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); wordlist = getwordsfromfile("text_file.txt", mainactivity.this); toast.maketext(this,"word size:"+wordlist.size(),toast.length_short).show(); textview textview = (textview) findviewbyid(r.id.tv_words); for(string word : wordlist){ textview.append("\n"+word); } } public list<string> getwordsfromfile(string textfilename, context context){ list<string> wordlist = new arraylist<>(); string textstr = ""; assetmanager = context.getassets(); try { inputstream = am.open(textfilename); textstr = getstringfrominputstream(is); } catch (ioexception e) { e.printstacktrace(); } if(textstr !=null){ string[] words = textstr.split("\\s+"); (int = 0; < words.length; i++) { words[i] = words[i].replaceall("[^\\w]", ""); wordlist.add(words[i]); } } return wordlist; } private static string getstringfrominputstream(inputstream is) { bufferedreader bufferedreader = null; stringbuilder stringbuilder = new stringbuilder(); string line; try { bufferedreader = new bufferedreader(new inputstreamreader(is)); while ((line = bufferedreader.readline()) != null) { stringbuilder.append(line); } } catch (ioexception e) { e.printstacktrace(); } { if (bufferedreader != null) { try { bufferedreader.close(); } catch (ioexception e) { e.printstacktrace(); } } } return stringbuilder.tostring(); } }
Comments
Post a Comment