java基础10(IO流)-字节流
IO流
输入与输出【参照物是程序】
如果从键盘、文件、网络甚至是另一个进程(程序或系统)将数据读入到程序或系统中,称为输入
如果是将程序或系统中的数据写到屏幕、硬件上的文件、网络上的另一端或者是一个进程(程序或系统),称为输出
IO流的分类
- 根据数据流向不同分为:输入流和输出流
输入流: 程序可以从中读取数据的流
输出流: 程序能向其中写入数据的流 - 根据数据处理类不同分为:字节流和字符流
字节流:以字节为单位传输数据的流
字符流:以字符为单位传输数据的流
注:数据所在文件若能用win下记事本打开则用字符流
IO流类结构图


读数据
InputStream读取数据
int read() : 从此输入流中读取一个数据字节
int read(byte[] b): 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。返回值是一次读取字节数组的个数
int read(byte[] b, int off, int len): 从此输入流中将最多 len 个字节的数据读入一个 byte 数组中
InputStream读取数据(一次读取一个字节)
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamDemo{
public static void main(String[] args) throws IOException{
FileInputStream file = new FileInputStream("/home/hadoop/1.txt");
/*
int by = file.read();//一次读取一个字节
System.out.println(by);
System.out.println((char) by);
*/
//方式1(一次读取一个字节)
int by = 0;
while((by = file.read()) != -1){
// System.out.print(by);//不转的话读出来全是数字
System.out.print((char)by);//这里需要注意文件中如果有中文将出问题,因为将中文也转成char了;要读取的文件中有换行符,直接读取过来,所以这里不需要换行符
}
file.close();
}
}
InputStream读取数据(一次读取一个字节数组)
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamDemo2{
public static void main(String[] args)throws IOException{
FileInputStream fis = new FileInputStream("/home/hadoop/1.txt");
byte[] bys = new byte[1024];
int len = 0;
while((len = fis.read(bys)) != -1){ //读取到的实际长度是-1就没有数据了
System.out.print(new String(bys,0,len));//进行转换
}
fis.close();
}
}
BufferedInputStream读取数据(一次读一个字节)
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class BufferedInputStreamDemo{
public static void main(String[] args)throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("/home/hadoop/1.txt"));
int by =0;
while((by = bis.read()) != -1){
System.out.print((char)by);
}
bis.close();
}
}
BufferedInputStream读取数据(一次读一个字节数组)
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class BufferedInputStreamDemo{
public static void main(String[] args)throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("/home/hadoop/1.txt"));
byte[] bys = new byte[1024];
int len = 0;
while((len = bis.read(bys)) != -1){
System.out.print(new String(bys,0,len));
}
bis.close();
}
}
写数据
OutputStream写数据
void write(byte[] b):将 b.length 个字节从指定的 byte 数组写入此输出流
void write(byte[] b, int off, int len): 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。
void write(int b): 将指定的字节写入此输出流。
FileOutputStream写数据(写一个字节)
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo{
public static void main(String[] args)throws IOException{
/* //创建字节输出流对象
File file = new File("/home/hadoop/1.txt");
FileOutputStream fos = new FileOutputStream(File file);
*/
FileOutputStream fos = new FileOutputStream("/home/hadoop/wu.txt",true);//true表示追加数据
fos.write("wujiadong".getBytes());//将字符串转成字节数组
fos.write(97);//写一个字节
fos.close();
}
}
FileOutputStream写数据(写一个字节数组和写字节数组的部分)
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo{
public static void main(String[] args)throws IOException{
/* //创建字节输出流对象
File file = new File("/home/hadoop/1.txt");
FileOutputStream fos = new FileOutputStream(File file);
*/
FileOutputStream fos = new FileOutputStream("/home/hadoop/wu.txt",true);
byte[] bys = {97,98,99,100};
fos.write(bys);//写一个字节数组
fos.write(bys,1,3);//写一个字节数组的一部分
fos.close();
}
}
BufferedOutputStream写数据
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.BufferedOutputStream;
public class BufferedOutputStreamDemo{
public static void main(String[] args)throws IOException{
// FileOutputStream fis = new FileOutputStream("/home/hadoop/wu2.txt");
// BufferedOutputStream bos = new BufferedOutputStream(fis);
//简单写法
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/home/hadoop/wu3.txt"));
//写数据
bos.write("hello,wujiadong".getBytes());
//释放资源
bos.close();
}
}
读写数据练习
复制数据
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFileDemo{
public static void main(String[] args) throws IOException {
//封装数据源
FileInputStream fis = new FileInputStream("/home/hadoop/11.txt");
//封装目的地
FileOutputStream fos = new FileOutputStream("/home/hadoop/a.txt");
//复制数据
int by = 0;
while((by = fis.read()) != -1){
fos.write(by);
}
//释放资源
fis.close();
fos.close();
}
}
---------------------------------------
注:这种方法一次读一个字节,比较慢
----------------------------------------
复制数据
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFileDemo1{
public static void main(String[] args)throws IOException{
//封装数据源
FileInputStream fis = new FileInputStream("/home/hadoop/11.txt");
FileOutputStream fos = new FileOutputStream("/home/hadoop/wu.txt");
//复制数据
byte[] bys = new byte[1024];
int len = 0;
while((len = fis.read(bys)) != -1){
fos.write(bys,0,len);
}
//释放资源
fis.close();
fos.close();
}
}
字节流4种方式复制文件
/*
* 字节流4种方式复制文件
* 基本字节流一次读写一个字节
* 基本字节流一次读写一个字节数组
* 高效字节流一次读写一个字节
* 高效字节流一次读写一个字节数组
*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
public class CopyDemo{
public static void main(String[] args)throws IOException{
long start = System.currentTimeMillis();
// method1("/home/hadoop/1.txt","/home/hadoop/javatest/1.txt");
// method2("/home/hadoop/1.txt","/home/hadoop/javatest/1.txt");
// method3("/home/hadoop/1.txt","/home/hadoop/javatest/1.txt");
method4("/home/hadoop/1.txt","/home/hadoop/javatest/1.txt");
long end = System.currentTimeMillis();
System.out.println("共耗时:"+(end - start)+"mm");
}
public static void method1(String srcString,String desString)throws IOException{
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(desString);
//方法1:读写一个字节
int by = 0;
while((by = fis.read()) != -1){
fos.write(by);
}
fis.close();
fos.close();
}
public static void method2(String srcString,String desString)throws IOException{
FileInputStream fis = new FileInputStream(srcString);
FileOutputStream fos = new FileOutputStream(desString);
//方式2:读取一个字节数组
byte[] bys = new byte[1024];
int len = 0;
while((len = fis.read(bys)) != -1){
fos.write(bys,0,len);
}
fis.close();
fos.close();
}
public static void method3(String srcString,String desString)throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desString));
//方式3:高效读取一个字节
int by = 0;
while((by = bis.read()) != -1){
bos.write(by);
}
bis.close();
bos.close();
}
public static void method4(String srcString,String desString)throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desString));
//高效读取一个字节数组
byte[] bys = new byte[1024];
int len = 0;
while((len = bis.read(bys)) != -1){
bos.write(bys,0,len);
}
bis.close();
bos.close();
}
}
java基础10(IO流)-字节流的更多相关文章
- java基础之IO流(一)字节流
java基础之IO流(一)之字节流 IO流体系太大,涉及到的各种流对象,我觉得很有必要总结一下. 那什么是IO流,IO代表Input.Output,而流就是原始数据源与目标媒介的数据传输的一种抽象.典 ...
- java基础之IO流(二)之字符流
java基础之IO流(二)之字符流 字符流,顾名思义,它是以字符为数据处理单元的流对象,那么字符流和字节流之间的关系又是如何呢? 字符流可以理解为是字节流+字符编码集额一种封装与抽象,专门设计用来读写 ...
- Java基础之IO流整理
Java基础之IO流 Java IO流使用装饰器设计模式,因此如果不能理清其中的关系的话很容易把各种流搞混,此文将简单的几个流进行梳理,后序遇见新的流会继续更新(本文下方还附有xmind文件链接) 抽 ...
- Java基础:IO流之字节流和字符流
1. 流的概念 流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备.外部文件等. 一个流,必有源端和目的端 ...
- java基础44 IO流技术(输出字节流/缓冲输出字节流)和异常处理
一.输出字节流 输出字节流的体系: -------| OutputStream:所有输出字节流的基类(抽象类) ----------| FileOutputStream:向文件输出数据的输出字节流(把 ...
- java基础43 IO流技术(输入字节流/缓冲输入字节流)
通过File对象可以读取文件或者文件夹的属性数据,如果要读取文件的内容数据,那么我们就要使用IO技术. 一.输入字节流 输入字节流的体系: -------| InputStream:所有输入字节流的 ...
- java基础之 IO流
javaIO流 IO流 : (input output) 输入输出流 :输入 :将文件读到内存中 输出:将文件从内存输出到其他地方. IO技术的作用:主要就是解决设备和设备之间的数据传输问题 ...
- 【java基础】]IO流
IO流 概念: 流的概念源于unix中管道(pipe)的概念,在unix中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备,外部文件等 一个流,一定能够会有源和去向(目的地),他 ...
- java基础06 IO流
IO用于在设备间进行数据传输的操作. Java IO流类图结构: IO流分类 字节流: InputStream FileInputStream BufferedInputStream Output ...
- java基础之io流总结一:io流概述
IO流概念: 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.io流是实现输入和输出的基础,可以方便的实现数据的输入和输出操作. IO流的分类: 根据处理数据类型的不同分为:字符流 ...
随机推荐
- iphone断点下载,断点续传
本文转载至 http://blog.csdn.net/zaitianaoxiang/article/details/6650469 - (void)loadView { NSURLConnection ...
- SQL Server 还原错误“restore database正在异常终止 错误 3154”
今天在还原数据库时,先建立相同名字的数据库,然后在该数据库上右键还原数据库.遇到了这样的一个错误: “备份集中的数据库备份与现有的 'RM_DB' 数据库不同. RESTORE DATABASE 正在 ...
- activity通过流程实例id动态获取流程图并展示在jsp页面上
提供的Service方法如下: Java /** * 获取当前任务流程图 * * @param processInstanceId * @return */ @Override public Inpu ...
- JS中的动态合集与静态合集
JS的动态合集 前言 DOM是JavaScript重要组成部分,在DOM中有三个特别的集合分别是NodeList(节点的集合),NamedNodeMap(元素属性的集合)和HTMLCollection ...
- proguard-project.txt和project.properties混淆代码
[转]利用android proguard混淆代码 防止反编译,优化代码 网上虽然有很多相关博客,不过貌似都不是最新版的..于是百度+谷歌+github上的开源demo,终于成功的配置了androi ...
- SQL Server函数
阅读目录 SQL Server函数---Union与Union All的区别 回到顶部 SQL Server函数---Union与Union All的区别 如果我们需要将两个select语句的结果作为 ...
- Decimal fixed point and floating point arithmetic
decimal — Decimal fixed point and floating point arithmetic — Python 3.8.0a0 documentation https://d ...
- SQLServer中exists和except用法
一.exists 1.1 说明 EXISTS(包括 NOT EXISTS)子句的返回值是一个BOOL值.EXISTS内部有一个子查询语句(SELECT ... FROM...),我将其称为EXIST的 ...
- LinuxCentos系统安装Nginx过程记录
网站服务 想必我们大多数人都是通过访问网站而开始接触互联网的吧.我们平时访问的网站服务就是Web网络服务,一般是指允许用户通过浏览器访问到互联网中各种资源的服务. Web网络服务是一种被动访问的服务程 ...
- 介绍一下except的用法和作用?
Python的except用来捕获所有异常,因为Python里面的每次错误都会抛出一个异常,所以每个程序的错误都被当作一个运行时错误.