字节流与字符流

字节流可以处理所有类型的数据(图片、视频等),在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. 原型模式(Prototype Pattern)

    原型模型:用于创建重复对象,同时保证性能. 这种模式实现一个原型接口,用于创建对象的克隆,当直接创建对象的代价比较大,则可以采用这种模式.例如:一个对象需要高代价的数据库操作之后被创建,这时可以缓存该 ...

  2. 代码方式删除SVN

    public static void delect(File s) { File b[] = null; if (s.exists()) {// 判读是否存在 if (s.isDirectory()) ...

  3. DOM 文档对象模型

    document 对象(作为对象),是 DOM 的核心作用:对内容,属性,样式等操作属性:title:设置/返回当前文档的标题url:返回当前文档的 urlinnerHTML:获取指定对象内的内容bg ...

  4. 不能正确获得上次构建以来的Commit

    不能正确获得上次构建以来的Commit 如何解决?

  5. PDO操作mysql数据库(二)

    从 MySQL 数据库读取数据 <?php $server = "localhost"; $user = "root"; $pwd = "123 ...

  6. 【python】【转】if else 和 elif

    else和elif语句也可以叫做子句,因为它们不能独立使用,两者都是出现在if.for.while语句内部的.else子句可以增加一种选择:而elif子句则是需要检查更多条件时会被使用,与if和els ...

  7. [简历] PHP 技能关键字列表

    本技能关键字列表是从最近招聘PHP的数百份JD中统计出来的,括号中是出现的词频.如果你的简历要投递给有机器(简历分选系统)和不如机器(不懂技术的HR)筛选简历环节的地方,请一定从下边高频关键词中选择5 ...

  8. [日语歌词] If

    原唱:西野カナ (にしのカナ) 作词:西野カナ/GIORGIO 13 作曲:GIORGIO CANCEMI 1.单词表 仮名 漢字 ひ 日 あめ 雨 や 止 ちがい 違い とおり 通り じかん 時間 ...

  9. VMware网络选项分析

    摘自资料:VMware网卡选项分析.zip 很多朋友都曾问到关于Guest和Host互联,其实这并不是一件困难的事情,只要能够理解VMware的网络模型即可,今天结合着我的虚拟机,来详细介绍一下VMw ...

  10. SDUT 1305 查找基因序列问题 dp

    题目: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1305 这个题就是一个类似公共子串的dp ...