Вы находитесь на странице: 1из 11

import java.io.*; public class TestAllClass { public void testClasses(int testCase) { switch (testCase) { case 1: // BufferedInputStream System.out.

print("\nWhat is your name? "); StringBuffer response = new StringBuffer(); try { BufferedInputStream buff = new BufferedInputStream(System.in); int in = 0; char inChar; do { in = buff.read(); inChar = (char) in; if ((in != -1) & (in != '\n') & (in != '\r')) { response.append(inChar); '\r')); }

} while ((in != -1) & (inChar != '\n') & (in != buff.close(); System.out.println("BufferedInputStream :" + response.toString()); } catch (IOException e) {

e.getMessage()); }

System.out.println("Exception: " +

break; case 2: // BufferedOutputStream try { BufferedOutputStream bufferedOutput = new BufferedOutputStream(

new FileOutputStream("yourFile.txt")); bufferedOutput.write("Line one".getBytes()); bufferedOutput.write("\n".getBytes()); bufferedOutput.write(65); bufferedOutput.close(); } catch (Exception e) { e.getMessage()); } System.out.println("Exception: " +

break; case 3: // ByteArrayInputStream String out = "THis is a test"; ByteArrayInputStream inputStream; inputStream = new ByteArrayInputStream(out.getBytes()); int inBytes = inputStream.available(); System.out.println("inStream has " + inBytes + " available bytes"); byte inBuf[] = new byte[inBytes]; int bytesRead = inputStream.read(inBuf, 0, inBytes); System.out.println(bytesRead + " bytes were read"); System.out.println("They are: " + new String(inBuf)); break; case 4: // ByteArrayOutputStream ByteArrayOutputStream outStream = new ByteArrayOutputStream(); String s = "This is a test."; for (int i = 0; i < s.length(); ++i) outStream.write(s.charAt(i)); System.out.println("outstream: " + outStream); System.out.println("size: " + outStream.size()); break;

case 5: // CharArrayReader try { CharArrayReader inStream; inStream = new CharArrayReader(new char[] { 'c', 'h', 'a', 'r', 'a', 'r', 'r', 'a', 'y', 'r', 'e', 'a', 'd', 'e', 'r' }); int ch = 0; StringBuffer sb = new StringBuffer(""); while ((ch = inStream.read()) != -1) sb.append((char) ch); String s1 = sb.toString(); were read"); System.out.println(s1.length() + " characters System.out.println("They are: " + s1); } catch (Exception e) { e.getMessage()); } System.out.println("Exception: " +

break; case 6: // CharArrayWriter try { CharArrayWriter f = new CharArrayWriter(); String s1 = "This should end up in the array"; char buf[] = new char[s1.length()]; s1.getChars(0, s1.length(), buf, 0); f.write(buf); System.out.println(f.toString()); char c[] = f.toCharArray(); for (int i = 0; i < c.length; i++) { System.out.print(c[i]); } catch (Exception e) { }

System.out.println("Exception: " + e.getMessage()); } break; case 7: // DataOutputStream try { DataOutputStream out1 = new DataOutputStream( new FileOutputStream("yourFile2.txt")); double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 }; int[] units = { 12, 8, 13, 29, 50 }; "Support.", "" }; String[] descs = { "Java", "Source ", "and", for (int i = 0; i < prices.length; i++) { out1.writeDouble(prices[i]); out1.writeChar('\t'); out1.writeInt(units[i]); out1.writeChar('\t'); out1.writeChars(descs[i]); out1.writeChar('\n'); } out1.close(); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); } break; case 8: // DataInputStream try { FileInputStream( DataInputStream in = new DataInputStream(new "yourFile2.txt")); double price; int unit;

