Advertisement
StJimmy

JAVA I/O

Mar 28th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.78 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package iokol2016;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.BufferedWriter;
  10. import java.io.File;
  11. import java.io.FileFilter;
  12. import java.io.FileNotFoundException;
  13. import java.io.FileReader;
  14. import java.io.FileWriter;
  15. import java.io.FilenameFilter;
  16. import java.io.IOException;
  17. import java.io.RandomAccessFile;
  18.  
  19. /**
  20.  *
  21.  * @author jimmy
  22.  */
  23. public class IOkol2016 {
  24.  
  25.     /**
  26.      * @param args the command line arguments
  27.      */
  28.     public static void main(String[] args) throws IOException {
  29.         // TODO code application logic here
  30.         //zapisaj("text.txt","napocetok.txt");
  31.         //zapisajSoBuff("text.txt","sobuf.txt");
  32.         //napocetok("text.txt","odpocetok.txt");
  33.         //napocetok("text1.txt","odpocetok.txt");
  34.         ///javatxt("odpocetok.txt","java.txt");
  35.         filter(".");
  36.     }
  37.     public static void zapisaj(String in , String out) throws FileNotFoundException, IOException{
  38.        
  39.         File in1=new File(in);
  40.         File out1=new File(out);
  41.        
  42.         RandomAccessFile r=new RandomAccessFile(in1,"r");
  43.         RandomAccessFile rw=new RandomAccessFile(out1,"rw");
  44.         try{
  45.            
  46.             int kraj=(int) r.length();
  47.             int poc=0;
  48.             byte[] bajt = new byte[kraj];
  49.             int c;
  50.             rw.seek(rw.length());
  51.            
  52.             while((c=r.read(bajt))!=-1){
  53.                
  54.                 rw.write(bajt,0,kraj);
  55.             }
  56.            
  57.            
  58.            
  59.            
  60.         }finally{
  61.             if(rw!=null){
  62.                
  63.                 rw.close();
  64.             }
  65.             if(r!=null){
  66.                 r.close();
  67.             }
  68.            
  69.            
  70.         }
  71.        
  72.     }
  73.     public static void zapisajSoBuff(String in, String out) throws FileNotFoundException, IOException{
  74.        
  75.         File in1 = new File(in);
  76.         File out1=new File(out);
  77.        
  78.         FileReader fr = new FileReader(in1);
  79.         BufferedReader br=new BufferedReader(fr);
  80.         FileWriter fw=new FileWriter(out1,true);
  81.         BufferedWriter bw = new BufferedWriter(fw);
  82.        
  83.         try{
  84.            
  85.             String s;
  86.            
  87.             while((s=br.readLine())!=null){
  88.                
  89.                 bw.write(s);
  90.             }
  91.            
  92.            
  93.         }finally{
  94.            
  95.             if(bw!=null){
  96.                 bw.close();
  97.             }
  98.             if(br!=null){
  99.                 br.close();
  100.             }
  101.            
  102.         }
  103.     }
  104.     public static void napocetok(String in ,String out) throws FileNotFoundException, IOException{
  105.        
  106.        
  107.         File in1 = new File(in);
  108.         File out1=new File(out);
  109.        
  110.         RandomAccessFile r=null;
  111.         RandomAccessFile rw=null;
  112.         try{
  113.            
  114.            r=new RandomAccessFile(in,"r");
  115.            rw=new RandomAccessFile(out,"rw");
  116.            
  117.            rw.seek(rw.length());
  118.            int kraj=(int) r.length();
  119.            byte[] bajt=new byte[kraj];
  120.            int c;
  121.            while((c=r.read(bajt))!=-1){
  122.                
  123.                rw.write(bajt,0,kraj);
  124.                
  125.            }
  126.            
  127.            
  128.            
  129.         }finally{
  130.             if(r!=null){
  131.                
  132.                 r.close();
  133.             }
  134.             if(rw!=null){
  135.                 rw.close();
  136.             }
  137.         }
  138.        
  139.        
  140.        
  141.        
  142.        
  143.        
  144.        
  145.     }
  146.     public static void javatxt(String in , String out) throws FileNotFoundException, IOException{
  147.         RandomAccessFile r=null;
  148.         RandomAccessFile rw=null;
  149.         try{
  150.            
  151.            r=new RandomAccessFile(in,"r");
  152.            rw=new RandomAccessFile(out,"rw");
  153.            
  154.            //rw.seek(rw.length());
  155.            int kraj=(int) r.length();
  156.            byte[] bajt=new byte[kraj];
  157.            int c;
  158.            while((c=r.read(bajt))!=-1){
  159.                
  160.                rw.write(bajt,0,kraj);
  161.                
  162.            }
  163.            
  164.            
  165.            
  166.         }finally{
  167.             if(r!=null){
  168.                
  169.                 r.close();
  170.             }
  171.             if(rw!=null){
  172.                 rw.close();
  173.             }
  174.         }
  175.        
  176.     }
  177.     public static void filter(String in){
  178.        
  179.        
  180.         FileFilter file = new FileFilter() {
  181.             @Override
  182.             public boolean accept(File pathname) {
  183.                 //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  184.            
  185.                 return pathname.isFile() && pathname.getName().endsWith(".dat");
  186.             }
  187.         };
  188.         FilenameFilter name = new FilenameFilter(){
  189.             @Override
  190.             public boolean accept(File dir, String name) {
  191.                 //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  192.                 String ime = name.toLowerCase();
  193.                 if(ime.endsWith(".txt")){
  194.                     return true;
  195.                 }
  196.                 else
  197.                     return false;
  198.            
  199.             }
  200.            
  201.            
  202.         };
  203.        
  204.         File f = new File(in);
  205.        
  206.         for(File f1 : f.listFiles(file)){
  207.            
  208.             System.out.println(f1.getName());
  209.         }
  210.        
  211.         for(File f1 : f.listFiles(name)){
  212.             if(f1.isFile()){
  213.                
  214.                 System.out.println(f1.getName() + " " +f1.getAbsolutePath());
  215.             }
  216.             }
  217.     }
  218.    
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement