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. IntelliJ IDEA setup JDK无效

    参考 问题现象:功能全部爆红,总是提示“setup JDK”,设置JDK后无效.但程序能够正常编译和运行. 解决方法:  File -> Invalidate Caches/Restart -& ...

  2. springboot拦截中自动注入的组件为null问题解决方法

    一.写SpringUtil类来获取Springh管理的类实例,判断是否注入成功,如果没有注入成功重新获取注入 package com.util; import org.springframework. ...

  3. Appium Android 获取包名appPackage和appActivity的几种方法

    情况1: 安装包未安装到手机 准备前提条件: 1 Android SDK管理工具目录 2 PC端有apk包 使用方法: 1 打开终端,当前路径移动到sdk管理工具目录tools或build-tools ...

  4. pgsql sql字段拼接

    1.  一条记录数据字段拼接 语法:concat_ws('拼接符号',字段名,more fields) 例子:concat_ws(':',username,sex)2. 多条记录字段拼接 语法:con ...

  5. Delphi 鼠标的编程

  6. 在centos7上kvm网卡桥接

    系统环境准备 [root@linux-node1 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@linux-node1 ~ ...

  7. filepath:处理文件路径的一把好手

    1.ToSlash(path string) string 将相关平台的路径分隔符转为/ package main import ( "fmt" "os" &q ...

  8. osworkflow 入门基础2

    [quote]前篇我引入了互联网上找来的一篇文章,接着上篇讲:osworkflow 工作流是非常轻量级的,虽然2006就停止活动了,互联网上的资料也不是很多,官网也没过多的说明,比起jbpm 和act ...

  9. 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 ...

  10. 关于Win Re的故事

    闲来无事,拿出来自己的小破笔记本瞎折腾,突然发现桌面上竟然还残留着上一个公司的内部vpn, 我是一个有着轻微洁癖强迫症的人,留着这么一个东西占据我本来就不是很大的固态硬盘,简直是罪过.于是我头脑一热, ...