Java使用FileOutputStream写入文件
From: http://beginnersbook.com/2014/01/how-to-write-to-a-file-in-java-using-fileoutputstream/
- /* 使用FileOutputStream写入文件,FileOutputStream的write() 方法只接受byte[] 类型
- 的参数,所以需要将string通过getBytes()方法转换为字节数组。
- 1、首先判断文件是否存在,不存在就新建一个
- 2、写入文件是以覆盖方式
- 3、文件不存在会自动创建,存在则会被重写
- */
- import java.io.*;
- public class Exercise {
- public static void main(String args[]) {
- FileOutputStream fos = null;
- File file;
- String mycontent = "This is my Data which needs to be written into the file.";
- try {
- // specify the file path
- file = new File("/home/zjz/Desktop/myFile.txt");
- fos = new FileOutputStream(file);
- /* This logic will check whether the file exists or not.
- if the file is not found at the specified location it would create
- a new file
- */
- // if (!file.exists()) {
- // file.createNewFile();
- // }
- /* String content cannot be directly written into a file.
- It needs to be converted into bytes
- */
- byte[] bytesArray = mycontent.getBytes();
- fos.write(bytesArray);
- fos.flush();
- System.out.println("File Written Successfully");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (fos != null) {
- fos.close();
- }
- } catch (IOException ioe) {
- System.out.println("Error in closing the Stream");
- }
- }
- }
- }
Java使用FileOutputStream写入文件的更多相关文章
- Java进阶(二十二)使用FileOutputStream写入文件
Java使用FileOutputStream写入文件 绪 在Java中,文件输出流是一种用于处理原始二进制数据的字节流类.为了将数据写入到文件中,必须将数据转换为字节,并保存到文件.请参阅下面的完整的 ...
- FileInputStream读取文件&FileOutputStream写入文件
概念摘自:http://jingyan.baidu.com/article/5552ef473ab5f2518ffbc98e.html Java的流式输入输出建立在4个抽象类的基础上:InputStr ...
- Java将字符串写入文件与将文件内容读取到字符串
原文:http://blog.csdn.net/liuweiyuxiang/article/details/69487326 将字符串写入文件 方法一 public void WriteStringT ...
- java 将内容写入文件 txt
@Test //将内容写入文件 public void xieru() throws Exception{ FileWriter fileWriter=new FileWriter("d:\ ...
- Java将对象写入文件读出——序列化与反序列化
Java类中对象的序列化工作是通过ObjectOutputStream和ObjectInputStream来完成的. 写入: File aFile=new File("e:\\c.txt&q ...
- java写入文件的几种方法分享
转自:http://www.jb51.net/article/47062.htm 一,FileWritter写入文件 FileWritter, 字符流写入字符到文件.默认情况下,它会使用新的内容取代所 ...
- java写入文件的几种方法小结
一,FileWritter写入文件 FileWritter, 字符流写入字符到文件.默认情况下,它会使用新的内容取代所有现有的内容,然而,当指定一个true (布尔)值作为FileWritter构造函 ...
- Java中IO流文件读取、写入和复制
//构造文件File类 File f=new File(fileName); //判断是否为目录 f.isDirectory(); //获取目录下的文件名 String[] fileName=f.li ...
- Java输出流FileOutputStream使用详解
Java输出流FileOutputStream使用详解 http://baijiahao.baidu.com/s?id=1600984799323133994&wfr=spider&f ...
随机推荐
- 如何看待yandex开源clickhouse这个列式文档数据库?
如何看待yandex开源clickhouse这个列式文档数据库? 大数据云计算 water 5天前 24℃ 0评论 欧阳辰<Druid实时大数据分析>作者,”互联居”作者编辑推荐1 ...
- maven项目转换为gradle项目
进入到项目更目录,运行 gradle init --type pom 上面的命令会根据pom文件自动生成gradle项目所需的文件和配置,然后以gradle项目重新导入即可.
- @Resource与@Autowired注解的区别踩坑者入
一.写本博文的原因 有些童鞋搞不为什么要用@Resource或者@Autowired,咱们一起研究下 @Resource默认按照名称方式进行bean匹配,@Autowired默认按照类型方式进行bea ...
- 分布式---CAP和BASE理论
3.CAP 分布式系统不可能同时满足一致性(C:Consistence),可用性(A:Avaliability)和分区容忍性(P:Partition Tolerance),最多只能同时满足其中的两 ...
- MySQL性能优化(六):分区
原文:MySQL性能优化(六):分区 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/vbi ...
- 使用fiddler进程弱网测试
使用fiddler手机需调整所连网络代理模式为手动,主机名与端口改为与电脑相同 打开Fiddler,Rules(规则)->Performance(性能)->勾选 Simulate Mode ...
- python之开发规范
命名规范 Python之父推荐的规范 Type Public Internal Modules lower_with_under _lower_with_under Packages lower_wi ...
- ES6基本常见语法
特色:写法更加优雅,更加像面像对象的编程,其思想和 ES5 是一致的. 箭头函数.this ES6中可以使用 => 作为函数表达形式,极简风格,参数+ => +函数体. var foo = ...
- 自动化测试报告之allure使用基础指南
差不多三个月前些的教程,然后跳槽了,自定义模块还没有写....后续也不知道有时间补上没有,最近应该会毕竟专注app测试这块了 1.github下载allure安装包:https://githu ...
- ASP.NET Core WebAPI帮助页--Swagger简单使用1.0
1.什么是Swagger? Swagger是一个规范且完整的框架,提供描述.生产.消费和可视化RESTful API,它是为了解决Web API生成有用文档和帮助页的问题. 2.为啥选用swagg ...