如何在java中获取流编写器的长度

时间:2018-05-18 14:18:14

标签: java streamwriter

在java中,有任何功能等同于下面的c#代码,用于获取流长度。

StreamWriter.BaseStream.Length

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

非常感谢你。

2 个答案:

答案 0 :(得分:0)

Option Explicit Public Sub BuildStatsTable() Dim lngMaxRow As Long Dim lngMaxCol As Long Dim lngCol As Long Dim strRng As String Dim rngLastUsed As Range Set rngLastUsed = GetLastRange(Cells(13, 2)) lngMaxCol = rngLastUsed.Column lngMaxRow = rngLastUsed.Row For lngCol = 2 To lngMaxCol strRng = "R13C" & lngCol & ":R" & lngMaxRow & "C" & lngCol Cells(lngMaxRow + 2, lngCol).FormulaR1C1 = "=IF(COUNT(" & strRng & ")=0,"""",SUM(" & strRng & "))" Cells(lngMaxRow + 3, lngCol).FormulaR1C1 = "=IF(COUNT(" & strRng & ")=0,"""",MIN(" & strRng & "))" Cells(lngMaxRow + 4, lngCol).FormulaR1C1 = "=IF(COUNT(" & strRng & ")=0,"""",MAX(" & strRng & "))" Cells(lngMaxRow + 5, lngCol).FormulaR1C1 = "=IF(COUNT(" & strRng & ")=0,"""",AVERAGE(" & strRng & "))" Next lngCol End Sub Private Function GetLastRange(rngTopLeft As Range) As Range Dim rngUsed As Range Dim lngMaxRow As Long Dim lngMaxCol As Long Set rngUsed = Range(rngTopLeft, rngTopLeft.SpecialCells(xlCellTypeLastCell)) lngMaxRow = rngUsed.Find(What:="*", _ After:=rngUsed.Cells(1), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row lngMaxCol = rngUsed.Find(What:="*", _ After:=rngUsed.Cells(1), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Column Set GetLastRange = Cells(lngMaxRow, lngMaxCol) End Function 最后是写入流中的内容的长度。

答案 1 :(得分:0)

最后我不得不使用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));
                }