Improve this answer. Community Bot 1 1 1 silver badge. Should I use a StringBuffer to append all my String and then at the end just to do this: out. I wouldn't recommend it. That way you'd be unnecessarily using up memory to hold the entire "file" in memory. Just write the strings to the outputstream as you generate them, so that you can release the references to them and let the JVM reclaim the memory they use.
Ynjxsjmh Ynjxsjmh 7, 2 2 gold badges 14 14 silver badges 35 35 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.
Post as a guest Name. Email Required, but never shown. The Overflow Blog. Who owns this outage? Building intelligent escalation chains for modern SRE. Podcast Who is building clouds for the independent developer? Featured on Meta. Modern Guava alternative to deprecated Files. A word to the wise - this will create a new file if it isn't there, but will overwrite the characters of the existing file if it is.
If the new data is smaller, that will mean you probably create a corrupted file. Ask me how I know! Ok, how do you know? Just use Files. It will work as expected. Use Apache Commons IO api. Anirban Chakrabarti Anirban Chakrabarti 5 5 silver badges 8 8 bronze badges.
Use this, it is very readable: import java. Files; import java. Paths; Files. WRITE ;. BDL Ran Adler Ran Adler 3, 26 26 silver badges 27 27 bronze badges. It's also a copy of an existing answer. In case if you need create text file based on one single string: import java. IOException; import java. So this is a potential issue.
Use string. Sean H. Worthington Sean H. Worthington 1, 14 14 silver badges 9 9 bronze badges. You could do this: import java. Mostafa Rezaei Mostafa Rezaei 1 1 gold badge 5 5 silver badges 12 12 bronze badges. Using org. FileUtils: FileUtils. BullyWiiPlaza Info GoYun. Info 1, 12 12 silver badges 12 12 bronze badges. If you only care about pushing one block of text to file, this will overwrite it each time.
Eric Leschinski k 86 86 gold badges silver badges bronze badges. Eric Leschinski : thank you for making my answer more professional i also assumed this was exactly what the OP wanted since this is what actually most people wants,just dump the text and replace it — bhathiya-perera.
Once the original question has been answered and the OP is satisfied and long-gone, pages like this serve only as a useful artifact to people who land here from a Google search.
I landed on this page in order to create a mini text appender to a file. So it's good to speak to the entire audience rather than the OP after the OP has moved on. Jobin Mathew Jobin Mathew 1 1 silver badge 7 7 bronze badges. While this code snippet may be the solution, including an explanation really helps to improve the quality of your post.
Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please, improve your answer adding the proper error case handling. John Doe John Doe. Yes, JanusTroelsen is right, better use The try-with-resources statement docs. Brad Parks Brad Parks Please add proper error case handling closing all opened resources and propogating exceptions — Boris Brodski.
The Overflow Blog. Who owns this outage? Building intelligent escalation chains for modern SRE. Podcast Who is building clouds for the independent developer? Featured on Meta. Now live: A fully responsive profile. Reducing the weight of our footer. Linked 5. See more linked questions. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings. You can also subscribe to RSS Feed.
I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node. The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy!
No spam ever, unsubscribe at any time. How to save a string to a text file using Java.
0コメント