打印模板时将writer转换为字符串

时间:2018-06-12 23:21:17

标签: java

为了创建此输出,我使用了https://github.com/spullara/mustache.java

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.List;

public class Example {

    List<Item> items() {
        return Arrays.asList(new Item("Item 1", "$19.99", Arrays.asList(new Feature("New!"), new Feature("Awesome!"))));
    }

    static class Item {
        Item(String name, String price, List<Feature> features) {
            this.name = name;
            this.price = price;
            this.features = features;
        }

        String name, price;
        List<Feature> features;
    }

    static class Feature {
        Feature(String description) {
            this.description = description;
        }

        String description;
    }

    public static void main(String[] args) throws IOException {
        MustacheFactory mf = new DefaultMustacheFactory();
        Mustache mustache = mf.compile("template.mustache");
        mustache.execute(new PrintWriter(System.out), new Example()).flush();
    }
}

另一部分

<activity
        android:name=".Splash"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

如果有人能在String中获得此输出吗?如果有,怎么样?我已经尝试在堆栈上搜索,但我没有找到这样的东西。这不重复。

第一部分是我的template.mustache。

1 个答案:

答案 0 :(得分:0)

尝试使用StringWriter

pthread_mutex_t lock = * (pthread_mutex_t *) arg;

(注意:我不确定Mustache是​​如何工作的,所以只看看StringWriter的用法!)