【问题标题】:Vala How send result file.read to htmlVala 如何将结果 file.read 发送到 html
【发布时间】:2013-08-07 00:56:33
【问题描述】:

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

我们将不胜感激。谢谢

【问题讨论】:

    标签: html file process send vala


    【解决方案1】:

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

    【讨论】:

    • @tuanpembual 如果这解决了您的问题,请单击我的答案左上角的勾号,使其变为绿色(这就是 stackoverflow 的工作原理)
    猜你喜欢
    • 2018-03-09
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 2014-05-31
    • 2014-06-08
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    相关资源
    最近更新 更多