JAVA - שיעור #8

1. I/O:  GUI  Graphic User Interface, 
             AWT  Abstract Window Toolkit
            JavaBeans    Swing
   Stream    java.io

2. InputStream,  OutputStream
    DataInputStream,   DataOutputStream
    BufferedInputStream,  BufferedOutputStream
    PrintStream
    FileInputStream,   FileOutputStream
    RandomAccessFile
    LineNumberInputStream

3. java.lang.System          in  out  err
    PrintStream

    DataInputStream inVal = new DataInputStream(System.in);
    String line;
    try {
      while ((line = inVal.readLine()) != null)
          System.out.println(line);
    } catch (IOException i)
          (System.out.println("An I/O Error has occurred");
   }

4. read(byte[])
    readBoolean()  readChar()  readDouble()  readFloat()
        readLine()    readLong()  readInt()
    write(int j)
    writeBoolean(boolean b)   writeBytes(String s)
    writeChar(char c)   writeDouble(double d)
    writeFloat(float f)
    writeInt(int j)          writeLong(long l)    
    hval = inVal.readInt();       setNext = inVal.readBoolean();
   

    DataOutputStream outVal = new
           DataOutputStream(System.out);
    outVal.writeBoolean(b)      outVal.writeLong(lk)

   FileInputStream fis = new FileInputStream("sal.txt");
   DataInputStream ds = new DataInputStream(fis);

   FileOutputStream fos = new FileOutputStream("sal.txt");
   DataOutputStream ds = new DataOutputStream(fos);

   try {
     FileOutputStream fos = new FileOutputStream("sal.txt");
     int a, b, c;
     PrintStream ps = new PrintStream(fos);
     // set values for variables a, b, c
     ps.print(a);  ps.print(b);   ps.println(c);  }
     catch(IOException e) {
        System.out.println("Problem occurred in I/O");
    }

   System.out.print(a);  System.out.print(b); 
   System.out.println(c);

5. StringTokenizer      java.util
    StringTokenizer st = new StringTokenizer(strToProcess,
                    " \n\t\r");
    String nextToken()
    boolean hasMoreTokens()

    int count = 0;
    String nameSpace;
    String[] collArr;
    //  code to calculate nameSpace
    StringTokenizer st = new StringTokenizer(nameSpace,"|,");
    while (st.hasMoreTokens()) {
        collArr[count] = st.nextToken();  count++;  }

    int refs, use;
    String destination, gateway, flags, readString;
    StringTokenizer st;
    
       try {FileInputStream ni =
                 new FileInputStream("nestatef.dat");
           DataInputStream di = new DataInputStream(ni);
           FileOutputStream no =
                 New FileOutputStream("nestater.res");
        DataOutputStream dou = new DataOutputStream(no);
        while((readString = di.readLine()) != null)) {
            st = new StringTokenizer(readString," \t\n\r");
            destination = (String)st.nextElement();
            gateway = (String)st.nextElement();
            flags = (String)st.nextElement();
            refs = Integer.parseInt((String)st.nextElement());
            use = Integer.parseInt((String)st.nextElement());  }
        } catch (IOException i) 
              {System.out.println("An I/O Error has occurred"); }
           catch (NumberFormatException n)
              {System.out.println("Illegal integer encountered"); }

6. StreamTokenizer st = new InputStream(is);
    nextToken
    TT_EOF    TT_EOL    TT_NUMBER    TT_WORD
    nval  sval
    try { InputStream is = new FileInputStream("data.txt");
             StreamTokenizer st = new StreamTokenizer(is);
             int i;
             try {i=st.nextToken();
                    String word = st.sval;
                    l.addWord(word);
                    i = st.nextToken();
                    while (!(st.sval).equals("*****")) {
                        l.addWord(word);
                        i = st.nextToken();   }
                    }  catch(IOException io)
                       {System.out.println("Error in file");  }
             } catch(FileNotFoundException fe) {
                   System.out.println("Problem, file doesn't exist"); }
          }
7. boolean autoFlush = true;
    PrintStream p = new PrintStream(myOutputStream, 
                                                 autoFlush);
    System.out.println(veryLongString);
    if (System.out.checkError()) …

8. import java.io.*;
    class ListIt {
       public static void main (String args[] ) throws Exception {
           File file = new File (args[0]); 
           if (!file.exists() || !file.canRead() ) {
              System.out.println("Can't read " + file);
              return;
          } 
          if (file.isDirectory() ) {
              String[] files = file.list();
              for (int i=0; i<files.length; i++)
                 System.out.println(files[i]);
          }
         else try {
              FileInputStream fis = new FileInputStream (file);
              byte[] data = new byte[fis.available()];
              fis.read(data);
              System.out.write(data);
              } catch (FileNotFoundException e)
                    System.out.println("File Disappeared"); }
         }

9. String s = new DataInputStream(System.in).readLine();
   
    File out = new File("/tmp/foo.txt");
    if (out.canWrite()) {
       FileOutputStream fos = new FileOutputStream(out);
       PrintStream pfos = new PrintStream(fos);
       pfos.println(s); 
    }