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 ...
随机推荐
- rabbitmq中关于exchange模式type报错
channel.exchange_declare(exchange='logs', type='fanout') 报错: Traceback (most recent call last): Fil ...
- 附录1:arrayanalysis的本地使用(质量控制)
访问:https://github.com/BiGCAT-UM/affyQC_Module,点击“Download ZIP”,下载得到affyQC_Module-master.zip,解压得到一个af ...
- golang(3):strings和strconv使用 & 时间和日期类型 & 指针类型 & 流程控制 & 函数
strings和strconv使用 . strings.HasPrefix(s string, prefix string) bool: // 判断字符串s是否以prefix开头 . . string ...
- 在set中放入自定义类型
这件事情的起因是在学习背包问题时突然想到了一种算法,分析了一下应该是n^2logn复杂度的,当然比dp慢.但是既然想到了就实现了下: #include<bits/stdc++.h> usi ...
- python 基于detectron或mask_rcnn的mask遮罩区域进行图片截取
基于示例infer_simple.py 修改165行vis_utils.vis_one_image为vis_utils.vis_one_image_opencv 在detectron.utils.vi ...
- Android系统修改之展讯平台的Mms不能发送西班牙特殊字符ú的问题
在测试中, 发现在发送短信的时候特殊字符ú不能发送, 但是输入框可以输入并正常显示, 查看代码之后, 发现是展讯在字符转换的时候出现的问题 frameworks/base/telephony/java ...
- 21、Nginx 常见问题
1.多个server_name容易产生冲突,会按照如下顺序匹配 1.首先选择所有的字符串完全匹配的server_name.(完全匹配) 2.选择通配符在前面的server_name,如*.bgx.co ...
- PHP随机产生10个100以内互不相同的正整数按从小到大的顺序输出
<?php //产生1~100的正整数 $numbers = range(1,100); //从1~100中选出10个不重复的整数,并使用函数进行排序 $randNumbers = array_ ...
- Powershell - 获取服务器启动时间
www.orangeisland.cn 使用以下命令,直接获取服务器启动时间: 通过以下脚本,批量获取服务器的启动时间: $TextPath = "C:\Users\admin\Deskto ...
- 修改Anaconda启动时默认路径
1.找到Anoconda启动快捷方式,入下图: 2.右击点击属性,进入下图: 3.将第三行的 目标(T): ......D:\Anoconda\Scripts\jupyter-notebook-sc ...