Java осень 2012 лекция 6

  • View
    114

  • Download
    3

Embed Size (px)

Transcript

  • 1. 6. I/O

2. ? ? Random rnd = new Random(); rnd.nextInt(100); 0 100 Random rnd = new Random(1L); rnd.nextInt(100); 0 100 Math.random(); double 0 1 3. public class Context { private Map, Object> context = new HashMap, Object>(); public void add(Class clazz, Object object){ if(!object.getClass().equals(clazz)){ //ERROR } if(context.containsKey(clazz)){ //ERROR } context.put(clazz, object); } public Object get(Class clazz){ return context.get(clazz); } } 4. public interface MyEventListener { public void handle(EventObject event); } public class MyEventListenerImpl implements MyEventListener { //code public void handle(EventObject event){ //process event } } 5. public class MyEventSource { private List listeners = Collections.synchronizedList(new ArrayList()); public void addListener(MyEventListener listener){ listeners.add(listener); } public void removeListener(MyEventListener listener){ listeners.remove(listener); } public void fireEvent(){ EventObject event = new EventObject(this); for(MyEventListener listner : listeners){ listner.handle(event); } } } 6. class HelloWorld extends AbstractHandler jetty public void handle(...) callback 7. public class TimeHelper { public static long getTimeInMs(){ Date date = new Date(); return date.getTime(); //return System.currentTimeMillis(); } public static int getPOSIX(){ Date date = new Date(); int millisInSecond = 1000; return (int)(date.getTime() / millisInSecond); } public static String getUserDateFull(Locale locale){ Date date = new Date(); DateFormat dateFormatter = DateFormat. getDateInstance(DateFormat.FULL, locale); return dateFormatter.format(date); } } 8. int timeMs = 10000; TimeService.instance().start(); TimeService.instance().sheduleTask(new TimerTask(){ public void run() { System.out.append("Timer run!n"); TimeService.instance().stop(); } }, timeMs); 9. public abstract class InputStream 10. public class FilterInputStream extends InputStream 11. public class SimpleFileReader { public static void main(String args[]) throws IOException { FileReader fr = new FileReader("SimpleFileReader.java"); BufferedReader br = new BufferedReader(fr); String currentLine; while((currentLine = br.readLine()) != null) { System.out.println(currentLine); } fr.close(); } } 12. public class CustomFileReader { public static void main(String args[]) throws IOException { FileInputStream fstream = new FileInputStream("textfile.txt"); DataInputStream in = new DataInputStream(fstream); InputStreamReader isr = new InputStreamReader(in, "UTF-16"); BufferedReader br = new BufferedReader(isr); String strLine; while ((strLine = br.readLine()) != null) { System.out.println (strLine); } br.close(); } } 13. BufferedReader br = null; try{ //code br = new BufferedReader(isr); //code } catch (Exception e){ System.err.println("Error: " + e.getMessage()); } finally{ if(br != null){ try { br.close(); } catch (IOException e) { System.err.println("Error: " + e.getMessage()); } } } 14. public interface VFS { boolean isExist(String path); boolean isDirectory(String path); String getAbsolutePath(String file); byte[] getBytes(String file); String getUFT8Text(String file); Iterator getIterator(String startDir); } public class VFSImpl implements VFS { private String root; public VFSImpl(String root){ this.root = root; } 15. private class FileIterator implements Iterator{ private Queue files = new LinkedList(); public FileIterator(String path){ files.add(new File(root + path)); } public boolean hasNext() { return !files.isEmpty(); } public String next() { File file = files.peek(); if(file.isDirectory()){ for(File subFile : file.listFiles()){ files.add(subFile); } } return files.poll().getAbsolutePath(); } public void remove() { } }