FileOutputStream字节输出流和FileInputStream输入流(切记:out是输出到本地中,in是输入到程序中)这里介绍大文件和小文件的读取方式
//FileOutputStream
public class FileOutputStreamDemo {
/**字节流:适用于任何文件,以字节为单位,进行读写操作
*字节流操作步骤:
*1、创建文件对象
*2、创建字节流
*3、读写操作
*4、关闭流
*/
//字节流(写操作)
public static void main(String[] args) {
String messageString = "hello world";
byte[] bytes = messageString.getBytes();
//1、创建文件对象
File file = new File("abc.txt");
FileOutputStream fos = null;
try {
//2、创建字节流
fos = new FileOutputStream(file,true);//参数一:file对象(也可以是一个文件路径) 参数二:true表示追加模式 false表示覆盖模式
//3、写操作
fos.write(bytes);
System.out.println("写入成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
//4、关闭流
if(fos!=null){
fos.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//FileInputStream
public class FileInputStreamDemo {
//字节流(读小文件操作)
// public static void main(String[] args) {
// //1、创建文件对象
// File file = new File("abc.txt");
// FileInputStream fis = null;
// try {
// //2、创建字节流
// fis = new FileInputStream(file);
// byte[] bytes = new byte[(int)file.length()];//创建存储从文件读取到的字节数据
// //3、读操作
// fis.read(bytes);
// String str = new String(bytes);//将字节数组转换为字符串
// System.out.println(str);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }finally{
// try {
// //4、关闭流
// if(fis!=null){
// fis.close();
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
//字节流(读大文件操作)
public static void main(String[] args) {
File file = new File("abc.txt");
FileInputStream fis = null;
//一般byte数组的大小为512,,1024,2048
byte[] bytes = new byte[1024];//存储每次读取的字节数据
int len = 0;//存储每次读取的字节数
try {
fis = new FileInputStream(file);
while((len=fis.read(bytes))!=-1){
//参数一:字节数组 参数二:从数组的哪个下标开始转换为字符串 参数三:要转换的字节数
String str = new String(bytes,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(fis!=null){
fis.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
FileOutputStream字节输出流和FileInputStream输入流(切记:out是输出到本地中,in是输入到程序中)这里介绍大文件和小文件的读取方式的更多相关文章
- 序列化(ObjectOutputStream和ObjectInputStream)(切记:out是输出到本地中,in是输入到程序中)
注意:序列化自定义类必须实现一个接口Serializable,在c#中序列化自定义类是使用特性也就是[Serializable] //要实现序列化的类 public class Student imp ...
- DataInputStream(二进制输入流)和DataOutputStream二进制输出流(注意:in是从本地文件输入到程序中,out是从程序输出到本地种)
//切记以数据类型输出就以什么数据类型读入, //例如: dos.writeInt(100);写入,读取:dis.readUTF()有时会出现意想不到的错误,所以要时刻记得以数据类型输出就以什么数据类 ...
- 一切皆为字节和字节输出流_OutputStream类&FileOutputStream类介绍
一切皆为字节 一切文件数据(文本.图片.视频等)在存储时,都是以二进制数字的形式保存,都一个一个的字节,那么传输时一样如此.所以,字节流可以传输任意文件数据.在操作流的时候,我们要时刻明确,无论使用什 ...
- 编码占用的字节数 1 byte 8 bit 1 sh 1 bit 中文字符编码 2. 字符与编码在程序中的实现 变长编码 Unicode UTF-8 转换 在网络上传输 保存到磁盘上 bytes
小结: 1.UNICODE 字符集编码的标准有很多种,比如:UTF-8, UTF-7, UTF-16, UnicodeLittle, UnicodeBig 等: 2 服务器->网页 utf-8 ...
- Java基础 FileInputStream/ FileOutputStream / 字节输入流 字节输出流实现文件的复制
FileInputStream/FileOutputStream的笔记: /**(FileInputStream/FileOutputStream四个步骤: ①声明②加载地址③read/write④c ...
- java 20 - 9 带有缓冲区的字节输出流和字节输入流
由之前字节输入的两个方式,我们可以发现,通过定义数组读取数组的方式比一个个字节读取的方式快得多. 所以,java就专门提供了带有缓冲区的字节类: 缓冲区类(高效类) 写数据:BufferedOutpu ...
- 字节输出流FileOutputStream
#字节流 字节输出流FileOutputStream 创建输出流对象 OutputStream 流对象是一个抽象类,不能实例化.所以,我们要找一个具体的子类 :FileOutputStream. 查看 ...
- IO初步,字节输入流和字节输出流
字节输出流 OutputStream(基类,抽象) 特点:写任意的文件 方法:写出数据的方法:write write(int b) 写出1个字节 -128~127之间,写的是一个ASCLL码的值 wr ...
- JAVA笔记11__File类/File类作业/字节输出流、输入流/字符输出流、输入流/文件复制/转换流
/** * File类:文件的创建.删除.重命名.得到路径.创建时间等,是唯一与文件本身有关的操作类 */ public class Main { public static void main(St ...
随机推荐
- C#操作IIS完整解析
原文:C#操作IIS完整解析 最近在为公司实施做了一个工具,Silverlight部署早已是轻车熟路, 但对于非技术人员来说却很是头疼的一件事,当到现场实施碰到客户情况也各不相同, 急需一个类似系统备 ...
- Android项目打包成APK文件
第一步:右键单击该项目选择Export项目 显演示样例如以下界面:选择Exprot Android Application 第二步:输入项目名称,默认的情况下是原始的项目名称 下一步: 点击 Crea ...
- [DB][mybatis]MyBatis mapper文件引用变量#{}与${}差异
MyBatis mapper文件引用变量#{}与${}差异 默认,使用#{}语法,MyBatis会产生PreparedStatement中.而且安全的设置PreparedStatement參数,这个过 ...
- Node.js新手教程——怎样实现文件上传功能
作者:zhanhailiang 日期:2014-11-16 本文将介绍怎样使用Node.js实现文件上传功能. 1. 初始化项目信息:npm init [root@~/wade/nodejs/node ...
- Codeforces Round #266 (Div. 2)-C,D
C - Number of Ways 直接暴力从前往后寻找.假设找到1/3sum的位置,那么标记++.找到2/3的位置,总数加上标记数. #include<stdio.h> #includ ...
- warning: shared library text segment is not shareable
warning: shared library text segment is not shareable error: treating warnings as errors 近期在调试一个Gsen ...
- strip 使用命令
使用 通过消除使用调试器的粘合剂和符号信息,减少扩展公共对象文件格式(XCOFF)对象文件大小. 语法 strip [ -V ] [ -r [ -l ] | -x [ -l ] | -t | -H | ...
- CENTOS6.4安装lxml失败
环境如下: Centos6.4 Python 2.7.6 pip install lxml 执行上面的命令,有类似下面的提示: src/lxml/lxml.etree.c:188133: error: ...
- 再议 js 数字格式之正则表达式
原文:再议 js 数字格式之正则表达式 前面我们提到到了js的数字格式<浅谈 js 数字格式类型>,之前的<js 正则练习之语法高亮>里也提到了优化数字匹配的正则.不过最近落叶 ...
- 用css2属性clip实现网页进度条
前言 看了网上一些关于网页进度条样式的资料,有很多方式实现,针对其展现形式,有用图片的,有用css2属性clip,有用flash的,本人就学会了一种,下面就简单来介绍一下. css2的属性clip 如 ...