2013-10-31 64 views
0

我想從文件中讀取偶數行,並反向打印,同時保持奇數行,因爲它是 這裏是我試過的代碼打印在反向如何從文件中讀取偶數行並反向打印,同時保持奇數行,因爲它是?

import java.io.BufferedReader; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 


public class RevWords 
{ 
public static void main(String[] args) throws IOException 
{ 
    BufferedReader br = new BufferedReader(new InputStreamReader(new  
    FileInputStream("home.txt"), "UTF-8"));     

    String lines = br.readLine(); 
    ArrayList<String> buffer = new ArrayList<String>(); 
    while (lines!=null) 

    { 

    if (lines!=null) 

        { 
      buffer.add("\n"); 

      StringBuilder str = new StringBuilder(); 
      String[] splitStr = lines.split(" "); 
      for (int i = splitStr.length; i > 0; i--) { 
       str.append(splitStr[i - 1]).append(" "); 
      } 

      buffer.add(str.toString()); 
     } 


     lines=br.readLine(); 
    } 


    for(int i = buffer.size()-1; i>=0; i--) 
    { 

     System.out.print(buffer.get(i)); 
    } 

    br.close(); 
    } 
} 

我了home.txt的所有行是

One reason people lie is to achieve personal power. 
Achieving personal power is helpful for someone who pretends to be more confident than 
he really is. 
For example, one of my friends threw a party at his house last month. 
He asked me to come to his party and bring a date. However, 
I didn’t have a girlfriend. One of my other friends, 
who had a date to go to the party with, asked me about my date. 
I didn’t want to be embarrassed, 
so I claimed that I had a lot of work to do. 
I said I could easily find a date even better than his if I wanted 
I also told him that his date was ugly. I achieved power to help me feel confident; 
however, I embarrassed my friend and his date. 
Although this lie helped me at the time, 
since then it has made me look down on myself. 

我的輸出是

myself. on down look me made has it then since 
time, the at me helped lie this Although 
date. his and friend my embarrassed I however, 
confident; feel me help to power achieved I ugly. was date his that him told als 
o I 
wanted I if his than better even date a find easily could I said I 
do. to work of lot a had I that claimed I so 
embarrassed, be to want didn?t I 
date. my about me asked with, party the to go to date a had who 
friends, other my of One girlfriend. a have didn?t I 
However, date. a bring and party his to come to me asked He 
month. last house his at party a threw friends my of one example, For 
is. really he than confident more be to pretends who someone for helpful is powe 
r personal Achieving 
power. personal achieve to is lie people reason One 

現在如何保持奇數行它是在轉打印偶數行ERSE

+0

不清楚你在問什麼。僅在奇數行中反轉單詞?或者顛倒線條本身的順序?或兩者? – iluxa

回答

0

下面的代碼片段會做你的工作:

br = new BufferedReader(new InputStreamReader(new FileInputStream("/home/smd/Desktop/test.txt"), "UTF-8")); 
     String lines = br.readLine(); 
     ArrayList<String> buffer = new ArrayList<String>(); 
     //Counter to keep count of line 
     int counter = 0; 
     while (lines != null) { 
      counter++; 
      buffer.add("\n"); 
      /* 
      * Check for odd line. If line is odd, revers it, else take the line as it is. 
      */ 
      if (counter % 2 == 0) { 
       StringBuilder str = new StringBuilder(); 
       String[] splitStr = lines.split(" "); 
       for (int i = splitStr.length; i > 0; i--) { 
        str.append(splitStr[i - 1]).append(" "); 
       } 
       buffer.add(str.toString()); 
      } else { 
       buffer.add(lines); 
      } 
      lines = br.readLine(); 
     } 
     /* 
     * Print from buffer. Even buffer.toString() will work, but it appends comma ',' to separate line and add braces to whole string. 
     */ 
     for (int i = 0; i < buffer.size() - 1; i++) { 
      System.out.print(buffer.get(i)); 
     } 
     br.close(); 

Modfications做你的代碼: - 使用計數器檢查Line是否是奇數。如果線是奇數反向它,否則使用原始線。 - 直接從緩衝區打印,而不是按相反順序打印。

+0

這段代碼對我來說很好,謝謝 – Mohit

+0

謝謝我將在下一次記住這一點 – Mohit

0

你可以嘗試添加計數器知道哪些線數爲偶數:

int counter = 1;  
while (lines!=null) 

{ 

if (lines!=null) 

       { 
     buffer.add("\n"); 

     StringBuilder str = new StringBuilder(); 
     if(counter%2==0){ 
      String[] splitStr = lines.split(" "); 
      for (int i = splitStr.length; i > 0; i--) { 
       str.append(splitStr[i - 1]).append(" "); 
      } 
      buffer.add(str.toString()); 
     }else{ 

      buffer.add(lines); 
     } 
    } 

    lines=br.readLine(); 
    counter++; 
} 
+0

這隻顯示偶數行,但它幫助我瞭解如何做到這一點,謝謝 – Mohit

+0

我編輯了代碼,它現在應該顯示所有行。 – majk

相關問題