How can we modify a text file from android, such as
append at the end of the file.
clear data at any intermediate position and also blank lines in it.
Is there any option to delete a particular line from the text file, I read so many tutorials regarding this all have mentioned to create a temporary file and asked to copy the contents from the original file except that particular line, and to replace the original file with this temporary file.
Is there any alternative to do this with in that original file itself without creating a temporary file?
Thanks in Advance.
You can't do the second option programmatically, that's virtually impossible. The first option however is possible. Just use an InputStream Object to load your file, then you can get the text from the InputStream as a String, I believe, and then you can append the data at the end. Now that I think of it, it might be possible, but with a heck of a lot of work, to edit the text at the middle... But that's up to you to figure out...
Pretty much everything that is possible with java's file api's can be done in android
You can use a FileOutputStream, passing true for append and then using the normal output stream operations.
For changing files in a random-access manner, try RandomAccessFile. This allows you to change the current byte you are pointing to. However, if you delete data in this manner it won't automatically cause the file to contract to fill the empty space; you would need to do this manually and then change the length of the file via setLength().
Related
The question might seem incomprehensible just from the title alone so let me elaborate on what I mean. From what little I've dabbled in HTML and JavaScript, you could add HTML lines to the HTML file from using a Javascript function in the script.js file and it would add those HTML lines you've written into the function to the HTML file on execution and it would work as if you've written it in the HTML file to begin with. That was my understanding of how it worked, at least, if I'm wrong on my assessment feel free to correct me on that matter.
Anyway, I'm wondering if we could do a similar thing in Android Studio where we can use a Kotlin function to add an XML line/attribute/command like 'app:srcCompat="#drawable/whatever"' to an XML file.
Of course the question doesn't come from a mere sense of wonder. I currently have an application with a fragment that's supposed to get some football teams from the Room database and display them in CardViews using RecyclerView. In those cards, the team's name and their logo should be displayed. I don't have logos as image files in the Room database itself, however there is a column that stores the names of the drawable files in which the team logos are stored. (For example: Team A's logo is stored in the drawable's as 'teama.png' and it has 'teama' stored in a column.)
In the Adapter class of the RecyclerView, I want to use the bind() function to put the name and the logo on the cards. What I'm expecting to do (related to my question overall) is using a function that can take a string parameter ("app:srcCompat="#drawable/teama"") and puts it to the XML file of my team item. Is this possible? I'm open to other solutions as well and can post code if requested.
Thank you for your answer beforehand.
Is there a way to add XML code to an XML file from a Kotlin code?
Yes, but not in the context of what you are asking.
What I'm expecting to do (related to my question overall) is using a function that can take a string parameter ("app:srcCompat="#drawable/teama"") and puts it to the XML file of my team item. Is this possible?
No. You cannot modify the content of a resource XML file at runtime.
From what little I've dabbled in HTML and JavaScript, you could add HTML lines to the HTML file from using a Javascript function in the script.js file and it would add those HTML lines you've written into the function to the HTML file on execution and it would work as if you've written it in the HTML file to begin with.
JavaScript, run in the browser, does not modify the HTML file on the server. It modifies the DOM: the parsed representation of the HTML that is used by the browser to render a UI on the screen.
Similarly, in Android, you will need to update the View objects — created from parsing that resource XML file — to reflect your desired name and logo. This approach is covered in books and courses on Android app development. FWIW, here is a free book of mine on the subject.
Till now this technology is not available and if it is available it's not that famous and in use
There are scenarios in feature files wherein I've use the text "Foo" and on click its open a new page. this text sometime changes to "Foo1" or "Foo2" or to something else. to avoid line by line change in feature file for "Foo" to "Foo1" or "Foo2" is there any way that I can globally declare variable in top/bottom of the feature file where I can set the required text in variable on fly and I shall start executing my test instantly?
This change exist in many feature files and around 1000 lines in feature file. To get solution for this, I try on setting environment variables but I couldn't reach all the way till end this issue to solve. So can anyone help me on this?
Thanks in advance
What if you do the replacement in your step implementation instead? Then you could have the desired value in a separate file or pass it as arguments. That way you don't need to hard code the values.
Could scenario outlines help you in any way or is the value only changing depending on external changes?
My first thought was scenario outlines like #homaxto said.
Then I thought you might want to affect it by which system you are connected to. You can do this through configuration. I have done this with Fig_Newton.
You can either set an environment varaible or use one in the commandline. Or you can use #hook type tags. With a hook tag, you can have a tag at the top of a feature file that you can use to set a variable that affects how the steps operate (but it needs to be handled inside the step).
This is not a coding problem question, I just need general advice.
I am making an application that populates a listfragment with multiple text files. What I want is for the user to select the intended article they wish to read and said article will be provided for them. I have already made a master/detail esk application but it is only reading in a static array for the articles (which will later be changed to an expanding list and more dynamic method for reading in the files)
My question is this: do I make a database for the ~30-40 text files and create an onlistclick--database adapter function or should I use Assetmanager (or something else).
I am new to android programming and this is my first proper application so any suggestions that would be suitable for a novice would be much obliged.
If those text files are going to be modified very often then yes, it is a good idea to create a local database to keep them into.
On the other hand if you only need those 30-40 fixed text files and you are planning to add more just through app updates then you can simplify your life by using the assets folder. Cycle through all the files in there and use them to create the list.
Code snippet for Assets usage:
AssetManager assets = context.getAssets(); //use 'this' if you are inside an activity or 'getActivity()' if you are inside a fragment
String files[] = assets.list("folder_inside_assets"); //use assets.listFiles("folder"); if you want an array of File objects
I have a file that I open and parse and some of the categories can be modified. Im wondering how I can update the original Xml file with the new values.
I assume that the XML file is a normal sequential file. If so, you could try to update just one tag, but it would be easier and safer just to rewrite the entire file.
I write file to sdcard using BufferedWriter.After that I want to overwrite header of file,but other data must be without changes( In the header I must add size of file). So I think I must change position where I need write.
How can I do that?What function should I use?
You can use a RandomAccessFile to access a file, and modify it.