Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Random;
- import java.util.StringTokenizer;
- public class MainIO {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- try {
- //1. feladat
- FileInputStream TestExample = new FileInputStream("d:/zimmgy/IOex1.txt");
- FileOutputStream TestExampleCopy = new FileOutputStream("d:/zimmgy/IOex11.txt");
- int c;
- while ((c = TestExample.read()) != -1){
- TestExampleCopy.write(c);
- }
- TestExample.close();
- FileInputStream TestExample2 = new FileInputStream("d:/zimmgy/IOex1.txt");
- FileOutputStream TestExampleCopy2 = new FileOutputStream("d:/zimmgy/IOex12.txt");
- byte[] buf = new byte[10];
- while ((c =TestExample2.read(buf)) == buf.length){
- TestExampleCopy2.write(buf);
- }
- TestExampleCopy2.write(buf, 0, c);
- FileInputStream TestExample3 = new FileInputStream("d:/zimmgy/IOex1.txt");
- FileOutputStream TestExampleCopy3 = new FileOutputStream("d:/zimmgy/IOex13.txt");
- byte[] fullbuf = new byte[TestExample3.available()];
- TestExample3.read(fullbuf);
- TestExampleCopy3.write(fullbuf);
- //2. feladat
- FileInputStream TestExample21 = new FileInputStream("d:/zimmgy/IOex2.txt");
- byte[] recordbuf = new byte[512];
- TestExample21.skip(512);
- while ((c =TestExample21.read(recordbuf)) == recordbuf.length){
- System.out.println(new String(recordbuf));
- }
- System.out.println(new String(recordbuf, 0, c));
- //3. feladat
- FileInputStream TestExample31 = new FileInputStream("d:/zimmgy/IOex3.txt");
- System.out.println("Fájlméret: " + TestExample31.available());
- byte[] buffor3 = new byte[TestExample31.available()/4];
- TestExample31.read(buffor3);
- System.out.println(new String(buffor3));
- System.out.println("Hátravan: " + TestExample31.available());
- byte[] buffor32 = new byte[buffor3.length/2];
- System.out.println(new String(buffor32));
- TestExample31.skip(buffor3.length);
- c =TestExample31.read(buffor3);
- System.out.println(new String(buffor3, 0, c));
- //6. feladat
- int[] Szvatopluk = new int[10];
- DataOutputStream TestExample61 = new DataOutputStream (new FileOutputStream ("d:/zimmgy/IOex6.bin"));
- Random R = new Random();
- for (int i =0; i<10; i++){
- Szvatopluk[i] = R.nextInt();
- TestExample61.writeInt(Szvatopluk[i]);
- System.out.println(Szvatopluk[i]);
- }
- System.out.println();
- TestExample61.close();
- DataInputStream TestExample62 = new DataInputStream (new FileInputStream ("d:/zimmgy/IOex6.bin"));
- for (int i =0; i<10; i++){
- System.out.println(TestExample62.readInt());
- }
- //soronkénti olvasás
- BufferedReader TestExampleAdded11 = new BufferedReader (new FileReader ("d:/zimmgy/IOex1.txt"));
- String bufforAdded1;
- int WordCount =0, LineCount=0;
- while((bufforAdded1 = TestExampleAdded11.readLine()) != null){
- System.out.println(bufforAdded1);
- LineCount++;
- StringTokenizer st = new StringTokenizer (bufforAdded1);
- WordCount += st.countTokens();
- }
- System.out.println("Szavak száma: " + WordCount + ", sorok száma : " + LineCount);
- } catch (Exception e) {
- try {
- @SuppressWarnings("unused")
- FileOutputStream BackupTest = new FileOutputStream("d:/zimmgy/IOex1.txt");
- @SuppressWarnings("unused")
- FileInputStream TestExample = new FileInputStream("d:/zimmgy/IOex1.txt");
- }
- catch (Exception f){
- System.out.println ("Java is a haywire!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement