c# - 如何在C#中编写此Java代码

标签 c# java

我有这个Java代码,我想翻译成C#:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
ps.printf("\n\n+++++ %s %s\n",Environment.UserName, new SimpleDateFormat("MM/dd/yyyy HH:mm:ss aa").format(new Date()));

最佳答案

就像是:

  // You can use "using" blocks guarantee streams are disposed (like try/finally { boas.Close(); })
  using (var baos = new MemoryStream())
  {
    // Stream encodes as UTF-8 by default; specify other encodings in this constructor
    using (var ps = new StreamWriter(baos))
    {
      // Format string almost the same, but 'tt' for 'am/pm'
      // Could also use move formatting into
      //    DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss tt")
      // or ToLongDateString and ToLongTimeString for locale-defined formats
      ps.Write("\n\n+++++ {0} {1:MM/dd/yyyy HH:mm:ss tt}\n", Environment.UserName, DateTime.Now);

      // Need to close or flush the StreamWriter stream before the bytes will
      // appear in the MemoryStream
      ps.Close();
    }

    // Extract bytes from MemoryStream
    var bytes = baos.ToArray();
  }

关于c# - 如何在C#中编写此Java代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5064355/

相关文章:

Java FileReader,仅作为读取

c# - 客户端 GET 请求没有返回任何内容?

c# - 将 Windows 应用商店应用程序中的 Canvas 保存为图像文件

c# - 调用最佳实践

C# SerialPort - 混合具有不同波特率的端口时出现问题

c# - Mock.Of<foo> 为带参数的方法调用设置返回值

java类导入显示编译错误

java - 有没有办法将 @Autowired 变量传递给 Spring Boot 应用程序中的其他类?

java - 用于 Git 或 TFS 的 Ivy 自定义解析器

java - 我无法将 bean 引用给客户端拦截器