Which of the following is the method used to add more data to the end of an existing file?

public: static System::IO::StreamWriter ^ AppendText(System::String ^ path); public static System.IO.StreamWriter AppendText (string path); static member AppendText : string -> System.IO.StreamWriter Public Shared Function AppendText (path As String) As StreamWriter StreamWriter

A stream writer that appends UTF-8 encoded text to the specified file or to a new file.

Show

Examples

The following example appends text to a file. The method creates a new file if the file doesn't exist. However, the directory named temp on drive C must exist for the example to complete successfully.

using namespace System; using namespace System::IO; int main() { String^ path = "c:\\temp\\MyTest.txt"; // This text is added only once to the file. if ( !File::Exists( path ) ) { // Create a file to write to. StreamWriter^ sw = File::CreateText( path ); try { sw->WriteLine( "Hello" ); sw->WriteLine( "And" ); sw->WriteLine( "Welcome" ); } finally { if ( sw ) delete (IDisposable^)sw; } } // This text is always added, making the file longer over time // if it is not deleted. StreamWriter^ sw = File::AppendText( path ); try { sw->WriteLine( "This" ); sw->WriteLine( "is Extra" ); sw->WriteLine( "Text" ); } finally { if ( sw ) delete (IDisposable^)sw; } // Open the file to read from. StreamReader^ sr = File::OpenText( path ); try { String^ s = ""; while ( s = sr->ReadLine() ) { Console::WriteLine( s ); } } finally { if ( sr ) delete (IDisposable^)sr; } } using System; using System.IO; class Test { public static void Main() { string path = @"c:\temp\MyTest.txt"; // This text is added only once to the file. if (!File.Exists(path)) { // Create a file to write to. using (StreamWriter sw = File.CreateText(path)) { sw.WriteLine("Hello"); sw.WriteLine("And"); sw.WriteLine("Welcome"); } } // This text is always added, making the file longer over time // if it is not deleted. using (StreamWriter sw = File.AppendText(path)) { sw.WriteLine("This"); sw.WriteLine("is Extra"); sw.WriteLine("Text"); } // Open the file to read from. using (StreamReader sr = File.OpenText(path)) { string s = ""; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } } } Imports System.IO Public Class Test Public Shared Sub Main() Dim path As String = "c:\temp\MyTest.txt" ' This text is added only once to the file. If Not File.Exists(path) Then ' Create a file to write to. Using sw As StreamWriter = File.CreateText(path) sw.WriteLine("Hello") sw.WriteLine("And") sw.WriteLine("Welcome") End Using End If ' This text is always added, making the file longer over time ' if it is not deleted. Using sw As StreamWriter = File.AppendText(path) sw.WriteLine("This") sw.WriteLine("is Extra") sw.WriteLine("Text") End Using ' Open the file to read from. Using sr As StreamReader = File.OpenText(path) Do While sr.Peek() >= 0 Console.WriteLine(sr.ReadLine()) Loop End Using End Sub End Class

Remarks

This method is equivalent to the StreamWriter(String, Boolean) constructor overload. If the file specified by path does not exist, it is created. If the file does exist, write operations to the StreamWriter append text to the file. Additional threads are permitted to read the file while it is open.

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

The path parameter is not case-sensitive.

For a list of common I/O tasks, see Common I/O Tasks.

Applies to

Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file.

Syntax

object.OpenTextFile (filename, [ iomode, [ create, [ format ]]])

The OpenTextFile method has these parts:

Part Description
object Required. Always the name of a FileSystemObject.
filename Required. String expression that identifies the file to open.
iomode Optional. Indicates input/output mode. Can be one of three constants: ForReading, ForWriting, or ForAppending.
create Optional. Boolean value that indicates whether a new file can be created if the specified filename doesn't exist. The value is True if a new file is created; False if it isn't created. The default is False.
format Optional. One of three Tristate values used to indicate the format of the opened file. If omitted, the file is opened as ASCII.

Settings

The iomode argument can have any of the following settings:

Constant Value Description
ForReading 1 Open a file for reading only. You can't write to this file.
ForWriting 2 Open a file for writing only. Use this mode to replace an existing file with new data. You can't read from this file.
ForAppending 8 Open a file and write to the end of the file. You can't read from this file.

The format argument can have any of the following settings:

Constant Value Description
TristateUseDefault -2 Opens the file by using the system default.
TristateTrue -1 Opens the file as Unicode.
TristateFalse 0 Opens the file as ASCII.

The following code illustrates the use of the OpenTextFile method to open a file for appending text:

Sub OpenTextFileTest Const ForReading = 1, ForWriting = 2, ForAppending = 8 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 Dim fs, f Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.OpenTextFile("c:\testfile.txt", ForAppending, True, TristateFalse) f.Write "Hello world!" f.Close End Sub

See also

  • Objects (Visual Basic for Applications)

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.

In Java, we can append a string in an existing file using FileWriter which has an option to open a file in append mode. Java FileWriter class is used to write character-oriented data to a file. It is a character-oriented class that is used for file handling in Java. Unlike FileOutputStream class, we don’t need to convert the string into a byte array because it provides a method to write a string directly. 

Note: The buffer size may be specified, or the default size may be used. A Writer sends its output immediately to the underlying character or byte stream.



Let us see constructors used  later on adhering towards as usual methods of this class 

Constructor: FileWriter(File file, boolean append): 

It Constructs a FileWriter object given a File object in append mode. Now let us toggle onto methods of this class which is invoked here and play a crucial role in appending a string in an existing file as follows:

Method 1: write()

This method writes a portion of a String

Syntax:

void write(String s,int off,int len);

Return Type: Void 

Parameters: 

  • Input string
  • int off
  • String length

Method 2: close()

This method closes the stream after flushing it.

Return Type: Void 

Example 

    public static void appendStrToFile(String fileName,

            BufferedWriter out = new BufferedWriter(

                new FileWriter(fileName, true));

            System.out.println("exception occurred" + e);

    public static void main(String[] args) throws Exception

        String fileName = "Geek.txt";

            BufferedWriter out = new BufferedWriter(

                new FileWriter(fileName));

            out.write("Hello World:\n");

            System.out.println("Exception Occurred" + e);

        String str = "This is GeeksforGeeks";

        appendStrToFile(fileName, str);

            BufferedReader in = new BufferedReader(

                new FileReader("Geek.txt"));

            while ((mystring = in.readLine()) != null) {

                System.out.println(mystring);

            System.out.println("Exception Occurred" + e);

Output:


Article Tags :