2013-03-01 34 views
0

Dart是否等同於C++的「\ n」?Dart當量 n

在下面的例子:

for(int j=0;j<readLines.length;j++) 
{ 
    outputFile.writeAsStringSync(readLines[j], mode: FileMode.APPEND); 
} 

我想有這是在單獨的行「readlines方法[J]」的文字。這怎麼能做到?

例如:

readlines方法是字符串列表,它包含:「嘿我的名字是Cheshie」和「謝謝你們試圖幫助」。

在上面的代碼中,我試圖寫列表的內容到一個文件中,使用「OUTPUTFILE」,我想它寫成如下:

嘿我的名字是Cheshie

謝謝你們試圖幫助

也就是說,每readlines方法[J]應該寫在一個獨立的行。

謝謝。

代碼:

import 'dart:io'; 

void func (String foldername) 
{ 
    Directory thisFolder = new Directory (foldername); 

    List<File> files = thisFolder.listSync(recursive:false); 
    int number=1; 
    String oldContent=''; 
    String newContent=''; 
    String concatenate=''; 
    String name=''; 
    int nameStart; 
    int nameLength; 
    for (int i=0; i<files.length; i++) 
    { 
    if (files[i].name.endsWith('.in')) 
    { 
     oldContent=files[i].readAsStringSync(Encoding.UTF_8); 
     newContent=number.toString(); 

     var Strings=[newContent, oldContent]; 
     concatenate=Strings.join(); 

     files[i].writeAsStringSync(concatenate); 
     number++; 
    } 

// ===================== 這裏開始相關的部分 ==== ==============

nameLength=files[i].name.length; 
    nameStart=files[i].name.lastIndexOf('\\', nameLength); 
    name=files[i].name.slice(nameStart+1, nameLength); 
    if (name.compareTo('hello.in')==0) 
    { 
     File outputFile=new File('hello.out'); 
     if (outputFile.existsSync()) 
     { 
     outputFile.deleteSync(); 
     } 
     outputFile.createSync(); 
     List<String> readLines=files[i].readAsLinesSync(Encoding.UTF_8); 
     for(int j=0;j<readLines.length;j++) 
     { 
     outputFile.writeAsStringSync('$readLines[j]\n', mode: FileMode.APPEND); 
     //outputFile.writeAsStringSync('\n', mode: FileMode.APPEND); 
     // TODO: figure out how to get to the next line. 

     if (readLines[j].contains('you')) 
      print(readLines[j]); 
     } 
    } 
    } 
} 


void main() 
{ 
    func('In files'); 
    print('the end!'); 
} 

回答

2

\n不是C++具體的,你可以就其寫入文件之前添加到您的字符串的結尾。所以,使用類似

outputFile.writeAsStringSync('${readLines[j]}\n', mode: FileMode.APPEND); 
+0

謝謝,但你確定嗎?我試着用\ n,正如你所說的那樣,但它的反應很奇怪:它將'readLines'的內容寫入'outputFile'j次,每個末尾都帶有字符'[j]'(包括逗號)線。所以是的,它確實跳到了下一行,但它應該每次只在第j個地方打印字符串。那是怎麼發生的...?再次感謝。 – Cheshie 2013-03-02 17:45:27

+0

聽起來和你一樣'$ readLines [j] \ n',而不是'$ {readLines [j]} \ n'。花括號在那裏有很大的不同。 – 2013-03-04 07:31:11

+0

否...與大括號在輸出文件中沒有區別(文字不會移動到下一行)。 :( – Cheshie 2013-03-04 21:53:01

1

readLines[j]的內容是什麼?一堆字?

假設是這樣的:

var readLines = [ 
    'Dart is fun', 
    'It is easy to learn' 
]; 

而且假設你要輸出的以下內容:

Dart 
is 
fun 
It 
is 
easy 
to 
learn 

試試這個:

for (String line in readLines) { 
    String split = line.replaceAll(new RegExp(r'\s+'), '\n'); 
    outputFile.writeAsStringSync(split, mode: FileMode.APPEND); 
} 
+0

readLines [j]是一堆單詞,但我希望輸出是:「Dart很有趣」,並在下一行:「這很容易學習」。所以我不能用\ n來替換空格。 JustDanyul建議的答案看起來是正確的方向,只是它不起作用... – Cheshie 2013-03-04 21:55:42

+0

Chesie,你能用輸入的例子和你期望的輸出例子來更新你的問題嗎?這樣,我可以肯定地幫你:) – 2013-03-05 01:42:40

+0

好的,我做到了。現在更清楚了嗎? =) – Cheshie 2013-03-05 18:33:08