如何在一行中保存字符串?

Yar*_*arh 1 java string io android

我有几个字符串:a,b,c,d,e.我想将它们保存为文件FILENAME作为单个字符串 a b c d:e.问题 - 我需要保持d;e从新行开始的信息.

String str = a
        + " "
        + b

        + c
        + "\n "
        + d
        + ";"
        + e + "\r\n";
try {
    fos = openFileOutput(FILENAME, Context.MODE_APPEND);
    fos.write(str.getBytes());
    fos.flush();
    fos.close();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

kee*_*lar 5

你可以使用String.format()(如果你熟悉C/C++,这个类似printf()).例如,以下为您提供所需的字符串格式:

String.format("%s %s %s%n%s;%s%n", a, b, c, d, e);
Run Code Online (Sandbox Code Playgroud)

其中%n是新的线符号.

如果你输出字符串很长,那么你也可以使用StringBuilder的与一起String.format().