字节流和字符流的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方法的更多相关文章

  1. java_字节流、字符流的使用方法

    字节流 字节输出流[OutputStream] java.io.OutputStream 抽象类是表示字节输出流的所有类的超类,将指定的字节信息写出到目的地.它定义了字节输出流的基本共性功能方法. p ...

  2. javaIO流(二)--字节流与字符流

    一.流的基本概念 在java.io包中,File类是唯一一个与文件本身有关的程序处理类,但是File类只能操作文件本身,而不能操作文件内容,IO操作的核心意义在于输入和输出操作.而对于程序而言,输入和 ...

  3. java学习笔记之IO编程—字节流和字符流

    1. 流的基本概念 在java.io包里面File类是唯一一个与文件本身有关的程序处理类,但是File只能够操作文件本身而不能操作文件的内容,或者说在实际的开发之中IO操作的核心意义在于:输入与输出操 ...

  4. java 字节流与字符流的区别

    字节流与和字符流的使用非常相似,两者除了操作代码上的不同之外,是否还有其他的不同呢?实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作 ...

  5. (转)java字节流和字符流的区别

    转载: http://www.cnblogs.com/dolphin0520/category/361055.html 字节流与和字符流的使用非常相似,两者除了操作代码上的不同之外,是否还有其他的不同 ...

  6. java中字节流与字符流的区别

    字节流 在I/O类库中,java.io.InputStream和java.io.OutputStream分别表示字节输入流和字节输出流,它们都是抽象类,不能实例化,数据流中的最小单位是字节,所以叫做字 ...

  7. JAVA IO 字节流与字符流

    文章出自:听云博客 题主将以三个章节的篇幅来讲解JAVA IO的内容 . 第一节JAVA IO包的框架体系和源码分析,第二节,序列化反序列化和IO的设计模块,第三节异步IO. 本文是第一节.     ...

  8. Java字节流与字符流基本操作

    在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据时要使用输入流读取数据,而当程序需要将一些数据保存起来时,就要使用输出流. 在java.io包中流的操作主要有字节流.字符流两大类,两类都 ...

  9. 字节流与字符流的区别&&用字节流好还是用字符流好?

    字节流: (A)FileOutputStream(File name) 创建一个文件输出流,向指定的 File 对象输出数据. (B)FileOutputStream(FileDescriptor) ...

随机推荐

  1. linux下安装setuptools

    wget https://pypi.python.org/packages/07/a0/11d3d76df54b9701c0f7bf23ea9b00c61c5e14eb7962bb29aed866a5 ...

  2. tensor维度变换

    维度变换是tensorflow中的重要模块之一,前面mnist实战模块我们使用了图片数据的压平操作,它就是维度变换的应用之一. 在详解维度变换的方法之前,这里先介绍一下View(视图)的概念.所谓Vi ...

  3. mybatis-plus invalid bound statement (not found) insert解决办法

    使用mybatis-plus时,使用IService.insert方法时,提示找不到insert方法,原因是,mybatis-plus提供了两个BaseMapper和IService. 改成引用imp ...

  4. 台大郭彦甫MATLAB教学-个人笔记(一)

    命令和一些特殊的变量 who:查看有哪些变量1. whos:可以查看变量的大小.字节和类型等资料. clear:如果单独使用则是清空所有命令,若后面跟着一个变量名称则为删除此变量. clc:清空命令行 ...

  5. 题解【AcWing1089】烽火传递

    题面 单调队列优化 DP 模板题. 我们考虑设 \(dp_{i}\) 表示从 \(1\) 到 \(i\) 能够准确传递情报,且第 \(i\) 个烽火台发出信号的最小费用. 转移方程不难得出:\(dp_ ...

  6. C++ -> 在使用动态链表和异质链表产生野指针的步骤

    C++ -> 在使用动态链表和异质链表产生野指针的步骤 使用异质链表产生野指针的情况,下面是修改书本的例子: ------------------------------------------ ...

  7. 微信小程序调试页面的坑

    使用微信开发者工具切新的页面保存刷新无法在左侧直接预览必须在app.json文件配置页面(填写路径但是不用写后缀名),并且把想要预览的页面放在第一个位置.

  8. [Python]pyhon去除txt文件重复行 python 2020.2.10

    代码如下: import shutil readPath='E:/word4.txt' #要处理的文件 writePath='E:/word5.txt' #要写入的文件 lines_seen=set( ...

  9. vue animate.css训练动画案例 列表循环

    制作目标动画:向上入场添加数据,点击数据右滑动离场 简单页面效果: 实现代码如下: <!DOCTYPE html> <html> <head> <meta c ...

  10. PP: Learning representations for time series clustering

    Problem: time series clustering TSC - unsupervised learning/ category information is not available. ...