import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class TestIO {
private List<String> strList= new LinkedList<String>();
//读取文件,并放入一个LinkedList中.
public void input() {
try {
BufferedReader reader = new BufferedReader(new FileReader(new File("E:\\myworks2\\MyJavaProject\\MyFolder\\helloworld")));
while(true) {
try {
String s = reader.readLine();
if(s==null) {
break;
}
strList.add(s);
} catch (IOException e) {
e.printStackTrace(); } }
} catch (FileNotFoundException e) {
e.printStackTrace(); } }
public List<String> getList() {
return this.strList; }
public static void main(String[] args) {
TestIO testIO = new TestIO();
testIO.input();
List<String> oList = testIO.getList();
//为什么减1,因为java是从零开始算的,list.size()=22 是0~21 ;
for(int i =oList.size()-1; i>=0; i--) {
System.out.println(oList.get(i)); }
try {
//用BufferedWriter把读出来的东西写入到另一个文件中.
BufferedWriter writer = new
BufferedWriter(new FileWriter(new File("E:\\myworks2\\MyJavaProject\\MyFolder\\helloworld2")));
for(int i = 0; i <oList.size()-1; i++) {
writer.append(oList.get(i));
writer.newLine();//启一新行 }
writer.flush();
} catch (IOException e) { // TODO Auto-generated
catch block e.printStackTrace(); }
}
}
转载于:https://my.oschina.net/u/131206/blog/98074