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 ...
随机推荐
- 如何去除List集合中的重复元素? a,b,c,a,c,b,d,,,,,,
package com.fs.test; import java.util.ArrayList; import java.util.List; public class Listdemo { publ ...
- Tcp之心跳包
Tcp之心跳包 心跳包 跳包之所以叫心跳包是因为:它像心跳一样每隔固定时间发一次,以此来告诉服务器,这个客户端还活着. 事实上这是为了保持长连接,至于这个包的内容,是没有什么特别规定的,不过一般都是很 ...
- JS定时器的用法及示例
JS定时器的用法及示例 目录 js 定时器的四个方法 示例一 示例二 示例三 js 定时器的四个方法 setInterval() :按照指定的周期(以毫秒计)来调用函数或计算表达式.方法会不停地调用函 ...
- Android系统修改之葡萄牙沃达丰One Net服务问题处理
客户反馈的葡萄牙沃达丰的OneNet服务问题 Vodafone Portugal have a service (One Net) for enterprise customers that used ...
- nodejs在Mac下的卸载
卸载: 在 node 官网上下载的安装包,用安装包安装的node.应该可以用一下命令行卸载: 在终端输入以下命令: sudo rm -rf /usr/local/{bin/{node,npm},lib ...
- 仿造email后缀自动添加功能(1)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- ActiveMQ基础01——Linux下载安装ActiveMQ
1.下载 下载地址:http://activemq.apache.org/ 点击按钮 下载Linux下最新版安装包,点击即可下载 2.安装ActiveMQ 将之前下载的安装包上传到linux当中,一般 ...
- QT textbroswer textedite Qlist的常用的操作函数
Textbrowser: 一.添加函数 1.insertPlainText():这个函数特别好用,括号里面的参数是QString,可以用QString(“%1%2”).arg(QString变量).a ...
- PowerDesigner连接 MySQL 生成 ER图
powerdesigner 16.5 http://www.pcsoft.com.cn/soft/27495.html jdk 1.8 32位 https://mirrors.huaweicloud. ...
- Summer training round2 #5 (Training #21)
A:正着DFS一次处理出每个节点有多少个优先级比他低的(包括自己)作为值v[i] 求A B 再反着DFS求优先级比自己高的求C #include <bits/stdc++.h> #incl ...