如何从文件中读取前15行 [英] How to read top 15 lines from a file

查看:63
本文介绍了如何从文件中读取前15行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我在一个文本文件中有30行。

现在我要打印前15行file.after打印后会显示一个消息框,单击确定按钮后会打印剩余的15行。



以下是我的打印功能



Hi all

I am having 30 lines in a text file.
Now i want to print top 15 line first from the file.after printing it will show a message box and after clicking ok button it will print the remaining 15 lines.

Below is my printing function

public void Print_Trans(string filePath)
       {
           UInt32 len = 0;
           StringBuilder buffer = new StringBuilder();
           string[] data = new string[100];
           buffer = buffer.Append(File.ReadAllText(filePath));
           buffer = buffer.Append("\r");
           len = Convert.ToUInt32((buffer.Length));
           data = File.ReadAllLines(filePath);
           noOfLines = data.Length;
           PrtSetFont(10002);
           System.IO.StreamReader file =
               new System.IO.StreamReader(@"C:\Display.txt");
           string[] liness = File.ReadAllLines(@"C:\Display.txt");
           var firstValue = "";
           var SValue = "";
           var TValue = "";
           var FValue = "";
           var FiValue = "";
           var SiValue = "";
           var SeValue = "";
           status = PrtWrite("", len);
           string line111 = "   Date                    Particulars                              ChequeNo     Withdrawals          Deposite          Balance ";
           status = PrtWrite(line111, len);
           PrtPosRelV(1);
           string line222 = "   ---------------------   ---------------------------------      -----------    ---------------         -----------         -----------";
           status = PrtWrite(line222, len);
           PrtPosRelV(1);



           foreach (var lines in liness)
           {
                    if (lines == null || lines.Length < 1)
                    {
                   PrtPosRelV(1);
                    }
                    else
                    {

                   firstValue = lines.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[0];
                   if (firstValue == null)
                   {
                       firstValue = "";
                   }
                   SValue = lines.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[1];
                   if (SValue == null)
                   {
                       SValue = "";
                   }
                   TValue = lines.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[2];
                   if (TValue == null)
                   {
                       TValue = "";
                   }
                   FValue = lines.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[3];
                   if (FValue == null || FValue == "")
                   {
                       FValue = "0";
                   }
                   FiValue = lines.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[4];
                   if (FiValue == null)
                   {
                       FiValue = "";
                   }
                   SiValue = lines.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[5];
                   if (SiValue == null)
                   {
                       FiValue = "";
                   }
                   SeValue = lines.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)[6];
                   if (SeValue == null)
                   {
                       SeValue = "";
                   }
                   int leng = 0;
                   int sp = 0;
                   PrtPosAbsH(5);
                   status = PrtWrite(firstValue, len);
                   PrtPosAbsH(10);
                   status = PrtWrite(SValue, len);
                   PrtPosAbsH(30);
                   status = PrtWrite(TValue, len);
                   PrtPosAbsH(75);
                   status = PrtWrite(FValue, len);
                   leng = FiValue.Length;
                   if (leng > 3)
                   {
                       sp = app_sp(leng);
                       int tot = 99;
                       int rem = tot - (92 + sp);
                       PrtPosAbsH(92 + sp);
                   }
                   status = PrtWrite(FiValue, len);
                   leng = SiValue.Length;
                   if (leng > 3)
                   {
                       sp = app_sp(leng);
                       int tot = 120;
                       int rem = tot - (112 + sp);
                       PrtPosAbsH(112 + sp);
                   }
                   status = PrtWrite(SiValue, len);
                   leng = SeValue.Length;
                   if (leng > 3)
                   {
                       sp = app_sp(leng);
                       int tot = 134;
                       int rem = tot - (127 + sp);
                       PrtPosAbsH(135 + sp);
                   }
                   status = PrtWrite(SeValue, len);
                   PrtPosRelV(1);
               }
           }
       }





请告诉我怎么做。



谢谢



Please tell me how to do this.

Thank you

推荐答案

你可以用 File.ReadLines方法 [ ^ ]

you can use File.ReadLines Method [^]
var firstlineSet = File.ReadLines(fileName)
            .Take(15)   // limit to first 15
            .ToArray(); 




var secondlineSet = File.ReadLines(fileName)
            .Skip(15)   // skip first 15 and take the rest
            .ToArray(); 





更新:



UPDATE:

Print_Trans(firstlineSet);
//show your message
Print_Trans(secondlineSet);



您需要更改print方法以接受字符串数组打印


you need to change the print method to accept string array to print

public void Print_Trans(string[] lines)
{
   // use above lines to print inside your method 
}


我会通过编写可以重用的代码来解决这个问题。我的目标是:



1.只读一次文件



2.产生一个有用的集合根据每件作品的行数和文件的件数量,以及要生产的件数。



a。 'chunk:一个字符串列表,其大小是用户为每个块指定的行数。



b。 'lineChunks:一系列的块。



3.以某种方式处理文件过于短甚至无法读取一个块或者行数文件不能被块大小整除。
I would approach this problem by writing code I could re-use. My goal would be:

1. read the file only once

2. produce a useful collection of "pieces" of the file based on the number of lines to read into each piece, and the number of pieces to be produced.

a. 'chunk: a List of string whose size is the number of lines the user specifies per chunk.

b. 'lineChunks: a collection of chunks.

3. deal in some way with the file being too "short" to read even one chunk, or the number of lines in the file not being integrally divisible by the chunk size.
private string filePath = @"C:\Users\YourUserName\Desktop\thirty.txt";

private List<list><string>> getLines(string fPath, int nChunks, int linesPerChunk)
{
    var Lines = File.ReadLines(fPath);

    var lineChunks = new List<list><string>>();

    for (int i = 0; i < nChunks; i++)
    {
        var chunk = Lines.Skip(i * linesPerChunk).Take(linesPerChunk).ToList();
        
        if(! (chunk.Count == 0)) lineChunks.Add(chunk);
    }

    return lineChunks;
}
</string></list></string></list>

为了测试这个,我创建了一个名为'thirty.txt'的文本文件,有30行;每行的内容是1~30之间的数字。

To test this I created a Text file named 'thirty.txt' that had 30 lines; each line's content was a number from 1~30.

// tests

var chunkCollection1 = getLines(filePath, 2, 15);
// returns a List with two elements: 
// chunkCollection1[0] has lines 1-15
// chunkCollection1[1] has lines 16-30

var chunkCollection2 = getLines(filePath, 2, 30);
// returns a List with one element: 
// chunkCollection2[0] has lines 1-30

var chunkCollection3 = getLines(filePath, 2, 20);
// returns a List with two elements: 
// chunkCollection3[0] has lines 1-20
// chunkCollection3[1] has lines 21-30

var chunkCollection4 = getLines(filePath, 2, 59);
// returns a List with one element: 
// chunkCollection4[0] has lines 1-30

var chunkCollection5 = getLines(filePath, 2, 0);
// returns an empty List


你好朋友,

这是一个有趣的问题。说实话,我从来没有使用过这个,但是已经给这些门票添加了书签。

SO LINK [ ^ ]

SO LINK [ ^ ]

希望这些可以帮到你...

谢谢..
Hello friend,
This is an interesting question. To be honest I have never used this, but had bookmarked some tickets for this.
SO LINK[^]
SO LINK[^]
Hope these help you...
Thanks..


这篇关于如何从文件中读取前15行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