字节流和字符流的read方法
字节流和字符流的read方法
public class Test {
public void fileOutput() throws Exception {
File file = new File("D:/2.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);
String s = "abcdefg";
fileOutputStream.write(s.getBytes());
fileOutputStream.close();
}
/**
* fileInputStream.read()是一个字节一个字节的读,返回值为根据ascii码表转成的int值
* 输出结果
97
a
98
b
99
c
100
d
101
e
102
f
103
g
*
* @throws Exception
*/
public void fileInput() throws Exception {
File file = new File("D:/2.txt");
FileInputStream fileInputStream = new FileInputStream(file);
int a;
while ((a = fileInputStream.read()) != -1) {
System.out.println(a);
System.out.println((char)a);
}
fileInputStream.close();
}
/**
* 输出结果:
* 97
* 98
* 99
* 100
* 101
* 102
* 103
* [B@5a8e6209
* abcdefg
*
* @throws Exception
*/
public void fileInput2() throws Exception {
File file = new File("D:/2.txt");
FileInputStream fileInputStream = new FileInputStream(file);
int a;
int[] b = new int[10];
byte[] c = new byte[10];
int len = 0;
while ((a = fileInputStream.read()) != -1) {
b[len] = a;
c[len] = (byte) a;
len++;
}
for (int i = 0; i < len; i++) {
System.out.println(b[i]);
}
System.out.println(c.toString());
System.out.println(new String(c));
fileInputStream.close();
}
/**
* 带参数read(byte[] b)方法,读取参数b字节大小
* 其返回值为int类型,the total number of bytes read into the buffer, or <code>-1</code> if there is no more data because the end of the file has been reached.
* 输出结果:
* 7
* -1
* abcdefg
*
* @throws Exception
*/
public void fileInput3() throws Exception {
File file = new File("D:/2.txt");
FileInputStream fileInputStream = new FileInputStream(file);
byte[] b = new byte[10];
byte[] bb = new byte[5];
int a = fileInputStream.read(b);
int c = fileInputStream.read(bb);
System.out.println(a);
System.out.println(c);
System.out.println(new String(b));
fileInputStream.close();
}
/**
* 输出结果
* 7
* abcdefg
*
* @throws Exception
*/
public void fileInput4() throws Exception {
File file = new File("D:/2.txt");
FileInputStream fileInputStream = new FileInputStream(file);
byte[] b = new byte[10];
int a = fileInputStream.read(b, 0, new Long(file.length()).intValue());
System.out.println(a);
System.out.println(new String(b));
fileInputStream.close();
}
/**
* 输出结果:
* 97
* 98
* 99
* 100
* 101
* 102
* 103
* @throws Exception
*/
public void fileReader() throws Exception {
File file = new File("D:/2.txt");
FileReader fileReader = new FileReader(file);
int a;
while ((a=fileReader.read())!=-1){
System.out.println(a);
}
fileReader.close();
}
/**
* 输出结果:
* abcdefg
* @throws Exception
*/
public void fileReader2() throws Exception {
File file = new File("D:/2.txt");
FileReader fileReader = new FileReader(file);
int a;
int len=0;
byte[] b=new byte[10];
while ((a=fileReader.read())!=-1){
b[len]=(byte) a;
len++;
}
System.out.println(new String(b));
fileReader.close();
}
/**
* 输出结果:
* abcdefg
* @throws Exception
*/
public void fileReader3() throws Exception {
File file = new File("D:/2.txt");
FileReader fileReader = new FileReader(file);
char[] cbuf=new char[10];
fileReader.read(cbuf);
System.out.println(new String(cbuf));
fileReader.close();
}
public static void main(String[] args) throws Exception {
Test test = new Test();
test.fileInput();
}
}
字节流和字符流的read方法的更多相关文章
- java_字节流、字符流的使用方法
字节流 字节输出流[OutputStream] java.io.OutputStream 抽象类是表示字节输出流的所有类的超类,将指定的字节信息写出到目的地.它定义了字节输出流的基本共性功能方法. p ...
- javaIO流(二)--字节流与字符流
一.流的基本概念 在java.io包中,File类是唯一一个与文件本身有关的程序处理类,但是File类只能操作文件本身,而不能操作文件内容,IO操作的核心意义在于输入和输出操作.而对于程序而言,输入和 ...
- java学习笔记之IO编程—字节流和字符流
1. 流的基本概念 在java.io包里面File类是唯一一个与文件本身有关的程序处理类,但是File只能够操作文件本身而不能操作文件的内容,或者说在实际的开发之中IO操作的核心意义在于:输入与输出操 ...
- java 字节流与字符流的区别
字节流与和字符流的使用非常相似,两者除了操作代码上的不同之外,是否还有其他的不同呢?实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作 ...
- (转)java字节流和字符流的区别
转载: http://www.cnblogs.com/dolphin0520/category/361055.html 字节流与和字符流的使用非常相似,两者除了操作代码上的不同之外,是否还有其他的不同 ...
- java中字节流与字符流的区别
字节流 在I/O类库中,java.io.InputStream和java.io.OutputStream分别表示字节输入流和字节输出流,它们都是抽象类,不能实例化,数据流中的最小单位是字节,所以叫做字 ...
- JAVA IO 字节流与字符流
文章出自:听云博客 题主将以三个章节的篇幅来讲解JAVA IO的内容 . 第一节JAVA IO包的框架体系和源码分析,第二节,序列化反序列化和IO的设计模块,第三节异步IO. 本文是第一节. ...
- Java字节流与字符流基本操作
在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据时要使用输入流读取数据,而当程序需要将一些数据保存起来时,就要使用输出流. 在java.io包中流的操作主要有字节流.字符流两大类,两类都 ...
- 字节流与字符流的区别&&用字节流好还是用字符流好?
字节流: (A)FileOutputStream(File name) 创建一个文件输出流,向指定的 File 对象输出数据. (B)FileOutputStream(FileDescriptor) ...
随机推荐
- 在IE8的基础上安装IE11
安装包下载 链接:https://pan.baidu.com/s/1WAuyMg_SrfVa0wLjoop5Jw 提取码:nh1r 安装方式 先运行 - EIE11_ZH-CN_WOL_WIN76 ...
- 我国自主开发的编程语言“木兰”居然是一个披着“洋”皮的Python!
究竟是真“自主”,还是又一个披着“洋”皮的“红芯浏览器”? 作者 | 沉迷单车的追风少年 出品 | CSDN博客 昨天看到新闻: ! 心头一震,看起来很厉害啊!毕竟前几天美国宣布要对中国AI软 ...
- 给你的网站添加谷歌AMP、百度MIP、神马MIP链接自动提交功能
我们在做网站的时候,经常会听到别人说SEO优化,网站优化等等.但是我们经常听的云里雾里的,但是经过我们运营一段时间之后,我们慢慢的就会熟悉了,知道什么是SEO.SEO中文译名为搜索引擎优化,既然是叫搜 ...
- WPF DataGrid标题Header Binding失效
前言:因业务需要对WPF中的DataGrid控件中的行数进行统计,同时把统计的行数显示在列标题上. 如果我们用常规的Binding手段更新DataGridTextColunm的Header的话是不会生 ...
- 牛客寒假6-C汉诺塔
链接:https://ac.nowcoder.com/acm/contest/3007/C来源:牛客网 题目描述 现在你有 N 块矩形木板,第 i 块木板的尺寸是 Xi*Yi,你想用这些木板来玩汉诺塔 ...
- JS获取样式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- AcWing 6. 多重背包问题 III
//f[i,j] 所有只从前i块能量石中选,且总体积恰好为j的方案数 #include <iostream> #include <algorithm> #include < ...
- vjudge A Funny Game 思维题 (其实今天讲的全是数学。。。)
原文链接https://vjudge.net/contest/331993#problem/H Alice and Bob decide to play a funny game. At the be ...
- 《NVM-Express-1_4-2019.06.10-Ratified》学习笔记(6.15)-- 写命令
6.15 Write command 写命令 写命令写数据和元数据,如果适用介质,发到逻辑块相应的I/O controller.主机也可以指定保护信息,作为操作的一部分包含进来. 命令用Command ...
- Python基本数据类型set方法概述
li=[1,2,3,4,5,6,3,2,1] s2 = set(li) print(set(li)) #difference()去除相同项,生成一个新的集合,删除 s3=s2.difference([ ...