Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FilenameFilter;
  3.  
  4. public class HW01_1 {
  5.     public static void main(String [] args)
  6.     {
  7.         if(args.length != 0)
  8.         {
  9.             File file1 = new File (args[0]);
  10.             File [] files = null;
  11.            
  12.             if(file1.isDirectory())
  13.                 files = file1.listFiles(new DirFilter(".txt"));
  14.            
  15.             long length=0;
  16.            
  17.             if(files != null)
  18.             {
  19.                 for(File file2 : files)
  20.                     length += file2.length();
  21.             }
  22.            
  23.             System.out.println(length/ files.length);
  24.         }
  25.     }
  26. }
  27. class DirFilter implements FilenameFilter
  28. {
  29.     private String check;
  30.  
  31.     DirFilter(String check) {
  32.         this.check = check;
  33.     }
  34.     @Override
  35.     public boolean accept(File dir, String name) {
  36.         String tmp = new File(name).getName();
  37.         return tmp.endsWith(check);
  38.     }
  39. }
  40.  
  41. import java.io.*;
  42. import java.nio.charset.StandardCharsets;
  43.  
  44. public class HW01_2 {
  45.     public static void main(String[] args) {
  46.         try
  47.         {
  48.             File src = new File("source.txt");
  49.             String text;
  50.            
  51.             if(!src.exists())
  52.             {
  53.                 BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  54.                 text = bf.readLine();
  55.  
  56.                 PrintWriter pw = new PrintWriter("source.txt");
  57.                 pw.println(text);
  58.                 pw.close();
  59.             }
  60.          
  61.  
  62.  
  63.             File dest = new File("destination.txt");
  64.             FileInputStream input = new FileInputStream(src);
  65.  
  66.             RandomAccessFile dest2 = new RandomAccessFile(dest, "rw");
  67.             dest2.setLength(src.length());
  68.  
  69.             long position = src.length();
  70.  
  71.             int b;
  72.             while((b = input.read()) != -1)
  73.             {
  74.                 String s = Character.toString((char)b);
  75.  
  76.                 byte [] buff = s.getBytes();
  77.                 position -= buff.length;
  78.                 dest2.seek(position);
  79.  
  80.                 dest2.write(buff);
  81.             }
  82.  
  83.             input.close();
  84.             dest2.close();
  85.         }
  86.         catch (IOException e) {
  87.             e.printStackTrace();
  88.         }
  89.     }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement