字节流分为FileInputStream 和FileOutputStream

 package com.io;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* 文件字节流的读取
* @author ganhang
*
*/
public class FileInputStreamDemo {
public static void main(String[] args) {
File file=new File("1.txt");
try {
InputStream is=new FileInputStream(file);
byte [] b= new byte[10];
int len=-1;
StringBuilder sb=new StringBuilder();//存读取的数据
while((len=is.read(b))!=-1){
sb.append(new String(b,0,len));
}
is.close();
System.out.println(sb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
}
 package com.io;

 import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* 文件字节流的写入
* @author ganhang
*
*/
public class FileOutputStreamDemo {
public static void main(String[] args) {
File file = new File("1.txt");
if (!file.exists()) {
try {
file.createNewFile();//没有则创建文件
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
OutputStream fos = new FileOutputStream(file, true);//文件末尾添加,不是覆盖
byte[] info = "hello,world".getBytes();
fos.write(info);
fos.close();
System.out.println("写入成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

最新文章

  1. dataGridViewX和数据库的链接之dataGridViewX1.DataSource = ds.Tables[0];
  2. (一)MVC5干货篇,目录和路由
  3. Apache Options Indexes FollowSymLinks详解
  4. [BZOJ4636]蒟蒻的数列
  5. Java之美[从菜鸟到高手演练]之JDK动态代理的实现及原理
  6. 颜色空间转换 cvtColor()[OpenCV 笔记13]
  7. 开发反模式 - SQL注入
  8. Response 关于浏览器header的方法
  9. CSS中垂直居中的方法
  10. 如何在关闭ssh连接的情况下,让程序继续运行?
  11. Springboot 部署到linux(二)
  12. Sqlserver常用基础语句
  13. os模块(二十)
  14. 更改具有Foreign key约束的表
  15. [PHP] 看博客学习观察者模式
  16. 2018-2019-2 《网络对抗技术》Exp5 MSF基础应用 Week7-8 20165233
  17. 原生Ajax函数
  18. openstack kolla 部署---不同的节点采用不同的物理接口
  19. java 读入文件 FileInputStream
  20. 基于注解的ssh框架之spring配置文件

热门文章

  1. JS HTML DOM
  2. 2014-07-23 利用ASP.NET自带控件实现单文件上传与下载
  3. BootStrap 智能表单系列 十 自动完成组件的支持
  4. 微型 ORM 的第一篇 DapperLambda发布
  5. 浮点数(double)的优势
  6. [转]使用ping钥匙临时开启SSH:22端口,实现远程安全SSH登录管理就这么简单
  7. OutputDebugString 输出信息到调试器
  8. mapper分页排序指定字段查询模板
  9. Bootstrap dropdown 使用
  10. C#手机充值