Hi
I am writing a hangman game with the words stored in a text file.
where do l put the file and how to I retrieve words into a string?
Android project
Is there any one out there?
I have created a new folder called asset along side java and res and put my text file ’ word1. txt’ into it.
Is there any one out there?
I have created a new folder called asset along side java and res and put my text file ’ word1. txt’ into it.
In word1.txt put
Hello
This
Works
Fine
Then a textView called tv in main xml
And the following code
package com.mycompany.readtext;
import android.app.;
import android.os.;
import android.widget.;
import java.io.;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=findViewById(R.id.textview1);
//public static void main(String[] args) {
String mLine = null;
BufferedReader reader = null;
String sentence="";
try
{
reader = new BufferedReader(
new InputStreamReader(getAssets().open("word1.txt")));
}
catch (IOException e)
{
tv.setText("fault");
}
// do reading, usually loop until end of file reading
try
{
while ((mLine = reader.readLine()) != null)
{
sentence=sentence+mLine+" ";
tv.setText(sentence);
}
}
catch (IOException e){
tv.setText("fault2");
}
finally {
try {
reader.close();
} catch (IOException e) {
tv.setText("Unable to close file: " + mLine.toString());
}
catch(NullPointerException ex) {
tv.setText("// File was probably never opened!");
}
}}}