score:1
i'm only a first year student in java programming and i am a new user here at stackoverflow.com, so pardon me if coding for android has some special rules i don't know about, which prevents this simple and humble example from working. but here is how i would read from a file in the simplest of ways.
file tempfile = new file("<subdirectoryifany/name_of_file.txt");
scanner readfile = new scanner( tempfile );
// assuming that you can structure the file as you please with fx each bit of info
// on a new line.
int counter = 0;
while ( readfile.hasnextline() ) {
score[counter] = readfile.nextline();
counter++;
}
as for the writing back to the file? put it in an entirely different method and simply make a simplified tostring-like method, that prints out all the values the exact way you want them in the file, then create a "loadtofile" like method and use the to string method to print back into the file with a printstream, something like below.
file tempfile = new file("<subdirectoryifany/name_of_file.txt");
printstream write = new printstream(tempfile);
// specify code for your particular program so that the tostring method gets the
// info from the string array or something like that.
write.print( <objectname/this>.tostringlikemethod() );
// remember the /n /n in the tostringlikemethod so it prints properly in the file.
again if this is something you already know, which is just not possible in this context please ignore me, but if not i hope it was useful. as for the exceptions, you can figure that you yourself. ;)
score:0
since you are a beginner, and i assume you are trying to get things off the ground as quickly as possible, i'd recommend using sharedpreferences
. basically it is just a huge persistent map for you to use! having said that... you should really learn about all the ways of storage in android, so check out this document:
http://developer.android.com/guide/topics/data/data-storage.html
the android docs are awesome! fyi sharedpreferences may not be the best and awesomest way to do this... but i'm all for quick prototyping as a learner. if you want, write a wrapper class around sharedpreferences.
Source: stackoverflow.com
Related Query
- Read the five score in a textfile then print them back with the new score added to the list - ANDROID
- When I deploy my java artifacts to maven with source and javadocs, and then try to reference them from another project, it only downloads the main jar
- I added a new font, works with the first one
- Is maven-eclipse-plugin no longer needed with the new M2Eclipse in Indigo?
- Can anyone get the new marketplace drag n' drop to work with Eclipse Indigo?
- Eclipse: how to update a JUnit test file with newly added method in the source file?
- Why are class files different size when compiling the same code in eclipse, and then with the eclipse compiler via ant?
- Completely remove Android Studio, Eclipse and all the libs/packages installed with them
- Creating a new C++ Project in Eclipse CDT with the same settings as another project
- What is the minimum number of steps to get full javadoc support with the new default project options?
- Boostrapping new Eclipse machines with all the Plugins
- gogo: BundleException: An error occurred trying to read the bundle in OSGi with Equinox
- GWT CodeServer incremental compilation issue with maven and the new packaging gwt-app and gwt-lib
- How to copy my Java Eclipse project to desktop, and then run with the command line?
- How to build an Eclipse plugin with Maven/Tycho for use in the "Install New Software" dialog
- Eclipse Pydev autocomplete not working with error: java.lang.RuntimeException: The python client still hasn't connected back to the eclipse java vm
- What's the correct way to create a new Spring Roo Project with multiple maven modules?
- How to copy all the elements in an array list, then paste them to the array list? Example provided
- Android Camera to open and then open the captured image in a new activity
- Fail to read file and parse with gson when the program packaged as jar
- How can I print a number with a high quantity of decimals in the console?
- I want java to create a new file with ...new PrintStream... it all works fine, but I can't find the file in my project
- Editing the Manifest.MF file with new plugin template wizard
- Importing projects with Eclipse so new stuff I add will go to the source directory as well
- Is it possible to update the Eclipse RCP application with new Eclipse using RCP Updatesite
- Import existing project into eclipse, it auto combine old version with the new one
- How can I read and print a file in eclipse with relative paths?
- How to use the Eclipse Toolkit with the new AWS update (Expansion of AWS Lambda states to all functions)?
- Trying to work on a new project but facing problems with the JRE
- How to read Java byte code with Eclipse only in the console?
More Query from same tag
- Unable to import userlibrary
- How to most efficiently tie a git build version number, to an apk version with eclipse?
- What is an asset in android?
- Commit code from eclipse to GitHub
- Why does my Emulator not work?
- Difference between Run vs Run as in Eclipse
- Eclipse parameter dialog/hint when using functions in code
- PMD in eclipse does not accept exclude-pattern
- Eclipse doesn't recocnize ImageView or AnimationDrawable
- Detect Who Created a Thread (w. Eclipse)
- configure japid with play app?
- How To: Maven project to build JavaScript in Eclipse
- Eclipse (Pulsar) packaging: How do I incorporate other projects into my jar?
- Android: Conversion to Dalvik format failed: Unable to execute dex: null
- Why am I getting this error using Soot operations from the Eclipse Soot plug-in?
- Code won't launch
- Can not open file with special charecters from Java when using Eclipse on MacOSX
- Eclipse can't open class declarations
- GWT in eclipse Compile error
- ColdFusion 9 line debugger for eclipse
- error while trying to run MapReduce job using eclipse Indigo
- printf not printing in console in eclipse c
- Why is Re-installation failed due to different application signatures?
- How to debug ant build war files
- Save state between closening/opening intro part
- C++ coding in eclipse...help with code assist
- android-maven-plugin 3.2.0 with maven 3.0.4 not compile it
- Tomcat installation and configuration for Eclipse dynamic web project
- Can't find the generated bulkloader.yaml
- Keyboard Shortcut key for run