基本网络编程分析.pptVIP

  • 2
  • 0
  • 约1.14万字
  • 约 54页
  • 2016-06-01 发布于湖北
  • 举报
public static void main(String[] args) throws Exception { OutputStream out = new FileOutputStream(test.txt); out.write(hello.getBytes()); out.flush(); out.close(); } public static void main(String[] args) throws Exception { OutputStream out = new FileOutputStream(test.txt,true); byte[] bys = hello.getBytes(); for (int i = 0; i bys.length; i++) { out.write(bys[i]);//每次只写入一个内容 } out.flush(); out.close(); } OutputStreamTest.java int available() throws IOException:文件大小 abstract void close() throws IOException:关闭流 int read() throws IOException:从输入流中读取单个字节。 int read(byte[] b) throws IOException:从输入流中读取最多b.length个字节的数据,并将其存储在字节数组b中,返回实际读取的字节。 直到read(byte[] b)函数返回-1表示输入流的结束。 以其子类FileInputStream来实例化InputStream对象 FileInputStream(File/String file) throws FileNotFoundException 创建一个向指定 File 对象表示的文件中写入数据的文件输出流。 public static void main(String[] args) { String path = pathName; InputStream input = null; input = new FileInputStream(new File(path)); // 定义缓冲区 byte[] bys = new byte[50]; int len = 0; while ((len = input.read(bys)) != -1) { System.out.println(new String(bys, 0, len)); } input.close(); } } InputStreamTest.java 1个字符 等于 2个字节 字符流主要是操作char的类型数据: 字符输出流:Writer 字符输入流:Readr abstract void close() throws IOException:关闭流 int read() throws IOException:从输入流中读取单个字符。 int read(char[] cbuf) throws IOException:从输入流中读取最多c.length个字节的数据,并将其存储在字符数组c中,返回实际读取的字节。 直到read(char[] c)函数返回-1表示输入流的结束。 char[] cBuff = new char[1024];//动态声明数组,长度 read(cBuff ) !=-1 read(cBuff ) 0 以其子类FileReader来实例化Reader对象 FileReader(File/String file) throws FileNotFoundException 创建一个向指定对象的文件输入流 public static void main(String[] args) { String path = pathName; Reader reader = null; reader = new FileReader(new File(path)); // 定义缓冲区 char[] buf = new char[50]; int len = 0; // 循环读取直到为空的时候 while ((len = reader.read(buf)) != -1) { System.out.println(new String(buf, 0, len)); } reader.close(); } } abstract void flush() throws IOException:清空缓存 abstr

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档