字节流与字符流

字节流可以处理所有类型的数据(图片、视频等),在java中对应的类都为“stream”结尾
1字节=8位二进制=具体存储空间
 
字符流仅能处理纯文本的数据,在java中对应的类都是以“reader”或者“writer”结尾
如汉字,符号等
 
 

import org.junit.Test; 

public class IOTest {
/**
* BufferedInputStream BufferedOutputStream
* 利用字节缓冲流实现文件的复制
* @throws IOException
* */
@Test
public void bufferedInputStreamAndbufferedOutputStream() throws IOException{
//新建字节的输入输出
InputStream in = new FileInputStream("hellow.txt");
BufferedInputStream bStream = new BufferedInputStream(in); OutputStream out = new FileOutputStream("hellow2.txt");
BufferedOutputStream bOutputStream = new BufferedOutputStream(out);
//创建缓冲数组
byte[] bytes = new byte[100];
//进行复制
int len = 0;
while((len = bStream.read(bytes))!= -1)
{
bOutputStream.write(bytes, 0, len);
}
//关闭流
bStream.close();
bOutputStream.close();
}
/**
* bufferdeReader And bufferedWriter
* 利用缓冲流实现文件的复制
* @throws IOException
* **/
@Test
public void bufferdeReaderAndbufferedWriter() throws IOException {
// 新建字符的输入输出
Reader in = new FileReader("hellow.txt");
BufferedReader bReader = new BufferedReader(in); Writer out = new FileWriter("hellow2.txt");
BufferedWriter bWriter = new BufferedWriter(out);
// 进行复制
String line = null;
int i = 0;
while ((line = bReader.readLine()) != null) {
if (i != 0)
bWriter.write("\n");
bWriter.write(line, 0, line.length());
i++;
}
// 关闭流
bReader.close();
bWriter.close();
} /**
*reader writer
* 利用字符输入输出流, 完成 hello.txt 文件的复制.
* 把该文件复制为 hello2.txt
*/ @Test
public void readerAndwriter() throws IOException {
//新建字符的输入输出
Reader reader = new FileReader("hellow.txt");
Writer writer = new FileWriter("hellow2.txt");
//定义数组,用于读写文件
char[] cbuf = new char[100];
//读写文件
int len;
while((len = reader.read(cbuf)) != -1)
{
writer.write(cbuf, 0, len);
}
//关闭流
reader.close();
writer.close();
} /**
*InputStream OutputStream
* 利用字节输入输出流, 完成 hello.txt 文件的复制.
* 把该文件复制为 hello2.txt
* @throws IOException
*/
@Test
public void testCopyFile() throws IOException{
//1. 创建定位到 hello.txt 的文件的输入流
InputStream in = new FileInputStream("枚举类.avi"); //2. 创建定位到 hello2.txt 的文件输出流
OutputStream out = new FileOutputStream("枚举类2.avi"); //3. 创建一个 byte 数组, 用于读写文件
byte [] buffer = new byte[1024 * 10];
int len = 0; //4. 读写文件:
//in.read(buffer); out.write(buffer, 0, len);
while((len = in.read(buffer)) != -1){
out.write(buffer);
} //5. 关闭流资源.
out.close();
in.close();
} /**
* 测试字节输出流 OutputStream
* @throws IOException
*/
@Test
public void testOutputStream() throws IOException{
OutputStream out = new FileOutputStream("abcd.txt"); String content = "www.atguigu.com\nHello Java!";
out.write(content.getBytes()); out.close();
} /**
* 测试字符输入流. Reader
* @throws IOException
*/
@Test
public void testReader() throws IOException{
//利用字符输入流读取 hello.txt 文档的内容, 输出到控制台.
Reader reader = new FileReader("hello.txt"); char [] buffer = new char[10];
int len = 0; while((len = reader.read(buffer)) != -1){
for(int i = 0; i < len; i++){
System.out.print(buffer[i]);
}
} reader.close();
} /**
* 测试字节输入流 InputStream
* @throws IOException
*/
@Test
public void testInputStream() throws IOException{
//1. 创建了一个字节输入流.
InputStream in = new FileInputStream("hello.txt"); //2. 读取文件的内容
//2.1 第一读取一个字节. 效率很低, 不建议这样读. -1 表示读取到文件的结尾处
// int result = in.read();
//
// while(result != -1){
// System.out.print((char)result);
// result = in.read();
// } //2.2 一次读取一组: 一组字符.
//返回一次实际读取的字节数, 若为 -1 表示读取到文件的结尾
// byte [] buffer = new byte[10];
// int len = 0;
//
// while((len = in.read(buffer)) != -1){
// for(int i = 0; i < len; i++){
// System.out.print((char)buffer[i]);
// }
// } //2.3 把内容读取到字节数组的部分连续的元素中.
byte [] result = new byte[1024 * 10];
in.read(result, 10, in.available()); //3. 关闭流资源
in.close();
} /**
* File: 代表物理的意义的文件或目录
* @throws IOException
*/
@Test
public void testFile() throws IOException{
//1. 创建 File 对象
File file = new File("hello.txt"); //2. 测试 File 对象的方法.
//2.1 文件名相关的方法
String fileName = file.getName();
System.out.println(fileName); //2.2 访问文件的绝对路径
String path = file.getAbsolutePath();
System.out.println(path); //2.3 为文件重命名
//file.renameTo(new File("d:\\hello.txt")); //3. 文件检测相关的方法
System.out.println(file.exists());
File dir = new File("atguigu");
System.out.println(dir.isFile()); //4. 获取文件的常规信息
System.out.println(file.length()); //5. 文件操作相关.
File file2 = new File("abcd.txt");
file2.createNewFile();
} }

