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

Posted

技术标签:

【中文标题】Vala 如何将结果 file.read 发送到 html【英文标题】:Vala How send result file.read to 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

我们将不胜感激。谢谢

【问题讨论】:

【参考方案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 如果这解决了您的问题,请单击我的答案左上角的勾号,使其变为绿色(这就是 *** 的工作原理)

以上是关于Vala 如何将结果 file.read 发送到 html的主要内容,如果未能解决你的问题,请参考以下文章

如何使用Vala将GStreamer视频输出到Gdk.Pixbuf?

如何在 Vala 中导入其他文件?

如何访问 Vala 中的环境变量?

Vala 和 PolicyKit

如何监控 vala 中的目录?

如何在 Vala 中使用 Qt GUI?