import java.io.*;public class Reverse {
static int i;    
    public static boolean catStdinToFile(String fileName) {
        File file = new File(fileName);
        PrintWriter writer = null;
        BufferedReader in = null;
        i = 0;
        try {
            writer = new PrintWriter(new FileWriter(file));
            System.out.println("请输入文件内容,输入quit结束:");
            in = new BufferedReader(new InputStreamReader(System.in));
            String inputLine = null;
            while (((inputLine = in.readLine()) != null) && (!inputLine.equals("quit"))) {
                writer.println(inputLine);
                i++;
            }
            writer.flush();
            writer.close();
            return true;
        } catch (IOException e) {
            System.out.println(e.getMessage());
            return false;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }      public static boolean readFileByLines(String filename) {
     File file = new File(filename);
     BufferedReader in = null;
     String temp = "";
     try {
     in = new BufferedReader(new InputStreamReader(new FileInputStream("temp.txt")));
     String inputLine = null;
     //此处开始逆序,但逆序不成功,想用i进行逆序....
     while((i>=0) && ((inputLine = in.readLine())!= null)) {
     temp+=inputLine+"\n";
     i--;
     }
     System.out.println(temp);
     return true;
     }catch(FileNotFoundException e) {
     e.printStackTrace();
     return false;
     }catch(IOException e) {
     System.out.println(e.getMessage());
     return false;
     }finally {
     if(in != null) {
     try {
     in.close();
     }catch(IOException e) {
     e.printStackTrace();
     }
     }
     }
    }
    
    public static void main (String[] args) {
        String fileName = "temp.txt";
        Reverse.catStdinToFile(fileName);
        System.out.println();
        System.out.println("输出文件的内容:");
        Reverse.readFileByLines(fileName);
    }}

解决方案 »

  1.   

    第一个想法就是用一个Stack来实现
    把你读到的放到一个Stack中,再取出来,不就逆序了吗?
      

  2.   

    temp=inputLine+temp+"\n"
    你这样试试.
      

  3.   


    import java.io.*; 
    import java.util.*;
    public class Reverse {    
        public  boolean catStdinToFile(String fileName) { 
            File file = new File(fileName); 
            PrintWriter writer = null; 
            BufferedReader in = null;         
            try { 
                writer = new PrintWriter(new FileWriter(file)); 
                System.out.println("请输入文件内容,输入quit结束:"); 
                in = new BufferedReader(new InputStreamReader(System.in)); 
                String inputLine = null; 
                while (((inputLine = in.readLine()) != null) && (!inputLine.equals("quit"))) 
                { 
                    writer.println(inputLine); 
                    map.put(i,inputLine);
                    i++; 
                } 
                writer.flush(); 
                writer.close(); 
                return true; 
            } catch (IOException e) { 
                System.out.println(e.getMessage()); 
                return false; 
            } finally { 
                if (in != null) { 
                    try { 
                        in.close(); 
                    } catch (IOException e) { 
                        e.printStackTrace(); 
                    } 
                } 
            } 
        } 
          
        public  boolean readFileByLines(String filename) { 
        //File file = new File(filename); 
        BufferedReader in = null; 
        String temp = ""; 
        try { 
        in = new BufferedReader(new InputStreamReader(new FileInputStream(filename))); 
        String inputLine = null; 
        //此处开始逆序,但逆序不成功,想用i进行逆序.... 
        while((i>=0) && ((inputLine = in.readLine())!= null)) { 
        temp += inputLine+"\n"; 
        i--;         
        output = map.get(i);
        System.out.println(output); 
                   
        }        
        return true; 
        }catch(FileNotFoundException e) { 
        e.printStackTrace(); 
        return false; 
        }catch(IOException e) { 
        System.out.println(e.getMessage()); 
        return false; 
        }finally { 
        if(in != null) { 
        try { 
        in.close(); 
        }catch(IOException e) { 
        e.printStackTrace(); 
        } 
        } 
        } 
        } 
        
        public static void main (String[] args) { 
         Reverse first = new Reverse();
            String fileName = "temp.txt"; 
            first.catStdinToFile(fileName); 
            System.out.println(); 
            System.out.println("输出文件的内容:"); 
            first.readFileByLines(fileName); 
            //first.reverseTest(fileName);
        } 
        private Map<Integer,String> map = new HashMap<Integer,String>();
        private String output;
        private int i = 0; 
    } 用了Map,不知道合不合适,不合适就当我没说。
      

  4.   


    你原来是
    temp+=inputLine+"\n"; 相当于temp=temp+inputLine+"\n";
    我让你改成,temp=inputLine+temp+"\n";
    这样就能逆序了,还要什么map,stack...