2013-12-12 28 views
-3

我想在java中逐行讀取文件。比如我有一個文件new.txt如何在java中使用行號讀取文件行

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。我知道寫在另一個文件。但不知道如何在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 
     } 
+0

LineNumberReader。 – bmargulies

+0

我用編碼編輯過。幫我。 – seenome

+0

使用Apache Common IO FileUtil http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html – Makky

回答

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(); 
相關問題