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代理
之前看过一些关于代理的资料,始终感觉还是很模糊,最近抽出一段空闲的时间,将这块内容整理下: 什么是代理,顾名思义就是帮别人做些事情,比如买房子,当我们买房子时,我们一般会找房屋中介,因为他们就是干这件 ...
- wamp配置
# #localhost # <VirtualHost *:> DocumentRoot "D:/software/wamp/www" ServerName local ...
- make makefile
Gcc的编译流程预处理阶段: gcc –E hello.c –o hello.i编译阶段: gcc –S hello.i –o hello.s汇编阶段:gcc –c hello.s –o hello. ...
- jquery上传插件uploadify 报错http error 302 解决方法之一
前段时间用到jquery上传插件uploadify时,始终出现系统报出 http error 302 的错误. 网上大量搜集信息,基本上都是说session值丢失的问题,根据网友提供的解决方案进行修改 ...
- #Leet Code# Evaluate Reverse Polish Notation
描述:计算逆波兰表达法的结果 Sample: [", "*"] -> ((2 + 1) * 3) -> 9 [", "/", & ...
- linux的fork函数
fork函数 头文件:#include<unistd.h> 函数原型:pid_t fork( void);(pid_t 是一个宏定义,其实质是int 被定义在#include< ...
- Navigation bar 的注意事项
Navigation bar 的注意事项 Bar button item 使用 button 作为 custom view,初始化 isEnabled 为 false,注意顺序 需要设置 bar bu ...
- 浅谈JavaScript的push()函数
push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度.返回值是把指定的值添加到数组后的新长度. 语法:arrayObject.push(newelement1,newelement2,. ...
- vs2010 使用SignalR 提高B2C商城用户体验(三)
vs2010 使用SignalR 提高B2C商城用户体验(三) 上一章节,我们的web即时通讯已经可以实现跨域了,但针对我们的需求,还希望,一些客户端程序可以和我们的web用户,在线聊天,所以到Sig ...
- jersey REST的接口简述
GET方法:只读,是幂等和安全的.在接口中抽象地定义了资源的请求方法类型后,其全部实现类都无须再定义. HEAD方法:与GET方法相似,只是服务器端的返回值不包括HTTP实体. OPTIONS方法:与 ...