Vala如何将结果file.read发送到html

时间:2013-08-06 06:26:26

标签: html file process send vala

我想知道是否有人可以给我指示如何将动作glib.file.read的结果发送到html。

示例我想读取文件string.txt

File file = File.new_for_path ("string.txt");
try {
    FileInputStream @is = file.read ();
    DataInputStream dis = new DataInputStream (@is);
    string line;

    while ((line = dis.read_line ()) != null) {
        stdout.printf ("%s\n", line);
    }
} catch (Error e) {
    stdout.printf ("Error: %s\n", e.message);
}

return 0;

并且我希望将此proses的结果发布到文件html,例如index.html

帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:2)

这是一个简短的例子,说明如何将数据写入取自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;
}