java的IO学习,字节流与字符流的编码讲解的更多相关文章

  1. java学习笔记之IO编程—字节流和字符流

    1. 流的基本概念 在java.io包里面File类是唯一一个与文件本身有关的程序处理类,但是File只能够操作文件本身而不能操作文件的内容,或者说在实际的开发之中IO操作的核心意义在于:输入与输出操 ...

  2. java IO之字节流和字符流-Reader和Writer以及实现文件复制拷贝

    接上一篇的字节流,以下主要介绍字符流.字符流和字节流的差别以及文件复制拷贝.在程序中一个字符等于两个字节.而一个汉字占俩个字节(一般有限面试会问:一个char是否能存下一个汉字,答案当然是能了,一个c ...

  3. java IO的字节流和字符流及其区别

    1. 字节流和字符流的概念    1.1 字节流继承于InputStream    OutputStream,    1.2 字符流继承于InputStreamReader    OutputStre ...

  4. java IO通过字节流,字符流 读出写入

    一:通过字节流操作数据的写入,读出 /** * 通过字节流写入和读出 * @param args */ public static String filePath = "G:" + ...

  5. Java中常用的字节流和字符流

    IO流(输入流.输出流) 字节流.字符流 1.字节流: InputStream.OutputStream InputStream抽象了应用程序读取数据的方式: OutputStream抽象了应用程序写 ...

  6. IO(字节流、字符流)

      第1章 字节流 在前面的学习过程中,我们一直都是在操作文件或者文件夹,并没有给文件中写任何数据.现在我们就要开始给文件中写数据,或者读取文件中的数据. 1.1 字节输出流OutputStream ...

  7. java:I/O 字节流和字符流

    字节流 InputStream和OutputStream的子类:FileInputStream 和 FileOutputStream 方法: int read(byte[] b,int off,int ...

  8. [Day23]IO(字节流、字符流)

    1.字节流 1.1 字节输出流OutputStream-抽象类,表示输出字节流的所有类的超类 (1)void close() 关闭此输出流并释放与此流相关的所有系统资源 (2)void flush() ...

  9. Java 带缓冲的字节流和字符流

    输入流就是文件从硬盘到内存的中间媒介,那么输出流就是文件从内存到硬盘的中间媒介.首先来看看FileOutputStream的继承了哪些类, java.lang.Object java.io.Outpu ...

随机推荐

  1. OpenJudge 2787 算24

    1.链接地址: http://poj.org/problem?id=1631 http://bailian.openjudge.cn/practice/2787/ 2.题目: 总时间限制: 3000m ...

  2. Eclipse相关

    JDK版本更换相关: 启动eclipse会报错:根据报错信息后面提示的eclipse配置信息,我将配置中的c:/xx/javaw.exe给移除了.并在eclipse.ini中配置了-vm d:/Jav ...

  3. [Computer Vision] SIFT特征学习笔记

    SIFT(Scale Invariant Feature Transform),尺度空间不变特征,目前手工设计的最好vision特征. 以下是学习http://blog.csdn.net/zddblo ...

  4. jQuery弹出层_点击自身以外地方关闭弹出层

    <html> <style> .hide{display:none;} </style> <script type="text/javascript ...

  5. node-webkit:开发桌面+WEB混合型应用的神器

    顾名思义, node -webkit就是 node js+webkit. 这样做的好处显而易见,核心奥义在于,用 node js来进行本地化调用,用webkit来解析和执行HTML+JS. 快速上手 ...

  6. 基于u-boot源码的简单shell软件实现

    一.概述 1.shell概念 Shell(命令解析器),它用于接收用户输入的命令,进行解析,然后调用相应的应用程序,为使用者提供了使用软件的界面. shell是操作系统最外面的一层.shell管理你与 ...

  7. mysql5.7版本无法启动服务问题

    cmd情况下进入mysql的bin目录后 输入命令:mysqld --initialize-insecure d:\mysql\bin

  8. C语言写解一元二次方程程序心得

    前言:在网上看到不少解一元二次方程的小程序,在使用时总得出一大堆小数,感觉很不爽,遂自己重新写了一遍. 首先,先回忆一下一元二次方程的求根公式: 分别读取二次项.一次项和常数项系数并且求出delta ...

  9. UOJ179 线性规划

    Description 这是一道模板题. 本题中你需要求解一个标准型线性规划: 有\(n\)个实数变量\(x_1,x_2,\cdots,x_n\)和\(m\)条约束,其中第\(i\)条约束形如\(\s ...

  10. 解决iOS应用内购买报错:invalidProductIdentifiers

    当写完IAP业务过程后,点击测试却发现没有返回成功的商品Id,反而返回了无效的商品:response.invalidProductIdentifiers 这种情况下考虑以下因素: 创建的App ID是 ...