I/O流(Stream)

INPUT:输入流,从文件里读OUPUT:输出流,写内容到文件

IO流分为:字符流和字节流

字符流:处理纯文本文件。

字节流:处理可以所有文件。

测试字节输出流OuPut(写):

@Test
public void test7(){ //字节流 FileOutputStream fos=null; try {
fos=new FileOutputStream("./fff.txt"); //创建字节流对象
fos.write("abcd".getBytes()); //与字符流相比,字节流写入的内容后面加.getBytes()方法。 } catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

测试结果:

字节输入流InPut(读):

@Test
public void test8(){ //字节流读(INput) FileInputStream fis=null; try {
fis = new FileInputStream("./fff.txt");
byte[] buffer = new byte[4];
int len=0;
while ((len = fis.read(buffer)) != -1) { //将读出的内容放入byte型的buffer数组
System.out.println(new String(buffer, 0, len));
} } catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} 测试结果:
                   abcd
                   Process finished with exit code 0
带缓冲区的字节流:
缓冲区的作用:减少调用本地API的次数,从而优化系统的开销,缓冲输入流从被称为缓冲区(buffer)的存储器区域读出数据;
仅当缓冲区是空时,本地输入 API 才被调用。同样,缓冲输出流,将数据写入到缓存区,只有当缓冲区已满才调用本机输出 API。
输出流:
@Test
public void test10(){ BufferedOutputStream bos=null;
try {
bos=new BufferedOutputStream(new FileOutputStream("./sss.txt")); bos.write("缓冲区字节流".getBytes()); } catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
输入流:
@Test
public void test11(){ BufferedInputStream bis=null;
try {
bis=new BufferedInputStream(new FileInputStream("./sss.txt")); byte[] buffer=new byte[1024];
int len=0;
while ((len=(bis.read(buffer)))!=-1){ System.out.println(new String(buffer,0,len)); } } catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (bis != null) {
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} }
												

java_I/O字节流的更多相关文章

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

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

  2. 字节流VS缓冲流

    ---恢复内容开始--- 字节流VS缓冲流 java.io包中的类大致可以分为:InputStream.OutputStream.Reader.Writer.InputStream/Reader可以理 ...

  3. java IO输入输出流中的各种字节流,字符流类

    字节流字节流主要是操作byte类型数据,也byte数组为准,主要操作类就是·字节输出流:OutputStream·字节输入流:InputStream字符流在程序中一个字符等于2个字节,那么java提供 ...

  4. Java字节流和字符流区别

    1.字节流:直接操作文件本身. 2.字符流:通过缓冲区来操作文件. 所有的文件在硬盘或在传输时都是以字节的方式进行的,包括图片等都是按字节的方式存储的,而字符是只有在内存中才会形成,所以在开发中,字节 ...

  5. 字节流与数据类型的相互转换---使用struct模块

    字节流与数据类型的相互转换---使用struct模块 http://blog.csdn.net/Sunboy_2050/article/details/5974029 Python是一门非常简洁的语言 ...

  6. java IO流 之 字节流

    一.file类的常用操作 File file=new File("E:\\test\\javaIo"); System.out.println(file.isDirectory() ...

  7. [Java IO]02_字节流

    概要 字节流有两个核心抽象类:InputStream 和 OutputStream.所有的字节流类都继承自这两个抽象类. InputStream 负责输入,OutputStream 负责输出. 字节流 ...

  8. Java IO之字节流

    Java中的输入是指从数据源等读到Java程序中,这里的数据源可以是文件,内存或网络连接,输出则是指从Java程序中写到目的地. 输入输出流可以分为以下几种类型(暂时不考虑File类) 类名 中文名 ...

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

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

随机推荐

  1. 23.Merge k Sorted Lists (Array, Queue; Sort)

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...

  2. Git忽略规则及.gitignore规则不生效的解决办法(转)

    在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如无,则需自己手工建立此文件).这个文件每一行保存了一个匹配的规则例如: # 此为注 ...

  3. hive分隔符总结

    ascii对应的表Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex ----------------- ...

  4. socket收发消息

    .socket通讯类 using System; using System.Collections.Generic; using System.Net; using System.Net.Socket ...

  5. [.NET] GC垃圾回收机制

    前言: 在.NET程序开发中,为了将开发人员从繁琐的内存管理中解脱出来,将更多的精力花费在业务逻辑上,CLR提供了自动执行垃圾回收的机制来进行内存管理.开发人员甚至感觉不到这一过程的存在.CLR执行垃 ...

  6. tpshop使用中遇到的问题

    1.短信配置里:商家发货时是否给客户发短信  配置了 开启   如果购买者个人资料里的电话没填写,商家点击发货时, 程序会挂掉 解决方法:修改application\common\logic\SmsL ...

  7. msfconsole邮件收集器模块

    msfconsole search email collector use auxiliary/gather/search_email_collector show options 下面我们设置域名. ...

  8. mongo find

    MongoVUE 对应成语句,结构如下: db.logs.find({ "message" : /消息/ }, { "message" : 1 }).limit ...

  9. [转]Newtonsoft.Json 序列化和反序列化 时间格式

    本文转自:http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeObj ...

  10. C++ 的Tool工具收集

    C++ 的Tool工具收集 1. muparser - Fast Math Parser Library 数学公式解析函数,开源工具库 网址: http://muparser.beltoforion. ...