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 ...
随机推荐
- IntelliJ IDEA setup JDK无效
参考 问题现象:功能全部爆红,总是提示“setup JDK”,设置JDK后无效.但程序能够正常编译和运行. 解决方法: File -> Invalidate Caches/Restart -& ...
- springboot拦截中自动注入的组件为null问题解决方法
一.写SpringUtil类来获取Springh管理的类实例,判断是否注入成功,如果没有注入成功重新获取注入 package com.util; import org.springframework. ...
- Appium Android 获取包名appPackage和appActivity的几种方法
情况1: 安装包未安装到手机 准备前提条件: 1 Android SDK管理工具目录 2 PC端有apk包 使用方法: 1 打开终端,当前路径移动到sdk管理工具目录tools或build-tools ...
- pgsql sql字段拼接
1. 一条记录数据字段拼接 语法:concat_ws('拼接符号',字段名,more fields) 例子:concat_ws(':',username,sex)2. 多条记录字段拼接 语法:con ...
- Delphi 鼠标的编程
- 在centos7上kvm网卡桥接
系统环境准备 [root@linux-node1 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@linux-node1 ~ ...
- filepath:处理文件路径的一把好手
1.ToSlash(path string) string 将相关平台的路径分隔符转为/ package main import ( "fmt" "os" &q ...
- osworkflow 入门基础2
[quote]前篇我引入了互联网上找来的一篇文章,接着上篇讲:osworkflow 工作流是非常轻量级的,虽然2006就停止活动了,互联网上的资料也不是很多,官网也没过多的说明,比起jbpm 和act ...
- 13 Zabbix4.4.1系统告警“More than 75% used in the configuration cache”
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 13 Zabbix4.4.1系统告警“More than 75% used in the conf ...
- 关于Win Re的故事
闲来无事,拿出来自己的小破笔记本瞎折腾,突然发现桌面上竟然还残留着上一个公司的内部vpn, 我是一个有着轻微洁癖强迫症的人,留着这么一个东西占据我本来就不是很大的固态硬盘,简直是罪过.于是我头脑一热, ...