字节流和字符流的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) ...
随机推荐
- Uva12716 素数筛思想的应用
Uva12716 题意: 输入整数n,1<= n <=3e7,问有多少个整数对(a,b)满足:1 <= b <= a <= n,且gcd(a,b)== a XOR b 解 ...
- HTTP协议、时间戳、防盗链的一些概念
HTTP协议 什么是HTTP协议 (HyperText Transfer Protocol,超文本传输协议)是因特网上应用最为广泛的一种网络传输协议,所有的WWW文件都必须遵守这个标准. HTTP是一 ...
- Windows添加管理员用户
使用命令提示符添加用户并提升管理员权限 net user 查看当前所有系统用户 net user dan 123456 /add 添加用户名为dan 密码为123456的用户 net localgro ...
- 2020.01.19【NOIP提高组】模拟比赛-1.水池,2.数字排序,3.球星,4.钻石交易 总结反思
水池 比赛时 我最讨厌这种数学类题了,我首先想到了这几种情况,设\(jl[][]\)表示两点之间弧的距离,从F到G可以由 F->G F->B->A->G F->A-> ...
- xadmin使用
xadmin使用 官方 使用参考
- openlayers图层加标注
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- ASP.NET Identity系列教程-3【运用ASP.NET Identity】
https://www.cnblogs.com/r01cn/p/5180892.html 14 运用ASP.NET Identity In this chapter, I show you how t ...
- PAT (Advanced Level) Practice 1036 Boys vs Girls (25 分)
This time you are asked to tell the difference between the lowest grade of all the male students and ...
- Wannafly Camp 2020 Day 3F 社团管理 - 决策单调性dp,整体二分
有 \(n\) 个数构成的序列 \({a_i}\),要将它划分为 \(k\) 段,定义每一段的权值为这段中 \((i,j) \ s.t. \ i<j,\ a_i=a_j\) 的个数,求一种划分方 ...
- 1.mysql卸载重新安装问题
前言:在开发过程中,MySQL数据库是很频繁使用的数据库,但是有时候,数据库一单出错,或者其他原因,想要重装数据库,难免会遇到MySQL重装之后服务启不来,,下面我就跟大家讨论下如何干净的卸载MySQ ...