字节流分为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();
}
}
}
}

Java学习之IO字节流的更多相关文章

  1. java学习一目了然——IO

    java学习一目了然--IO IO是java学习当中很重要的一部分.IO流实现数据的上传下载,即读写数据,包括输入和输出流,输入流指的是将数据以字符或字节形式从外部媒介比如文件.数据库等读取到内存中所 ...

  2. Java学习之==>IO文件操作体系

    一.概述 在整个 Java.io 中最重要的就是5个类和一个接口.5个类指的是 File.InputStream.OutputStream.Reader.Writer,一个接口指的是Serializa ...

  3. java学习之IO流(学习之旅,一)

    个人在学习IO流的时候看到如下所示java 流类图结构的时候,我的感想是,这么多··处于蒙的状态. Java流类图结构 这么多,没有分类不好学,那我们就慢慢一口一口的吃,这样每天学习一点就好了,其实很 ...

  4. java学习笔记--IO流

    第十二章大纲: I/O input/output 输入/输出 一.创建文件,借助File类来实现 file.createNewFile() : 创建文件 file.exists() : 判断文件是否存 ...

  5. Java学习之IO之File类二

    之前学了File便想把我学习视频的名字改了,因为文件名太长不好看,便试着写了个功能实现 package com.gh.file; import java.io.File; /** * 批量文件命名 * ...

  6. java学习笔记IO之字节输入输出流

    IO字节输入输出流 OutputStream:字节输出流 该抽象类是所有字节输出流的超类: 定义了一些共性的成员方法: 1.写入一个字节 void write(int b);//b表示字节 2.写入字 ...

  7. 6.2(java学习笔记)字节流

    一.FileInputStream 文件输入流从文件中获取输入字节.简单的说就是读取文件. 1.构造方法 FileInputStream(File file)//根据指定文件对象创建一个输入流 2.常 ...

  8. java学习笔记 --- IO(3)

    1.FileReader:读取字符流,默认GBK public class CharStreamDemo { public static void main(String[] args) throws ...

  9. java学习笔记 --- IO(2)

    IO流的分类:  流向:  输入流  读取数据  输出流 写出数据  数据类型: 字节流 字节输入流  读取数据 InputStream 字节输出流  写出数据 OutputStream 字符流 字符 ...

随机推荐

  1. sql 分割字符串 存储过程

    默认是用 , 分割,如有需要可以自己更改 我写的是 循环插入数据 存储过程,也可以自己改成 方法使用 存储过程: ),@jrid int) as begin declare @location int ...

  2. iOS 监听键盘变化

    //将要显示键盘 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShowKeyboard: ...

  3. c++之构造函数学习

    #include<stdio.h> class Test {      private:      int i;      int j;      int k;     public :  ...

  4. WTL error C3861: 'DDX_Control': identifier not found

    error C3861: 'DDX_Control': identifier not found 继承类加上     public CWinDataExchange<CMainDlg>, ...

  5. Vijos 1083 小白逛公园(线段树)

    线段树,每个结点维护区间内的最大值M,和sum,最大前缀和lm,最大后缀和rm. 若要求区间为[a,b],则答案max(此区间M,左儿子M,右儿子M,左儿子rm+右儿子lm). ----------- ...

  6. AeroSpike 记录

    1.基本概念: namespace:类似关系型数据库中的schema,这个需要在配置文件中配置,可以指定存储引擎.存储大小.备份数.存活时间等 set:类似关系型数据库中的表 record:类似关系型 ...

  7. Linux 中 java 访问 windows共享目录

    有两个方案: 1.将windows共享目录,挂载到linux系统下,通过使用本地目录访问windows共享目录 2.通过samba的java实现包,不过需要开个windows共享目录的账户  http ...

  8. Windows下安装Apache2.4+PHP5.4+Mysql5.7

    注:文中所写的安装过程均在Win7 x86下通过测试,提供的百度云下载链接均为32位安装包,如需Apache和PHP的64位安装包请从官网下载! 一.安装Apache2.4.12 Apache官方下载 ...

  9. BootStrap 轮播 Carousel

    参考 http://wrongwaycn.github.io/bootstrap/docs/javascript.html#collapse 同样 启动方式有2种 一种是在div的class中加  另 ...

  10. net-snmp启用python模块

    1. http://blog.jeoygin.org/2011/09/centos-5-install-python-2-6-and-python-setuptools-2-6.html 2. htt ...