【问题标题】:How to get length of a stream writer in java如何在java中获取流编写器的长度
【发布时间】:2018-10-29 00:22:09
【问题描述】:

在 java 中是否有任何等效于以下 c# 代码的功能来获取流长度。

StreamWriter.BaseStream.Length

我在互联网上搜索过,还检查了“BufferredWriter”、“OutputStreamWriter”和“FileOutputStream”的属性,但没有找到任何东西。任何信息表示赞赏。

非常感谢。

【问题讨论】:

  • 我认为例如InputStreamavailable 参数。但有时返回的值不正确。
  • 那会是什么?流没有长度。也许您可以向我们 Java 人解释一下 C# 中流长度的含义是什么?
  • @MaxVollmer 看这里:msdn.microsoft.com/en-us/library/… 这是流中的字节数。
  • 我已经编辑了代码行。我错误地写了“StreamLength”而不是“StreamWriter”。
  • length() 将为您提供File 的字节数。

标签: java streamwriter


【解决方案1】:

OutputStream 最终具有 写入流中的内容的长度。

【讨论】:

    【解决方案2】:

    最后我不得不使用 File.length() 属性,因为我发现无法像 C# 这样从流中获取长度。

    这是怎么做的:

    注意(使用标志等)流与哪个文件相关联。

    当您需要流的长度时,只需获取与流关联的文件的 File.Length,如下所示。

    为什么我需要检查长度是为了防止写入文件超过定义的最大长度。

                    String sFilePath = this.m_sLogFolderPath + File.separator;
                    if(this.m_File2Active == true)
                    {
                        sFilePath += Def.DEF_FILE2;
                    }
                    else
                    {
                        sFilePath += Def.DEF_FILE1;
                    }
                    File file = new File(sFilePath);
                    if(file.length() > this.m_lMaxSize)
                    {
                        this.m_bwWriter.flush();
                        this.m_bwWriter.close();
                        this.m_bwWriter = null;
                        sFilePath = this.m_sLogFolderPath + File.separator;
                        if (this.m_File2Active == true)
                        {
                            sFilePath += Def.DEF_FILE1;
                            this.m_File2Active = false;
                        }
                        else
                        {
                            sFilePath += Def.DEF_FILE2;
                            this.m_File2Active = true;
                        }
                        this.m_bwWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sFilePath, true), Def.DEF_ENCODING_UTF8));
                    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多