Three ways to append file content to Java

Source: Internet
Author: User

  1. Import <a href="HTTP://LIB.CSDN.NET/BASE/17" class=' Replace_word ' title="Java ee Knowledge Base" target=' _blank ' style=' color: #df3434; font-weight:bold; ' >java</a>.io.  BufferedWriter;
  2. Import Java.io.File;
  3. Import Java.io.FileOutputStream;
  4. Import Java.io.FileWriter;
  5. Import java.io.IOException;
  6. Import Java.io.OutputStreamWriter;
  7. Import Java.io.RandomAccessFile;
  8. /**
  9. *
  10. * @author Malik
  11. * @version 2011-3-10 10:49:41
  12. */
  13. Public class AppendFile {
  14. public static void method1 (string file, String conent) {
  15. BufferedWriter out = null;
  16. try {
  17. out = new BufferedWriter (Newoutputstreamwriter (new FileOutputStream (file, true));
  18. Out.write (conent);
  19. } catch (Exception e) {
  20. E.printstacktrace ();
  21. } finally {
  22. try {
  23. if (out! = null) {
  24. Out.close ();
  25. }
  26. } catch (IOException e) {
  27. E.printstacktrace ();
  28. }
  29. }
  30. }
  31. /**   
  32. * Append file: Use FileWriter
  33. *
  34. * @param fileName
  35. * @param content
  36. */
  37. public static void Method2 (String fileName, string content) {
  38. FileWriter writer = null;
  39. try {
  40. //Open a write file, the second parameter in the constructor true indicates that the file is written in append form
  41. writer = New FileWriter (FileName, true);
  42. Writer.write (content);
  43. } catch (IOException e) {
  44. E.printstacktrace ();
  45. } finally {
  46. try {
  47. if (writer! = null) {
  48. Writer.close ();
  49. }
  50. } catch (IOException e) {
  51. E.printstacktrace ();
  52. }
  53. }
  54. }
  55. /**   
  56. * Append file: Use Randomaccessfile
  57. *
  58. * @param filename
  59. * @param content Additions
  60. */
  61. public static void Method3 (String fileName, string content) {
  62. Randomaccessfile randomfile = null;
  63. try {
  64. //Open a random Access file stream, read and write
  65. Randomfile = New Randomaccessfile (FileName, "RW");
  66. //File length, number of bytes
  67. Long filelength = Randomfile.length ();
  68. //Move the Write file pointer to the end of the file.
  69. Randomfile.seek (filelength);
  70. Randomfile.writebytes (content);
  71. } catch (IOException e) {
  72. E.printstacktrace ();
  73. } finally{
  74. if (randomfile! = null) {
  75. try {
  76. Randomfile.close ();
  77. } catch (IOException e) {
  78. E.printstacktrace ();
  79. }
  80. }
  81. }
  82. }
  83. public static void Main (string[] args) {
  84. try{
  85. File File = new file ("D://text.txt");
  86. if (File.createnewfile ()) {
  87. System.out.println ("Create file successed");
  88. }
  89. Method1 ("D://text.txt", "123");
  90. METHOD2 ("D://text.txt", "123");
  91. Method3 ("D://text.txt", "123");
  92. }catch (Exception e) {
  93. System.out.println (e);
  94. }
  95. }
  96. }

Three ways to append file content to Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.