OutputStreamWriter类close()方法 (OutputStreamWriter Class close() method)

  • close() method is available in java.io package.

    close()方法在java.io包中可用。

  • close() method is used to first flush before closing the stream and the method write() or flush() invokes after closing the stream will result in an exception.

    close()方法用于在关闭流之前先进行刷新,而关闭流之后调用write()或flush()方法将导致异常。

  • close() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    close()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • close() method may throw an exception at the time of closing the stream.

    close()方法在关闭流时可能会引发异常。

    IOException: This exception may throw when getting any input/output error while performing.

    IOException :在执行过程中遇到任何输入/输出错误时,可能引发此异常。

Syntax:

句法:

    public void close();

Parameter(s):

参数:

  • It does not accept any parameter.

    它不接受任何参数。

Return value:

返回值:

The return type of the method is void, it returns nothing.

该方法的返回类型为void ,不返回任何内容。

Example:

例:

// Java program to demonstrate the example
// of void close() method
// of OutputStreamWriter
import java.io.*;
public class CloseOfOSW {public static void main(String[] args) throws Exception {OutputStream os_stm = null;
OutputStreamWriter osw_stm = null;
InputStream is_stm = null;
int val = 65;
try {// Instantiates FileOutputStream and
// FileInputStream and OutputStreamWriter
os_stm = new FileOutputStream("D:\\includehelp.txt");
osw_stm = new OutputStreamWriter(os_stm);
// Loop to write byte to the stream
for (int i = 0; i < 3; ++i) {osw_stm.write(val);
val++;
}
// By using flush() method is to
// flush the stream
osw_stm.flush();
is_stm = new FileInputStream("D:\\includehelp.txt");
// Loop to read byte from the stream
for (int i = 0; i < 3; ++i) {char ch = (char) is_stm.read();
System.out.println("ch: " + ch);
}
osw_stm.close();
System.out.println("Stream Closed Successfully....");
} catch (Exception ex) {System.out.println(ex.toString());
} finally {// with the help of this block is to
// free all necessary resources linked
// with the stream
if (is_stm != null) {is_stm.close();
if (os_stm != null) {os_stm.close();
}
}
}
}
}

Output

输出量

ch: A
ch: B
ch: C
Stream Closed Successfully....

翻译自: https://www.includehelp.com/java/outputstreamwriter-close-method-with-example.aspx

Java OutputStreamWriter close()方法与示例相关推荐

  1. Java IOUtils.copy方法代码示例(亲测)

    本文整理汇总了Java中org.apache.commons.io.IOUtils.copy方法的典型用法代码示例.如果您正苦于以下问题:Java IOUtils.copy方法的具体用法?Java I ...

  2. java user directory,Java ProcessBuilder directory()方法与示例

    语法:public File directory (); public ProcessBuilder directory (File dir); ProcessBuilder类directory()方 ...

  3. Java序列化魔术方法及其示例使用

    在上一篇文章中, 您需要了解有关Java序列化的所有知识 ,我们讨论了如何通过实现Java序列化来启用类的可序列化性. Serializable接口. 如果我们的类未实现Serializable接口, ...

  4. catalog java,Java Connection getCatalog()方法与示例

    通常,目录是一个目录,其中包含有关数据集,文件或数据库的信息.而数据库目录中包含所有数据库,基本表,视图(虚拟表),同义词,值范围,索引,用户和用户组的列表. Connection接口的getCata ...

  5. filepermission java,Java FilePermission getActions()方法与示例

    FilePermission类getActions()方法getActions()方法在java.io包中可用. getActions()方法用于检查此FilePermission和给定对象在路径名和 ...

  6. java方法参数Bundle,Java ResourceBundle keySet()方法及示例

    ResourceBundle类keySet()方法keySet()方法在java.util包中可用. keySet()方法用于从此ResourceBundle及其超级捆绑包中获取所有现有键,以在Set ...

  7. java exec waitfor,Java Process waitFor()方法与示例

    流程类waitFor()方法在java.lang包中提供了waitFor()方法. waitFor()方法用于使当前正在运行的线程在需要时等待,直到由该Process对象表示的进程完成其终止为止. 当 ...

  8. java arraylist.add(),Java ArrayList add()方法与示例

    ArrayList类add()方法 语法:public boolean add(T ele); public void add(int indices, T ele);add()方法在java.uti ...

  9. java rollback用法,Java Connection rollBack()方法与示例

    回滚操作将撤消当前事务所做的所有更改,即,如果调用Connection接口的rollBack()方法,则所有修改都将还原到最后一次提交. 您还可以通过将所需的Savepoint对象作为参数传递给此方法 ...

  10. inputstream 关闭 java,Java ObjectInputStream close()方法与示例

    ObjectInputStream类close()方法close()方法在java.io包中可用. close()方法用于关闭此ObjectInputStream并释放与此流链接的所有系统资源. cl ...

最新文章

  1. C++ dll 动态链接库的创建与调用
  2. linux命令strip
  3. go不使用工具包将大写字符转成小写字符的方法
  4. sendkeys安装包_利用Windows命令行解压zip压缩文件(不借助第三方软件)
  5. 双向链表、双向循环链表
  6. Error:配置系统未能初始化
  7. xpath爬取美女图片
  8. UDP如何实现可靠性传输
  9. layer:子父之间的数据传递
  10. 大数据告诉你,中国哪里出美女
  11. java蓝桥杯练习 学做菜
  12. Android实现计算器功能
  13. 列表找房(十)03-页面滚动效果控制——WindowScroller组件用法 Scroll属性
  14. GitHub下载神器,被下架后又复活
  15. 电气绘图软件EPLAN在WIN10详细安装教程
  16. 蓝桥杯 ADV-222 求arccos
  17. SpringBoot系列:Spring Boot集成定时任务Quartz,java百度云短信发送
  18. 使用isolinux制作Linux启动光盘
  19. IP核Map编译报错:Buffers of the same direction cannot beplaced in series.
  20. 页面自动更新(javascript)

热门文章

  1. 茌平计算机中考成绩查询,中考成绩查询系统入口2021
  2. 深度学习自动编译和优化技术调研
  3. 计算机-p命令,OD(电脑命令)_百度百科
  4. java jps都卡死,java长时间运行后,jps失效
  5. android程序 幻灯片,Android:ViewPager制作幻灯片
  6. android preferenceactivity 源码,PreferenceActivity PreferenceScreen (3.0/API 11 以前)
  7. php负载均衡原理_负载均衡的原理(通俗理解)
  8. miniui展示日历能点击_2020年日历设计,除了366天有新字体,还有新形式
  9. 谈谈我对MYSQL乱码的解决办法
  10. c#随机数的产生与输出【C#】