且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从文件中读取前15行

更新时间:2023-08-26 18:27:28

你可以用 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..