From: http://beginnersbook.com/2014/01/how-to-write-to-a-file-in-java-using-fileoutputstream/

  1. /* 使用FileOutputStream写入文件,FileOutputStream的write() 方法只接受byte[] 类型
  2. 的参数,所以需要将string通过getBytes()方法转换为字节数组。
  3. 1、首先判断文件是否存在,不存在就新建一个
  4. 2、写入文件是以覆盖方式
  5. 3、文件不存在会自动创建,存在则会被重写
  6. */
  7.  
  8. import java.io.*;
  9.  
  10. public class Exercise {
  11.  
  12. public static void main(String args[]) {
  13. FileOutputStream fos = null;
  14. File file;
  15. String mycontent = "This is my Data which needs to be written into the file.";
  16. try {
  17. // specify the file path
  18. file = new File("/home/zjz/Desktop/myFile.txt");
  19. fos = new FileOutputStream(file);
  20. /* This logic will check whether the file exists or not.
  21. if the file is not found at the specified location it would create
  22. a new file
  23. */
  24. // if (!file.exists()) {
  25. // file.createNewFile();
  26. // }
  27. /* String content cannot be directly written into a file.
  28. It needs to be converted into bytes
  29. */
  30. byte[] bytesArray = mycontent.getBytes();
  31. fos.write(bytesArray);
  32. fos.flush();
  33. System.out.println("File Written Successfully");
  34. } catch (FileNotFoundException e) {
  35. e.printStackTrace();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. } finally {
  39. try {
  40. if (fos != null) {
  41. fos.close();
  42. }
  43. } catch (IOException ioe) {
  44. System.out.println("Error in closing the Stream");
  45. }
  46. }
  47. }
  48.  
  49. }

Java使用FileOutputStream写入文件的更多相关文章

  1. Java进阶(二十二)使用FileOutputStream写入文件

    Java使用FileOutputStream写入文件 绪 在Java中,文件输出流是一种用于处理原始二进制数据的字节流类.为了将数据写入到文件中,必须将数据转换为字节,并保存到文件.请参阅下面的完整的 ...

  2. FileInputStream读取文件&FileOutputStream写入文件

    概念摘自:http://jingyan.baidu.com/article/5552ef473ab5f2518ffbc98e.html Java的流式输入输出建立在4个抽象类的基础上:InputStr ...

  3. Java将字符串写入文件与将文件内容读取到字符串

    原文:http://blog.csdn.net/liuweiyuxiang/article/details/69487326 将字符串写入文件 方法一 public void WriteStringT ...

  4. java 将内容写入文件 txt

    @Test //将内容写入文件 public void xieru() throws Exception{ FileWriter fileWriter=new FileWriter("d:\ ...

  5. Java将对象写入文件读出——序列化与反序列化

    Java类中对象的序列化工作是通过ObjectOutputStream和ObjectInputStream来完成的. 写入: File aFile=new File("e:\\c.txt&q ...

  6. java写入文件的几种方法分享

    转自:http://www.jb51.net/article/47062.htm 一,FileWritter写入文件 FileWritter, 字符流写入字符到文件.默认情况下,它会使用新的内容取代所 ...

  7. java写入文件的几种方法小结

    一,FileWritter写入文件 FileWritter, 字符流写入字符到文件.默认情况下,它会使用新的内容取代所有现有的内容,然而,当指定一个true (布尔)值作为FileWritter构造函 ...

  8. Java中IO流文件读取、写入和复制

    //构造文件File类 File f=new File(fileName); //判断是否为目录 f.isDirectory(); //获取目录下的文件名 String[] fileName=f.li ...

  9. Java输出流FileOutputStream使用详解

    Java输出流FileOutputStream使用详解 http://baijiahao.baidu.com/s?id=1600984799323133994&wfr=spider&f ...

随机推荐

  1. js获取url上的指定参数

    function getAllUrlParams(url) { // get query string from url (optional) or window var queryString = ...

  2. @RequestMapping-标准映射和Ant风格的映射

    4.@RequestMapping 如果value不以“/”开头,SpringMVC会自动添加“/” 4.1.@RequestMapping映射 4.1.1.标准URL映射 4.1.2.Ant风格的U ...

  3. leetcode240 搜索二维矩阵 II

    题目: 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 ma ...

  4. c# log4net安装时在AssemblyInfo中提示找不到log4net解决办法

    在安装log4net时,按照安装手册需要在AssemblyInfo.cs里添加log4net的配置信息 [assembly: log4net.Config.XmlConfigurator(Config ...

  5. python根据已有数据库生成model.py

    有时我们需要根据已存在的数据库进行django开发时,手写model.py是不现实的 先执行下面的语句,在命令行终端会输出所有表的类 python .\manage.py inspectdb 检查无误 ...

  6. linux下/etc/rc.d目录的介绍及redhat启动顺序

    init inittab rc0 rc1 rc2 rc3 rc5 rc6 rcS init.d init 系统启动超级进程 inittab 进程启动配置文件 rc0 - rc6 各启动级别的启动脚本 ...

  7. kotlin函数式编程入门及图片处理

    函数式编程入门: 对于面向对象编程[OOP]和函数式编程[FP] 由于在JAVA8的学习中系统的学习过了,所以这里对其概念就不过多解释了,下面直接用代码来看下在kotlin中函数式编程是如何编写的: ...

  8. filter和filter_by 的区别

  9. MHA监控进程异常退出(MHA版本:0.56)

    最近遇到一个非常诡异的问题,mha后台进程自己中断退出了.以下是报错:Mon Dec 21 20:16:07 2015 - [info] OK.Mon Dec 21 20:16:07 2015 - [ ...

  10. GlusterFS 分布式文件系统

    简介 官方文档:https://docs.gluster.org/en/latest/Quick-Start-Guide/Architecture/ Glusterfs是一个开源的分布式文件系统,是S ...