我初学java
编程题:
要求打开一个文件,一次读取其内的一行文本,令每一行形成一个String,并将读出的String对象以相反的次序存储在test.txt文件中。

解决方案 »

  1.   

    public static int test() {
    Scanner s = null;
    try {
    s = new Scanner(new File("d:\\ApplicationResources_zh_CN.txt"));
    List<String> list = new ArrayList<String>();
    FileWriter fw = new FileWriter(new File("d:\\test.txt"),true);
    while(s.hasNext()) {
    String copy = s.nextLine();
    System.out.println(copy);
    list.add(copy);
    }
    System.out.println("-----------------------------");
    for(int i=list.size()-1;i>0;i--) 
    {
    fw.write(list.get(i)+"\n");
    }
    fw.close();
    System.out.println("over");
    return 0;
    }catch(Exception e) {
    e.printStackTrace();
    return 1;
    }finally{
    s.close();
    }

    }
      

  2.   


    import java.io.*;
    import java.util.*;
    class zhenfan 
    {
    static private final String newline = "\n";
    public static void main(String[] args) 
    {
    BufferedReader br =null;
    PrintWriter pw=null;

    String path1="d:\\test\\aaa.txt";
    String path2="d:\\test\\bbb.txt";
    int num=1;
    Map<Integer,String> result= new HashMap<Integer,String>();
    //List<String> result = new ArrayList<String>();
    try{
    br = new BufferedReader(new FileReader(path1));
    pw=new PrintWriter(new BufferedWriter(new FileWriter(path2,true)));

    String str = br.readLine();
    while(str!=null){
    result.put(num,str);
    num++;
    str = br.readLine();
    }
    num--;
    //System.out.println(result.get(num));
    for(int i=num;i>=1;i--){
    pw.println(result.get(i));
    }
    }catch(Exception e){
    e.printStackTrace();
    }finally{
    try{
    br.close();
    pw.close();
    }catch(Exception e2){
    e2.printStackTrace();
    }
    }
    }
    }
    d:\test下创建测试文件就可以了
      

  3.   

    测试文件ApplicationResources_zh_CN写下
    想改随便你
      

  4.   

    import java.io.*;
    import java.util.*;
    class zhenfan 
    {
        static private final String newline = "\n";
        public static void main(String[] args) 
        {
            BufferedReader br =null;
            PrintWriter pw=null;
            
            String path1="d:\\aaa.txt";
            String path2="d:\\bbb.txt";
            int num=1;
            //自己定义字符串数字的大小
            String[] result= new String[10];
            //List<String> result = new ArrayList<String>();
            try{
                br = new BufferedReader(new FileReader(path1));
                pw=new PrintWriter(new BufferedWriter(new FileWriter(path2,true)));
                
                String str = br.readLine();
                while(str!=null){
                   // System.out.println(str);
                    result[num]=str;
                   // System.out.println(result[num]);
                    num++;
                    str = br.readLine();            }
                num--;
                //System.out.println(result.get(num));
                //或者(j = num;j>=1;j--)
            for(int j = 1;j<=num;j++){    
                for(int i=result[j].length()-1;i>=0;i--){
                    pw.print(result[j].charAt(i));
                }
                pw.println();
            }
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                try{
                    br.close();
                    pw.close();
                    System.out.print("OK");
                }catch(Exception e2){
                    e2.printStackTrace();
                }
            }
        }
    }由于我用的是JDK 1.4,好多不支持,所以这个样子修改了下。验证过了,可以的。
      

  5.   

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    public class GetText {
    public static void main(String[] args) { new GetText().getText();
    }

    public void getText(){

    try {
    List<String> al = new ArrayList<String>();
    BufferedReader in = new BufferedReader(new FileReader("d:\\server.txt"));
    BufferedWriter out = new BufferedWriter(new FileWriter("d:\\test.txt"));
    String readline = "";
    while((readline = in.readLine())!=null){
    al.add(readline);
    }
    in.close();
    for(int i=al.size()-1;i>=0;i--){
    out.write(al.get(i));
    out.newLine();
    }
    out.flush();
    out.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }