java - How I suppose to use files in Android -
I am trying to develop applications for Android and I do not know how to write / read a file in Android phone is. / P>
Normally I will do the following:
import java.io.buffferedReader; Import java.io.buffferedWriter; Import java.io.fileReader; Import java.io.filewiter; Public category test {public static zero main (string [] args) {try {string path = "file.txt"; Bufordedder BV = new buffed vetter (new flamer (path)); Bw.write ("XYZ"); Bw.close (); Buffettder BR = New buffed reader (new flamerdder (path)); String line = "", data = ""; While ((line = br.readline ())! = Null {data = data + line;} br.close ();} hold (exception e) {e.printStackTrace ();}}}
You are very close here, something that I did, where I check to see if one SD card, if it will not save locally (Remember, because it is an Android app, all your data will be saved on the phone), then you can create a directory if not there and then file it If you have any questions, ask me, it is more to write, but reading is as easy, you just make a buffer reader, give it a file, and read it; like you did in your code
I hope that it helps:
SimpleDataFormatPhotoformat = New SimpleDetermat ("DDMMAI-HMMMS"); // DateDenam to Create SimpleDateFormat sdf = Create new SimpleDateFormat ("MMMddyy-Hhmmss"); / * * An account that is no SD card if * is an SD card, then crafted directory on the SD card to be used to store the results of the test log. If there is no SD card, then the directory is created on the internal hard drive / / / on the phone, if there is no SD card, (if the environment is the gatestylestorage (== faucet)) {directory = new file ( Environment.getDataDirectory () + "/ robotiumtastog /"); PhotoDirectory = New File (Environmental Gate Data Directory ("+ /" Robotium-Screenshots / "); // If no directory exists, create new directory (! Directory.exists ()) {directory.mkdir (); } // If the phone has an SD card, then the second;) if (environment. Gatesteenstoristestat (!) = Null) {// SD card directory = Search the directory on the new file (Environment Gatestinstoraz Directory () + "/ robotiumtstog / "); PhotoDirectory = New File (EnvironmentalGetInstyle Storey Directory ("+ / robotium-screenshot /"); // If no directory exists, create a new directory to store test //; (! Directory.exists ()) {directory.mkdir (); }} // end of screen of the SD card / * * where the name of the test log is created It can be changed to any other test methods * / string filename = "TravelTestResults.csv"; / * * Checks for existing test logs, and if they exist, they are deleted *, create a new test log for each test method / / file logic = new file (directory, filename); If (logResults.exists ()) {logResults.delete (); } If (! LogResults.exists ()) {logResults.createNewFile (); } / * * This type of writing stream for logging the test results * The stream must be closed, using * bw.close () * to display on log results. If the stream is not closed, the page will be empty when you open the * text file in which the results are stored. * / BW = new buffed vaitters (new flamerts (true)); Bw.write ("Test run, test results, comments \" \ n \ ""); Bw.close ();
Comments
Post a Comment