Bài giảng lập trình hướng đối tượng OUT PUT

96 639 3
Bài giảng lập trình hướng đối tượng OUT PUT

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Bài giảng lập trình hướng đối tượng OUT PUT

HỌC VIỆN CÔNG NGHỆ BƯU CHÍNH VIỄN THÔNG BÀI GIẢNG MÔNLẬP TRÌNH HƯỚNG ĐỐI TƯỢNGGiảng viên: Nguyễn Mạnh SơnĐiện thoại: 0904574001Bộ môn: Công nghệ phần mềm - Khoa CNTT1Học kỳ/Năm biên soạn: I – 2009/2010 Input và Output 12/09/12 3 Nội dung1. Files và Directories2. Character Streams3. Buffered Character Streams4. PrintWriter Class5. Byte Streams6. Random Access Files7. Kết hợp giao diện SWING và đọc ghi file 12/09/12 4Files và DirectoriesFile(String path)File(String directoryPath, String filename)File(File directory, String filename)File Constructorsboolean canRead(); boolean canWrite()boolean delete(); boolean equals(Object obj)boolean exists(); boolean exists()String getAbsolutePath(); String getCanonicalPath() throws IOExceptionString getName(); String getParent()String getPath(); boolean isAbsolute()MethodsLớp File cho phép định nghĩa các thông tin liên quan đến file và thư mụcboolean isDirectory()boolean isFile()long lastModified()long length()String[] list()boolean mkdir()boolean mkdirs()boolean renameTo(File newName)Methods 12/09/12 5Files and Directoriesclass FileDemo { public static void main(String args[]) { try { // Display Constant System.out.println("pathSeparatorChar = " + File.pathSeparatorChar); System.out.println("separatorChar = " + File.separatorChar); // Test Some Methods File file = new File(args[0]); System.out.println("getName() = " + file.getName()); System.out.println("getParent() = " + file.getParent()); System.out.println("getAbsolutePath() = " + file.getAbsolutePath()); System.out.println("getCanonicalPath() = " + file.getCanonicalPath()); System.out.println("getPath() = " + file.getPath()); Result :pathSeparatorChar = ;separatorChar = \getName() = FileDemo.javagetParent() = nullgetAbsolutePath() = D:\lecture\2004-01\teachyourself\example10-11\FileDemo.javagetCanonicalPath() = D:\lecture\2004-01\teachyourself\example10-11\FileDemo.javagetPath() = FileDemo.javacanRead() = truecanWrite() = trueSystem.out.println("canRead() = " + file.canRead()); System.out.println("canWrite() = " + file.canWrite()); } catch(Exception e) { e.printStackTrace(); } }} 12/09/12 6Character StreamsObjectReaderBufferedReaderInputStreamReader FileReaderWriter OutputStreamWriterPrintWriterBufferedWriterFileWriter……………… 12/09/12 7Character StreamsReaderBufferedReaderInputStreamReaderStringReaderCharArrayReaderPipedReaderFilterReader 12/09/12 8Character StreamsWriterBufferedWriterOutputStreamWriterStringWriterCharArrayWriterPipedWriterFilterWriterPrintWriter 12/09/12 9Character StreamsWriter()Writer(Object obj)Writer ConstructorsRefer to http://java.sun.com/j2se/1.5.0/docs/api/java/io/Writer.htmlabstract void close() throws IOExceptionabstract void flush() throws IOExceptionvoid write(int c) throws IOExceptionvoid write(char buffer[]) throws IOExceptionabstract void write(char buffer[], int index, int size) throws IOExceptionvoid write(String s) throws IOExceptionvoid write(String s, int index, int size) throws IOExceptionMethodsOutputStreamWriter(OutputStream os)OutputStreamWriter(OutputStream os, String encoding)OutputStreamWriter ConstructorsString getEncoding()getEncoding() MethodFileWriter(String filepath) throws IOExceptionFileWriter(String filepath, boolean append) throws IOExceptionFileWriter(String filepath) throws IOExceptionFileWriter Constructors 12/09/12 10Character StreamsRefer to http://java.sun.com/j2se/1.5.0/docs/api/java/io/Reader.htmlabstract void close() throws IOExceptionvoid mark(int numChars) throws IOExceptionboolean markSupported()int read() throws IOExceptionint read(char buffer[]) throws IOExceptionint read(char buffer[], int offset, int numChars) throws IOExceptionboolean ready() throws IOExceptionvoid reset() throws IOExceptionint skip(long numChars) throws IOExceptionMethods của lớp ReaderInputStreamWriter(InputStream os)InputStreamWriter(InputStream os, String encoding)InputStreamWriter ConstructorsString getEncoding()getEncoding() MethodFileReader(String filepath) throws FileNotFoundExceptionFileReader(File fileObj) throws FileNotFoundExceptionFileReader Constructors [...]... IOException FileOutputStreamWriter(File fileObj) throws IOException FileOutputStream Constructor BufferedOutputStream(OutputStream os) BufferedOutputStream(OutputStream os, int bufSize) BufferedOutputStream Constructor FilterOutputStream(OutputStream os) FilterOutputStream Constructor DataOutputStream(OutputStream os) DataOutputStream Constructor 12/09/12 17 Byte Streams OutputStream FileOutputStream ObjectOutputStream ByteArrayOutputStream PipeOutputStream FilterOutputStream ... và Output 12/09/12 23 Buffered[Input/Output]Stream import java.io.*; class BufferedOutputStreamDemo { public static void main(String args[]) { try { FileOutputStream fos = new FileOutputStream(args[0]); BufferedOutputStream bos = new BufferedOutputStream(fos); for (int i = 0; i < 12; i++) { bos.write(i); } bos.close(); } catch (Exception e) { System .out. println("Exception: "... textField.getText(); byte b[] = text.getBytes(); String outputFileName = System.getProperty("user.home", File.separatorChar + "home" + File.separatorChar + "zelda") + File.separatorChar + "text.txt"; FileOutputStream out = new FileOutputStream(outputFileName); out. write(b); out. close(); } catch(java.io.IOException e) { System .out. println("Khong the ghi vao file... BufferedInputStreamDemo { public static void main(String args[]) { try { FileInputStream fis = new FileInputStream(args[0]); BufferedInputStream bis = new BufferedInputStream(fis); int i; while((i = bis.read()) != -1) { System .out. println(i); } fis.close(); } catch (Exception e) { System .out. println("Exception: " + e); } } } Run : java BufferedOutputStreamDemo output.txt java... buffer[], int index, int size) throws IOException void write(String s) throws IOException void write(String s, int index, int size) throws IOException Methods OutputStreamWriter(OutputStream os) OutputStreamWriter(OutputStream os, String encoding) OutputStreamWriter Constructors String getEncoding() getEncoding() Method FileWriter(String filepath) throws IOException FileWriter(String filepath, boolean append)... Streams OutputStream FileOutputStream ObjectOutputStream ByteArrayOutputStream PipeOutputStream FilterOutputStream 12/09/12 33 Sử dụng từ khoá Catch class CatchSearch { public static void main(String args[]) { try { System .out. println("Before a"); a(); System .out. println("After a"); } catch (Exception e) { System .out. println("main: " + e); } finally { System .out. println("main: finally"); ...12/09/12 16 Byte Streams InputStream AutioInputStream FileInputStream ObjectInputStream SequenceInputStream ByteArrayInputStream PipedInputStream FilterInputStream 12/09/12 9 Character Streams Writer() Writer(Object obj) Writer Constructors Refer to http://java.sun.com/j2se/1.5.0/docs/api/java/io/Writer.html abstract... http://java.sun.com/j2se/1.4.2/docs/api/java/io/OutputStream.html void close() throws IOException void flush() throws IOException void write(int i) throws IOException void write(byte buffer[]) throws IOException void write(char buffer[], int index, int size) throws IOException Methods Defined by the OutputStream FileOutputStreamWriter(String filepath) throws IOException FileOutputStreamWriter(String filepath, boolean... text.txt"); } //Read try { String inputFileName = System.getProperty("user.home", File.separatorChar + "home" + File.separatorChar + "zelda") + File.separatorChar + "text.txt"; File inputFile = new File(inputFileName); FileInputStream in = new FileInputStream(inputFile); byte bt[] = new byte[(int)inputFile.length()]; in.read(bt); 12/09/12... public static void b() { try { System .out. println("Before c"); c(); System .out. println("After c"); } catch (ArrayIndexOutOfBoundsException e) { System .out. println("b: " + e); } finally { System .out. println("b: finally"); } } public static void c() { try { System .out. println("Before d"); d(); System .out. println("After d"); } . Streams)ObjectInputStreamFileInputStreamFilterInputStreamBufferedInputStreamFilterOutputStreamFileOutputStreamBufferedOutputStreamDataInputStreamOutputStreamDataOutputStreamPrintStream 12/09/12. bufSize)BufferedOutputStream ConstructorFilterOutputStream(OutputStream os)FilterOutputStream ConstructorDataOutputStream(OutputStream os)DataOutputStream Constructor 12/09/12

Ngày đăng: 12/09/2012, 15:44

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan