import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
//1、(字符流、转换流)完成下面功能:
//事先在准备好一个test.txt的文本文件,该文件具有多行。
//如:
//test.txt
//床前明月光
//疑是地上霜
//举头望明月
//低头思故乡
//利用字节流+转换流读入这个文件,然后按照行的顺序,
//写到test2.txt文件中,例如:
//test2.txt
//低头思故乡
//举头望明月
//疑是地上霜
//床前明月光
public class Test1
{
public static void main(String[] args) throws IOException
{
write1(); //从控制台输入;
write2();
}
private static void write1() throws IOException
{
File file = new File("d:\\a\\text.txt");
if(!file.exists())
{
file.createNewFile();
}
//将字节流转换为字符流;
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
String line = null;
System.out.println("input:");
while((line = is.readLine()) != null)
{
if(line.equals("over"))
break;
bw.write(line);
bw.newLine();
bw.flush();
}
is.close();
bw.close();
}
private static void write2() throws IOException
{
File file = new File("d:\\a\\text.txt");
File file2 = new File("d:\\a\\text2.txt");
if(!file2.exists())
{
file2.createNewFile();
}
LineNumberReader lnr = new LineNumberReader(new FileReader(file));
BufferedWriter bw2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file2)));
StringBuffer sb = new StringBuffer();
String line = null;
while((line = lnr.readLine()) != null)
{
sb.append(line).append("?");
}
String[] str = new String[lnr.getLineNumber()];
sb.deleteCharAt(sb.lastIndexOf("?"));
str = sb.toString().split("?");
for(int i=lnr.getLineNumber()-1; i>=0; i--)
{
bw2.write(str[i]);
bw2.newLine();
bw2.flush();
}
lnr.close();
bw2.close();
}
}