Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I concatenate string to an existing file?

Tags:

file

append

dart

I've got a text file (it has content in it) and I want to append text to it. This is my code:

File outputFile=new File('hello.out');
      outputFile.createSync();
      List<String> readLines=files[i].readAsLinesSync(Encoding.UTF_8);
      for(int j=0;j<readLines.length;j++)
      {

        outputFile.writeAsStringSync(readLines[j], FileMode.APPEND); }

For some reason Dart put a yellow line under "FileMode.APPEND" and it says that it's an "extra argument". However, this link http://api.dartlang.org/docs/releases/latest/dart_io/File.html claims that it is optional.

like image 224
Cheshie Avatar asked Feb 21 '13 21:02

Cheshie


People also ask

How do I add text to an existing file in Java?

Likewise, the text to be added is stored in the variable text . Then, inside a try-catch block we use Files ' write() method to append text to the existing file. The write() method takes the path of the given file, the text to the written, and how the file should be open for writing.

How do I append a string?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

How do I append a text file to another text file?

You do this by using the append redirection symbol, ``>>''. To append one file to the end of another, type cat, the file you want to append, then >>, then the file you want to append to, and press <Enter>.


1 Answers

The FileMode is an optional, named parameter, so you have to specify its name ('mode') when you call it. To solve your problem, change this:

outputFile.writeAsStringSync(readLines[j], FileMode.append);

to this:

outputFile.writeAsStringSync(readLines[j], mode: FileMode.append);
like image 103
plowman Avatar answered Sep 22 '22 00:09

plowman