1

我想知道是否有人可以指导我如何将操作 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;

我想将这篇散文的结果发布到文件 html,例如 index.html

帮助将不胜感激。谢谢

4

1 回答 1

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;
}
于 2013-08-06T10:00:15.357 回答