java的IO学习,字节流与字符流的编码讲解
字节流与字符流
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学习,字节流与字符流的编码讲解的更多相关文章
- java学习笔记之IO编程—字节流和字符流
1. 流的基本概念 在java.io包里面File类是唯一一个与文件本身有关的程序处理类,但是File只能够操作文件本身而不能操作文件的内容,或者说在实际的开发之中IO操作的核心意义在于:输入与输出操 ...
- java IO之字节流和字符流-Reader和Writer以及实现文件复制拷贝
接上一篇的字节流,以下主要介绍字符流.字符流和字节流的差别以及文件复制拷贝.在程序中一个字符等于两个字节.而一个汉字占俩个字节(一般有限面试会问:一个char是否能存下一个汉字,答案当然是能了,一个c ...
- java IO的字节流和字符流及其区别
1. 字节流和字符流的概念 1.1 字节流继承于InputStream OutputStream, 1.2 字符流继承于InputStreamReader OutputStre ...
- java IO通过字节流,字符流 读出写入
一:通过字节流操作数据的写入,读出 /** * 通过字节流写入和读出 * @param args */ public static String filePath = "G:" + ...
- Java中常用的字节流和字符流
IO流(输入流.输出流) 字节流.字符流 1.字节流: InputStream.OutputStream InputStream抽象了应用程序读取数据的方式: OutputStream抽象了应用程序写 ...
- IO(字节流、字符流)
第1章 字节流 在前面的学习过程中,我们一直都是在操作文件或者文件夹,并没有给文件中写任何数据.现在我们就要开始给文件中写数据,或者读取文件中的数据. 1.1 字节输出流OutputStream ...
- java:I/O 字节流和字符流
字节流 InputStream和OutputStream的子类:FileInputStream 和 FileOutputStream 方法: int read(byte[] b,int off,int ...
- [Day23]IO(字节流、字符流)
1.字节流 1.1 字节输出流OutputStream-抽象类,表示输出字节流的所有类的超类 (1)void close() 关闭此输出流并释放与此流相关的所有系统资源 (2)void flush() ...
- Java 带缓冲的字节流和字符流
输入流就是文件从硬盘到内存的中间媒介,那么输出流就是文件从内存到硬盘的中间媒介.首先来看看FileOutputStream的继承了哪些类, java.lang.Object java.io.Outpu ...
随机推荐
- IOS 学习日志 2015-3-17
Objective--C 一 关键字 @class 导入已有的类 id 对象类型 表示任何一个ObjC对象类型 Block 对象类型 OC中称为代码块 类似于C中的函数式指针 typedef 定义数据 ...
- wamp5 忘记mysql root密码 重置方法
wamp5比较恶心,忘记mysql root密码了,重新安装都没用,网上找了个文章可以修改root的密码. http://www.kuqin.com/database/20080306/4249.ht ...
- C#事件作用和用法
例如有下面的需求需要实现:程序主画面中弹出一个子窗口.此时主画面仍然可以接收用户的操作(子窗口是非模态的).子窗口上进行某些操作,根据操作的结果要在主画面上显示不同的数据. 即如下图所示: 大多数我们 ...
- 头文件的宏定义#ifndef测试
一.入题 在头文件的书写中,都加入了如下内容: #ifndef __头文件名_H #define __头文件名_H #endif 曾经在书中看到的解释是“防止重复定义”,今天突然想到为什么是这样的解释 ...
- STM32库函数实现方法
一.概述 1.调用STM32库函数配置与直接配置寄存器 ① 直接配置寄存器 使用过51单片机的朋友都知道为了将IO口配置成某种特殊功能或者配置中断控制,我们先将需要如下步骤: 根据需要配置功能计算值- ...
- gcc向待编译源文件传入参数的方法
gcc有两种方法向待编译源文件传入参数 第一种 利用–Dmacro=name 编译选项,详见gcc -D选项 第二种 利用链接脚本(*.lds)传入参数,类似于ADS的编译器参数可以被待编译源文件调用 ...
- 懒加载 jquery代码
懒加载代码.据说这是jquery代码. 说白了就是在 开始的时候调用,这个和C#代码错误处理机制是一样的. function check() { var obj = document ...
- URL参数加密解密
/// <summary> /// DES加密字符串 /// </summary> /// <param name=" ...
- HTML5之本地文件系统API - File System API
HTML5之本地文件系统API - File System API 新的HTML5标准给我们带来了大量的新特性和惊喜,例如,画图的画布Canvas,多媒体的audio和video等等.除了上面我们提到 ...
- JavaWeb学习总结(一)——JavaWeb开发入门(转载)
转载自http://www.cnblogs.com/xdp-gacl/p/3729033.html感谢孤傲苍狼的分享. 一.基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意 ...