字节流和字符流的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) ...
随机推荐
- Java-天平称重
题目: 用天平称重时,我们希望用尽可能少的砝码组合称出尽可能多的重量.如果只有5个砝码,重量分别是1,3,9,27,81 则它们可以组合称出1到121之间任意整数重量(砝码允许放在左右两个盘中).本题 ...
- mui下拉刷新上拉加载
新外卖商家端主页订单大厅页面 使用mui双webview,实现下拉刷新上拉加载 主页面: order_index.html <!doctype html> <html> < ...
- LAMP(七)之编译安装php(模块化和fpm两种方式)
安装前说明: 安装环境: CentOS6 安装应用程序:httpd2.4 + mariadb + php 安装次序: 先编译安装 httpd2.4和mariadb,最后安装php 编译安装 httpd ...
- Educational Codeforces Round 79 (Rated for Div. 2) Finished (A-D)
如果最大值比剩余两个加起来的总和+1还大,就是NO,否则是YES #include<bits/stdc++.h> using namespace std; int main(){ int ...
- Maven修改test/rsource的output folder报错Test source folder 'src/test/java'... is not also used for main s
eclipse新建maven项目时候,只出来三个文件夹,然后大都督手动添加了缺失的src/test/resource 的文件夹,最后想修改一下 Output folder的路径为 (原来是 d ...
- PAT (Basic Level) Practice (中文)1066 图像过滤 (15 分)
图像过滤是把图像中不重要的像素都染成背景色,使得重要部分被凸显出来.现给定一幅黑白图像,要求你将灰度值位于某指定区间内的所有像素颜色都用一种指定的颜色替换. 输入格式: 输入在第一行给出一幅图像的分辨 ...
- AI 数学基础 : 熵
什么是熵(entropy)? 1.1 熵的引入 事实上,熵的英文原文为entropy,最初由德国物理学家鲁道夫·克劳修斯提出,其表达式为: 它表示一个系系统在不受外部干扰时,其内部最稳定的状态.后来一 ...
- JS首字母进行分类合并加排序
let array = ['fds', 'ewfg1', 'cvd', 'ew', 'qer', 'jjh', 'rth', 'asd', 'vsd', 'tteh', 'fxv']; let map ...
- python 元组 列表 字典
type()查看类型 //取整除 **幂 成员运算符: in x在y序列中,就返回true 反之 not in 身份运算符: is is not 逻辑运算符 and or not 字符编码 问题 ...
- 仿ios按钮切换
<div> <label><input class="btn-switch" type="checkbox"> 默认未选中& ...