这是一个简短的示例,说明如何将数据写入从GNOME Wiki获取的文件(您可以在其中找到更多信息)
// an output file in the current working directory
var file = File.new_for_path ("index.html");
// creating a file and a DataOutputStream to the file
var dos = new DataOutputStream (file.create
(FileCreateFlags.REPLACE_DESTINATION));
// writing a short string to the stream
dos.put_string ("this is the first line\n");
所以你必须创建你的输出文件并为其创建一个 DataOutputStream ,然后,更改你的循环以将你的数据写入"string.txt"
到"index.html"
. 最后,它看起来像这样:
public static int main (string[] args) {
File in_file = File.new_for_path ("string.txt");
File out_file = File.new_for_path ("index.html");
// delete the output file if it already exists (won't work otherwise if
// it does)
if (out_file.query_exists ()) {
try {
out_file.delete ();
} catch (Error e) {
stdout.printf ("Error: %s\n", e.message);
}
}
try {
// create your data input and output streams
DataInputStream dis = new DataInputStream (in_file.read ());
DataOutputStream dos = new DataOutputStream (out_file.create
(FileCreateFlags.REPLACE_DESTINATION));
string line;
// write the data from string.txt to index.html line per line
while ((line = dis.read_line ()) != null) {
// you need to add a linebreak ("\n")
dos.put_string (line+"\n");
}
} catch (Error e) {
stdout.printf ("Error: %s\n", e.message);
}
return 0;
}