Advertisement
GraionDilach

Java.IO #1

Nov 28th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Random;
  3. import java.util.StringTokenizer;
  4.  
  5.  
  6. public class MainIO {
  7.  
  8.     /**
  9.      * @param args
  10.      */
  11.     public static void main(String[] args) {
  12.         // TODO Auto-generated method stub
  13.        
  14.         try {
  15.             //1. feladat
  16.             FileInputStream TestExample = new FileInputStream("d:/zimmgy/IOex1.txt");
  17.             FileOutputStream TestExampleCopy = new FileOutputStream("d:/zimmgy/IOex11.txt");
  18.            
  19.             int c;         
  20.             while ((c = TestExample.read()) != -1){
  21.                 TestExampleCopy.write(c);
  22.             }
  23.            
  24.             TestExample.close();
  25.             FileInputStream TestExample2 = new FileInputStream("d:/zimmgy/IOex1.txt");
  26.             FileOutputStream TestExampleCopy2 = new FileOutputStream("d:/zimmgy/IOex12.txt");
  27.            
  28.             byte[] buf = new byte[10];
  29.             while ((c =TestExample2.read(buf)) == buf.length){
  30.                 TestExampleCopy2.write(buf);
  31.             }
  32.             TestExampleCopy2.write(buf, 0, c);
  33.            
  34.             FileInputStream TestExample3 = new FileInputStream("d:/zimmgy/IOex1.txt");
  35.             FileOutputStream TestExampleCopy3 = new FileOutputStream("d:/zimmgy/IOex13.txt");
  36.             byte[] fullbuf = new byte[TestExample3.available()];
  37.             TestExample3.read(fullbuf);
  38.             TestExampleCopy3.write(fullbuf);
  39.            
  40.            
  41.             //2. feladat
  42.             FileInputStream TestExample21 = new FileInputStream("d:/zimmgy/IOex2.txt");
  43.             byte[] recordbuf = new byte[512];
  44.             TestExample21.skip(512);
  45.             while ((c =TestExample21.read(recordbuf)) == recordbuf.length){
  46.                 System.out.println(new String(recordbuf));
  47.             }
  48.             System.out.println(new String(recordbuf, 0, c));
  49.            
  50.             //3. feladat
  51.             FileInputStream TestExample31 = new FileInputStream("d:/zimmgy/IOex3.txt");
  52.             System.out.println("Fájlméret: " + TestExample31.available());
  53.             byte[] buffor3 = new byte[TestExample31.available()/4];
  54.             TestExample31.read(buffor3);
  55.             System.out.println(new String(buffor3));
  56.             System.out.println("Hátravan: " + TestExample31.available());
  57.             byte[] buffor32 = new byte[buffor3.length/2];
  58.             System.out.println(new String(buffor32));
  59.             TestExample31.skip(buffor3.length);
  60.             c =TestExample31.read(buffor3);
  61.             System.out.println(new String(buffor3, 0, c));
  62.            
  63.             //6. feladat
  64.             int[] Szvatopluk = new int[10];
  65.             DataOutputStream TestExample61 = new DataOutputStream (new FileOutputStream ("d:/zimmgy/IOex6.bin"));
  66.             Random R = new Random();
  67.             for (int i =0; i<10; i++){
  68.                 Szvatopluk[i] = R.nextInt();
  69.                 TestExample61.writeInt(Szvatopluk[i]);
  70.                 System.out.println(Szvatopluk[i]);
  71.             }
  72.             System.out.println();
  73.             TestExample61.close();
  74.             DataInputStream TestExample62 = new DataInputStream (new FileInputStream ("d:/zimmgy/IOex6.bin"));
  75.             for (int i =0; i<10; i++){
  76.                 System.out.println(TestExample62.readInt());
  77.             }
  78.            
  79.             //soronkénti olvasás
  80.             BufferedReader TestExampleAdded11 = new BufferedReader (new FileReader ("d:/zimmgy/IOex1.txt"));
  81.             String bufforAdded1;
  82.             int WordCount =0, LineCount=0;
  83.             while((bufforAdded1 = TestExampleAdded11.readLine()) != null){
  84.                 System.out.println(bufforAdded1);
  85.                 LineCount++;
  86.                 StringTokenizer st = new StringTokenizer (bufforAdded1);
  87.                 WordCount += st.countTokens();
  88.             }
  89.            
  90.             System.out.println("Szavak száma: " + WordCount + ", sorok száma : " + LineCount);
  91.            
  92.         } catch (Exception e) {
  93.             try {
  94.                 @SuppressWarnings("unused")
  95.                 FileOutputStream BackupTest = new FileOutputStream("d:/zimmgy/IOex1.txt");
  96.                 @SuppressWarnings("unused")
  97.                 FileInputStream TestExample = new FileInputStream("d:/zimmgy/IOex1.txt");
  98.             }
  99.             catch (Exception f){
  100.                 System.out.println ("Java is a haywire!");
  101.             }
  102.         }
  103.  
  104.     }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement