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 ExamplesThe 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 ClassRemarksThis 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. Syntaxobject.OpenTextFile (filename, [ iomode, [ create, [ format ]]]) The OpenTextFile method has these parts:
SettingsThe iomode argument can have any of the following settings:
The format argument can have any of the following settings:
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 SubSee also
Support and feedbackHave 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.
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:
Method 2: close() This method closes the stream after flushing it. Return Type: Void Example
Output: Article Tags : |