如何使用行号在java中逐行读取文件

时间:2013-12-12 12:02:51

标签: java filereader split

我想逐行读取java中的文件。例如,我有一个文件new.txt

new.txt

Log points: 

1.  We develop a project about Preventive Maintenance Checklist for Employees on PC/Tablet using Java.
2.  We here have check boxes for checking the list of works for quarterly, half yearly and annually.
3.  This tool is created to check the lists.
4.  This tool is created to make no human errors
5.  This tool is created using eclipse development tool.
6.  Eclipse is an IDE – Integrated development environment.
7.  Eclipse is a java development tool with many plug-ins and supports.
8.  We use WAMP server as database server which hosts the database connectivity.
9.  The database is created using the Mysql. Mysql is free source tool for creating database.
10.  The project has a user interface which is created using JSP –Java server pages.
11.  Jsp is used for user interface designing and it is compiled in java compiler.
12.  Servlets are server side scripting. Which accepts client side requests from the jsp and it connects with the business logic and gives the response.
13. Our application runs in Apache tom cat server. It is free server developed and released by apache software foundation.
14. This server is hosts the application and run.
15. In our project we give request from the jsp and we give the defined values to the servlets which runs in the server. The servlets takes the response and gives the expected output. 
16. The values are stored in the database. And can be retrieved and deleted in future.
17. This project gives the complete information and logs the information provided.
18. We used MIRRA ,DNS, Sort and merge, profiler.
19.  We also logs the time starts and time ends and it schedules that way.
20. We also logs the output, issues, action required etc.

现在我将根据行号阅读。并希望将其写入新文件中。例如,文件f1.txt中的第1-3行和第3-6行f2.txt中的第1-3行。我知道要写另一个文件。但不知道如何在java中拆分它。

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

        while(br.readLine()!=null)
        {
        String s=br.readLine();


        counter++;
        }


        int size=counter/9;

            System.out.println(counter);

       for(int id=0;id<size;id++)
       {

           //how to split the files and read And from here no idea
       }

1 个答案:

答案 0 :(得分:0)

你可以从中得到一些想法。这不是一个很好的解决方案,但你可以从中得到一些想法

Scanner scanner=new Scanner(new File("/home/ruchira/Test.txt"));
FileWriter fileWriter1=new FileWriter(new File("/home/ruchira/1.txt"));
// you will have to create more files. 
while (scanner.hasNextLine()){
    String line=scanner.nextLine();
    int lineNum=0;
    try {
        if(!line.equals("")){
            lineNum=Integer.parseInt(line.split("\\.")[0]);
            if(lineNum>=1 && lineNum<=3){ // check for line numbers
                fileWriter1.append(line) ;
                fileWriter1.append("\n");
            }
            // there will be more if conditions in your case.
        }

    }catch (NumberFormatException | IndexOutOfBoundsException e){

    }

}
fileWriter1.close();