String desc; double total = 0.0; try { while (true) { price = in.readDouble(); in.readChar(); // throws out the tab unit = in.readInt(); in.readChar(); // throws out the tab desc = in.readLine(); System.out.println(unit); System.out.println(desc); System.out.println(desc); total = total + unit * price; } } catch (Exception e) { e.getMessage()); } System.out.println("Exception: " + in.close(); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); } break; case 9: // FileINputStream try { File file = new File("file1.txt"); FileInputStream fis = new FileInputStream(file); int i1 = fis.read(); while (i1 != -1) { i1 = fis.read(); System.out.println(i1) ;} fis.close();

} catch (IOException e) { System.out.println("IO exception"); } break; case 10:// FIleOutputStream File originFile = new File("file1.txt"); File destinationFile = new File("file2.txt"); { if (!originFile.exists() || destinationFile.exists()) return; } try { byte[] readData = new byte[1024]; FileInputStream fis = new FileInputStream(originFile); FileOutputStream fos = new FileOutputStream(destinationFile); int i = fis.read(readData); while (i != -1) { fos.write(readData, 0, i); i = fis.read(readData); } fis.close(); fos.close(); } catch (IOException e) { System.out.println(e); } break; case 11: // InputStreamReader try { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); while (true) {

System.out.print("Radius? "); String str = br.readLine(); double radius; try { radius = Double.valueOf(str).doubleValue(); } catch (NumberFormatException nfe) { System.out.println("Incorrect format!"); continue; }

if (radius <= 0) { System.out.println("Radius must be positive!"); continue; } double area = Math.PI * radius * radius; System.out.println("Area is " + area); return; }

} catch (Exception e) { e.printStackTrace(); } break; case 12: // ObjectI/O stream try { Car c1 = new Car("Some car", 4, new Engine(6)); ObjectOutputStream oos = null; FileOutputStream fos = new FileOutputStream("car.ser"); oos = new ObjectOutputStream(fos); oos.writeObject(c1); ObjectInputStream ois = null; FileInputStream fis = new FileInputStream("car.ser"); ois = new ObjectInputStream(fis); Car c2 = (Car) ois.readObject();

c2.getNumTires());

System.out.println("Number of tires = " + System.out.println("Number of cylinders = " + c2.getEngine().getNumCylinders()); System.out.println("Name = " + c2.getName());

} catch (Exception e) { e.printStackTrace();} break; case 13: // PipedI/O stream Thread thread1 = new Thread(new PipeOutput("Producer")); Thread thread2 = new Thread(new PipeInput("Consumer")); thread1.start(); thread2.start(); boolean thread1IsAlive = true; boolean thread2IsAlive = true; do { if (thread1IsAlive && !thread1.isAlive()) { thread1IsAlive = false; } if (thread2IsAlive && !thread2.isAlive()) { thread2IsAlive = false; } } while (thread1IsAlive || thread2IsAlive); break; } } public static void main(String[] args) { new TestAllClass().testClasses(13); } } class Engine implements Serializable { private int numCylinders; Engine(int numCylinders) { this.numCylinders = numCylinders; }

int getNumCylinders() { return numCylinders; } } class Car implements Serializable { private int numTires; private Engine e; private String name;

Car(String name, int numTires, Engine e) { this.name = name; this.numTires = numTires; this.e = e; }

int getNumTires() { return numTires; } Engine getEngine() { return e; } String getName() { return name; } } class PipeIO { PipedOutputStream outputPipe = new PipedOutputStream(); PipedInputStream inputPipe = new PipedInputStream(); String name; public PipeIO(String id) { name = id; try { outputPipe.connect(inputPipe); } catch (IOException ex) { System.out.println("IOException in static initializer"); } } } class PipeOutput extends PipeIO implements Runnable {

public PipeOutput(String id) { super(id); } public void run() { String s = "This is a test."; try { for (int i = 0; i < s.length(); ++i) { outputPipe.write(s.charAt(i)); System.out.println(name + " wrote " + s.charAt(i)); } outputPipe.write('!'); } catch (IOException ex) { System.out.println("IOException in PipeOutput"); } } } class PipeInput extends PipeIO implements Runnable { public PipeInput(String id) { super(id); } public void run() { boolean eof = false; try { while (!eof) { int inChar = inputPipe.read(); if (inChar != -1) { char ch = (char) inChar; if (ch == '!') { eof = true; break; } else ch); } } System.out.println(name + " read " + } catch (IOException ex) { System.out.println("IOException in PipeOutput"); } }}

Output: C:\java>javac BooleanTest.java C:\java>java Compare: -1 Boolean value:false Value of :false Boolean to string: false BooleanTest 12345

Вам также может понравиться