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();
}
}
}
ohh我忘記把.txt放到最後,但文件只輸出一個字[yaM],而我有一個大文本 – Ivawen