I have a text file in res/raw/stringtest.txt. On each line of this there are different lines of text that need to be put into individual TextViews.
Where can I find the java code to read the text file and then put it into textViews?
I believe you can use a FileInputStream to output the text to the Logcat but that is as far as I know so far. I know you put some code into the class files but I am unsure what it is to make it place the Text into the TextViews by id. Any information or code will be very appreciated.
I would use a BufferedReader, for its readLine method. Something like:
BufferedReader br = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.id.my_raw_file)));
String line = null;
while((line = br.readLine()) != null) {
// update TextView with string
}
Related
I would like to compare content of .txt file that i have in my assets folder with some text on the screen.
Usually when I assert text on the screen I use:
onView(withId(R.id.someId)).check(matches(withText("String")));
is ther any easy way so i can assert it from file?
Also, if you want to shorten your assertions and actions when using Expresso, check this library: https://github.com/SchibstedSpain/Barista (disclaimer: I'm a contributor).
It contains a set of quick actions and assertions that make the tests much more readable.
Here is a code to read text from text file.
StringBuilder buf=new StringBuilder();
InputStream json=getAssets().open("book/contents.json");
BufferedReader in=
new BufferedReader(new InputStreamReader(json, "UTF-8"));
String str;
while ((str=in.readLine()) != null) {
buf.append(str);
}
in.close();
now compare your string from assets to you screen text.
buf.toString().equals("your text here");
Is it possible to read a textfile on sdcard during runtime of an UIautomator test? As in an android application, using getExternalDirectory() etc. to create a File-object pointing to the actual file. Is it possible to send a command using getRuntime().exec("cmd"), if so, how? Or is there an easier way to simply access the device:s sdcard and read a file into the test?
The goal is to throughout the test send parameters to the test. So the test will perform certain actions, then continously look for a change on a file on the devices sdcard, and if so, read that line, and continue to perform actions. So therefor a way to read a file, and check certain things, is needed.
Or is there perhaps another way to pass information into the test during runtime? I know it can be done at the start of the testrun, but not during testrun.
I use below code inside uiautomator code to read text files.instead of
public void FileRead(String file_location) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file_location));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
//you can do whatever you want here or return String
} finally {
br.close();
}
}
This should be straight forward but for some reason when I try to count words in a file after I download it to my SD Card, the number seems to be off. Also the more occurrences there are, the further my result seems to be off. I use Microsoft Word to verify the number of occurrences (using ignore case and whole word only). To test the number of occurrences, I use the "the_counter" variable below. I also verified there is nothing wrong with download & the FULL file is downloaded to my SD card. This is driving me nuts -- I'm thinking Word cannot be wrong here so what could possibly be wrong with my code below?
Could it be white space or special chars in the file causing the problem --is there a way to clean the file to verify this?
//Find the directory for the SD Card using the API
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,TEMP_FILE);
//Read text from file
//StringBuilder text = new StringBuilder();
m_tree = new Tree();
int i=0;
BufferedReader br = null;
long the_counter=0;
try {
br = new BufferedReader(new FileReader(file));
String line;
String []arLine;
while ((line = br.readLine()) != null) {
//get each word in line
if(line.length()==0)
continue;
arLine = line.split("\\s+");
//now add each word to search tree
for(i=0;i< arLine.length;++i){
m_tree.insert(arLine[i]);
if(arLine[i].equalsIgnoreCase("a"))
++the_counter;
}
}
m_sTest = Long.toString(the_counter) ;
br.close();
I edited my code to read in each character per line and create words manually. and I STILL GET THE SAME RESULT.
br = new BufferedReader(new FileReader(file));
String line;
String []arLine;
StringBuilder word = new StringBuilder();
while ((line = br.readLine()) != null) {
//check for word at end of last line
if(word.length()>0){
m_tree.insert(word.toString());
word.setLength(0);
}
char[] lineChars = new char [line.length()];
line.getChars(0,line.length(),lineChars,0);
for(char c: lineChars){
if(c== ' '){
//if we have a word then store and clear then move on
if(word.length()>0){
m_tree.insert(word.toString());
word.setLength(0);
}
}
else{
word.append(c);
}
}
This is issue was that I was not accounting for special characters in between words: i.e:
this-is-four-words and not one . I'm not even sure that is proper grammar or writing but it was in this file and it certainly threw off my count.
I've been researching about how diablo 2 dynamically generates loot, and I thought it'd be fun to create a fun app that will randomly generate items using this system.
I currently have code which I believe should read the entire txt file, but it's not parsed.
It looks like:
private void itemGenerator() {
int ch;
StringBuffer strContent = new StringBuffer("");
InputStream fs = getResources().openRawResource(R.raw.treasureclass);
// read file until end and put into strContent
try {
while((ch = fs.read()) != -1){
strContent.append((char)ch);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
An example in the text file would look something like:
Treasure Class Item1 Item2 Item3
tc:armo3 Quilted_Armor Buckler Leather_Armor
tc:armo60a Embossed_Plate Sun_Spirit Fury_Visor
tc:armo60b Sacred_Rondache Mage_Plate Diadem
So what I'm thinking right now is putting each row into an array with StringTokenizer delimited by \n to get each row. Then somehow do it again with tab-delimited for each item in the array and put it into a 2D array?
I haven't coded it yet because I think there's a better way to implement this that I haven't been able to find, and was hoping for some helpful input on the matter.
For anyone actually interested in knowing how the item generation works, their wiki page, http://diablo2.diablowiki.net/Item_Generation_Tutorial, goes very in-depth!
I think you are facing problem in distinguishing between each lines that are read-out from file. In order to read the file line-by-line you should change your code as below:
InputStream fs = getResources().openRawResource(R.raw.treasureclass);
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
String line = null;
while((line = br.readLine()) != null){
Log.i("line", line);
//split the content of 'line' and save them in your desired way
}
I'm trying to read a .txt file. but in the textview the text in not formatted. i,e there is no new line in the textview even if i have given the new line. The sentences are continuous. Is it possible to format? the code goes like this
final StringBuffer buffer = new StringBuffer();
try{
DataInputStream dataIO= new DataInputStream(getResources().openRawResource(R.raw.chapter1));
String strLine= null;
while((strLine = dataIO.readLine())!=null){
buffer.append(strLine);
}
dataIO.close();
}catch(Exception e){
}
TextView tv=(TextView) findViewById(R.id.chapter);
tv.setText(buffer.toString());
}
Refer to the documentation of DataInputStream.readLine()
The string does not include the newline sequence.
So the strLine, which you are appending to the StringBuffer will not contain the newline.
You can modify your code as follows:
while((strLine = dataIO.readLine())!=null){
buffer.append(strLine);
buffer.append('\n');
}
Store your file content in a string after reading the file as follow ::
String fileContent = buffer.toString().replaceAll("\\r|\\n", "<br />");
Then, in your text view set text as HTML :
txtview.setText(Html.fromHtml(fileContent));