使用随机文件流类RandomAccessFile将一个文本文件倒置读出。

来源:互联网 发布:二分搜索算法代码 编辑:程序博客网 时间:2024/06/06 05:58
import java.io.*;   
import java.util.*;  
//使用随机文件流类RandomAccessFile将一个文本文件倒置读出。
public class Home {
 
   public static final void main(String[] args) {   
       try {   
           File f = new File("exp.txt");   
           if(!f.exists()) {   
               f.createNewFile();   
           }   
           RandomAccessFile raf = new RandomAccessFile(f,"rw");   
           String str = new String();   
           System.out.println("请输入你想要输出的字母:");
           str = new Scanner(System.in).next();   
           for(int i=0; i<str.length(); i++) {   
               raf.write(str.charAt(i));   
           }   
           raf.writeUTF(str);   
           long len = str.length();   
           while(0 != len--) {   
               raf.seek(len);   
               char ch = (char)raf.read();   
               System.out.print(ch+" ");   
           }   
           raf.close();   
       } catch(IOException e) {   
           e.printStackTrace();   
       }   
   }   
}
0 0
原创粉丝点击