简体   繁体   English

从文本文件获取第n行,将其反转并打印出来

[英]Get every n-th line from text-file, reverse it and print out

I writing function which get: file-path and number as parameters. 我写的函数得到:文件路径和数字作为参数。 Function open this file and get every n-th line from it, reverse it and print out to console. 函数打开该文件并从中获取第n行,将其反转并打印到控制台。

Until now i manage to do something like: 到目前为止,我设法做类似的事情:

public static void reverseText(String filePath, int colle) // int colle = n-th line
{
    BufferedReader fileRead = null;

    String line;        

    try
    {
        File file = new File(filePath);
        if (!(file.exists()))
            file.createNewFile();

        fileRead = new BufferedReader(new FileReader(file));

        while ((line = fileRead.readLine()) != null)
        { 

        }


        fileRead.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

What I mean is that in file for example are following lines of text (int colle = 2, so we get every 2nd line form file) 我的意思是,例如在文件中是以下几行文本(int colle = 2,所以我们得到每个第二行表格文件)

first line second line <-- this first line second line <-此
third line fourth line <-- and this third line fourth line

(read file and reverse get lines) --> Output print out in console: (读取文件和反向获取行)->在控制台中输出打印输出:

"enil dnoces" "enil htruof" “ enil dnoces”“ enil htruof”

I guess for reverse I should use StringBuilder but first i need get these lines and I have no idea how to read every n-th line... 我想反向来说我应该使用StringBuilder,但是首先我需要获取这些行,而且我不知道如何读取每第n行...

Thanks for any help! 谢谢你的帮助!

Solution below read all lines and then select every n-line, reverse and print. 下面的解决方案读取所有行,然后选择每条n行,反转并打印。

public static void main(String[] args) throws IOException {
    int frequency = 5;
    final List<String> strings = Files.readAllLines(Paths.get("/home/test.txt"));
    List<String> collect = IntStream.range(0, strings.size())
            .filter(c -> c % frequency == 0)
            .mapToObj(c -> strings.get(c))
            .collect(Collectors.toList());

    collect.forEach(str ->
            System.out.println(new StringBuilder(str).reverse())
    );
}

IntStream is necessary to get specific lines from collection strings. 要从集合字符串中获取特定行,必须使用IntStream。 Then one by one lines are reversed and printed. 然后将一行一行反转并打印。 It can be one liner but decided to make it more readable. 它可以是一种衬纸,但决定使其更具可读性。

Use line counter to control loop iterations: 使用行计数器来控制循环迭代:

int n = 3;
StringBuilder sb = new StringBuilder();
for (int i = 0; (line = fileRead.readLine()) != null; i++) {
     if (i % n == 0) {
        sb.delete(0, line.length())
        System.out.println(sb.append(line).reverse());
     }
}

You still can do it using while defining a variable for that. 您仍然可以while定义变量的while使用它。

Thanks everybody for help. 谢谢大家的帮助。 Below i posted working code for my expectations 下面我发布了我期望的工作代码

public static void reverseText(String filePath, int colle)
{   
    BufferedReader fileRead = null;
    StringBuilder builder = new StringBuilder();
    int lineCounter = 0;
    String line;      

    try
    {
        File file = new File(filePath);
        if (!(file.exists()))
                file.createNewFile();

        fileRead = new BufferedReader(new FileReader(file));

        while ((line = fileRead.readLine()) != null)
        { 
            lineCounter++;
            if (lineCounter %colle == 0)
            {
                builder.delete(0, line.length());
                System.out.println(builder.append(line).reverse());
            } 
        }

        fileRead.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM