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 ...
随机推荐
- js获取url上的指定参数
function getAllUrlParams(url) { // get query string from url (optional) or window var queryString = ...
- @RequestMapping-标准映射和Ant风格的映射
4.@RequestMapping 如果value不以“/”开头,SpringMVC会自动添加“/” 4.1.@RequestMapping映射 4.1.1.标准URL映射 4.1.2.Ant风格的U ...
- leetcode240 搜索二维矩阵 II
题目: 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 ma ...
- c# log4net安装时在AssemblyInfo中提示找不到log4net解决办法
在安装log4net时,按照安装手册需要在AssemblyInfo.cs里添加log4net的配置信息 [assembly: log4net.Config.XmlConfigurator(Config ...
- python根据已有数据库生成model.py
有时我们需要根据已存在的数据库进行django开发时,手写model.py是不现实的 先执行下面的语句,在命令行终端会输出所有表的类 python .\manage.py inspectdb 检查无误 ...
- linux下/etc/rc.d目录的介绍及redhat启动顺序
init inittab rc0 rc1 rc2 rc3 rc5 rc6 rcS init.d init 系统启动超级进程 inittab 进程启动配置文件 rc0 - rc6 各启动级别的启动脚本 ...
- kotlin函数式编程入门及图片处理
函数式编程入门: 对于面向对象编程[OOP]和函数式编程[FP] 由于在JAVA8的学习中系统的学习过了,所以这里对其概念就不过多解释了,下面直接用代码来看下在kotlin中函数式编程是如何编写的: ...
- filter和filter_by 的区别
- MHA监控进程异常退出(MHA版本:0.56)
最近遇到一个非常诡异的问题,mha后台进程自己中断退出了.以下是报错:Mon Dec 21 20:16:07 2015 - [info] OK.Mon Dec 21 20:16:07 2015 - [ ...
- GlusterFS 分布式文件系统
简介 官方文档:https://docs.gluster.org/en/latest/Quick-Start-Guide/Architecture/ Glusterfs是一个开源的分布式文件系统,是S ...