2016-03-01 14 views
0

我正在編寫一個將IO輸入文件與文本一起讀取的算法,讀取它後處理它,然後用反向文本輸出文件。 例如:用其反向文件替換文件的行

你好字

卓爾2009東海生日賀。

該程序編譯但文件夾「text_Output」爲空。

有些洞察?

import java.util.*; 
import java.io.*; 
import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.io.PrintWriter; 
import java.io.File; 


public class Reverse 
{ 
    public static void main(String[] args) throws FileNotFoundException { 

    //read file from user 
    Scanner Input = new Scanner (new File("text_input.txt")) ; 
    // declar arraylist 
    ArrayList <String> Lines = new ArrayList <String>(); 

    //use while loop to copy by line all data from text to array list 
    while (Input.hasNextLine()) 
    { 
     //add line to array list 
     Lines.add(Input.nextLine()); 
    } 
    Input.close(); 

    // create new output file 
    PrintWriter Output = new PrintWriter(new File("text_Output")); 

    //copy data from lines arraylists to new text file 
    for (int i =0; i < Lines.size();i++) 
    { 
     //copy first element into char array 
     char[]strLine = Lines.get(i).toString().toCharArray(); 

     //copy each char to string array in new text file 
     for(int j=strLine.length-1;j>=0;j--) 
     { 
     //copy index char in file 
     Output.print(strLine[j]); 
     Output.println(); 
     } 
     //close the files 
     Input.close(); 
     Output.close(); 
    } 
    } 
} 

回答

1

在用於寫入的循環,它僅讀取第一行,然後將輸入和輸出closed.I試圖與輸入數據的多行和工作正常與下文提到的代碼。

 for (int i =0; i < Lines.size();i++) 
     { 
      //copy first element into char array 
      char[]strLine = Lines.get(i).toString().toCharArray(); 

      //copy each char to string array in new text file 
      for(int j=strLine.length-1;j>=0;j--) 
      { 
      //copy index char in file 
      Output.print(strLine[j]); 

      } 
      Output.println(); 
     } 
// here close the input and output. 
} 
} 
+0

ohh我忘記把.txt放到最後,但文件只輸出一個字[yaM],而我有一個大文本 – Ivawen