字节流和字符流的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) ...
随机推荐
- shadow文件中密码的加密方式
1) 查看shadow文件的内容 cat /etc/shadow 可以得到shadow文件的内容,限于篇幅,我们举例说明: root:$1$Bg1H/4mz$X89TqH7tpi9dX1B9j5YsF ...
- Callablestatement与JavaBean及其实例
一. Callablestatement:调用 数据库中的存储过程.存储函数 connection.prepareCall(参数:存储过程/存储函数名)参数格式:存储过程:(无返回值return,用O ...
- mac anyconnect 卸载方法
打开终端: 输入下面的命令 sudo /opt/cisco/vpn/bin/vpn_uninstall.sh 有问题 端口占用 sudo /opt/cisco/anyconnect/bin/webse ...
- 曼孚科技:AI算法领域常用的39个术语(上)
算法是人工智能(AI)核心领域之一. 本文整理了算法领域常用的39个术语,希望可以帮助大家更好地理解这门学科. 1. Attention 机制 Attention的本质是从关注全部到关注重点.将有限 ...
- LeetCode 160. 相交链表 (找出两个链表的公共结点)
题目链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/ 编写一个程序,找到两个单链表相交的起始节点. 如下面的两 ...
- 原生JS操作class 极致版
// 获取class function getClass(el) { return el.getAttribute('class') } // 设置class function setClass(el ...
- react-native构建基本页面1---主页:tab栏
配置Tab栏 配置Tab栏的图标 注意:使用图标,需要接收 license; /** * Sample React Native App * https://github.com/facebook/r ...
- DirectX11--HLSL语法入门
前言 编写本内容仅仅是为了完善当前的教程体系,入门级别的内容其实基本上都是千篇一律,仅有一些必要细节上的扩充.要入门HLSL,只是掌握入门语法,即便把HLSL的全部语法也吃透了也并不代表你就能着色器代 ...
- Win10安装3 —— U盘启动工具安装
本文内容皆为作者原创,如需转载,请注明出处:https://www.cnblogs.com/xuexianqi/p/12364593.html 一:准备一个空U盘 U盘容量推荐至少8个G,先提前备份好 ...
- 《NVM-Express-1_4-2019.06.10-Ratified》学习笔记(5.23)-- Format NVM command
5.23 Format NVM command - NVM Command Set Specific Format NVM命令用于低级格式化NVM媒介.这个命令被host主机使用,来变更LBA数据大小 ...