java IO通过字节流,字符流 读出写入
一:通过字节流操作数据的写入,读出
/**
* 通过字节流写入和读出
* @param args
*/
public static String filePath = "G:" + File.separator + "JAVA"
+ File.separator + "test" + File.separator + "aaa.txt"; public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
wirteInByte();
wirteInByteByByte();
wirteInByteAppend(); readFileExitSpace();
readFileNoSpace();
getLengthOfFile();
readByByte();
readToEndofFile(); } /*
* 0. 获取写入文件的两种方法
*/
public static void getFileMethod() throws FileNotFoundException {
File file = new File(filePath);
OutputStream os = new FileOutputStream(file); OutputStream os2 = new FileOutputStream(filePath);
} /*
* 1.向文件中写入整个字节数组
*/
public static void wirteInByte() throws IOException {
//
File file = new File(filePath);
if (!file.exists())
file.createNewFile();
OutputStream os = new FileOutputStream(file); // outputstream
// 是fileoutputstream的抽象类
// 也可直接用 OutputStream os=nmew FileOutputStream(filePath)
String s = "nihaoma";
byte[] myByte = s.getBytes();
os.write(myByte);
os.close();
} /*
* 2.向文件中一个字节一个字节的写入文件
*/
public static void wirteInByteByByte() throws IOException {
File file = new File(filePath);
if (!file.exists())
file.createNewFile(); OutputStream os = new FileOutputStream(file); // outputstream
// 是fileoutputstream的抽象类
String s = "还是键盘敲代码爽啊";
byte[] myByte = s.getBytes();
for (int i = 0; i < myByte.length; i++) {
os.write(myByte[i]);
} os.close();
} /*
* 3.向文件追加文字
*/
public static void wirteInByteAppend() throws IOException {
File file = new File(filePath);
if (!file.exists())
file.createNewFile(); OutputStream os = new FileOutputStream(file, true); // outputstream
// 是fileoutputstream的抽象类
// FileOutputStream(File file, boolean append) 创建一个向指定 File
// 对象表示的文件中写入数据的文件输出流。 String s = "今天的天气有点坑爹啊,怎么办呢?";
byte[] myByte = s.getBytes();
os.write(myByte);
os.close();
} /*
* 4.读取文件的内容(输出会有空格)
*/
public static void readFileExitSpace() throws IOException {
File file = new File(filePath);
InputStream in = new FileInputStream(file); byte[] allByte = new byte[100];
in.read(allByte);
in.close();
System.out.println(new String(allByte)); // allByte.toString()只是将地址转化为10机制而已
// 这样读取时有很多空格的,因为在byte数组中,只有一部分有值,看下面的方法
} /*
* 5.读取文件的内容(输出会无空格)
*/
public static void readFileNoSpace() throws IOException {
File file = new File(filePath);
InputStream in = new FileInputStream(file); byte[] allByte = new byte[100];
int len = in.read(allByte); // 注意这里in.read 返回的是读取的长度
in.close();
System.out.println(new String(allByte, 0, len)); // 这样读取时有很多空格的,因为在byte数组中,只有一部分有值,看下面的方法 } /*
* 6.通过file.length 获取对象file的字节长度
*/
public static void getLengthOfFile() {
File file = new File(filePath);
long len = file.length();// 直接可以获取到file的字节长度(必须是问价类型)\
System.out.println("file 问价的字节长度是:" + len); } /*
* 7.一次读取一个字节
*/
public static void readByByte() throws IOException {
File file = new File(filePath);
InputStream in = new FileInputStream(file); // 获取file字节的大小
int len = (int) file.length();
byte[] allbyte = new byte[len];
for (int i = 0; i < len; i++) {
allbyte[i] = (byte) in.read();
}
in.close();
System.out.println(new String(allbyte));
} /*
* 8.不知道文件的字节大小,只能判断是否读到文件的结尾(提醒一下,当独到文件末尾的时候会返回-1.正常情况下是不会返回-1的)
*/
public static void readToEndofFile() throws IOException {
File file = new File(filePath);
InputStream in = new FileInputStream(file); byte[] all = new byte[1024];
int count = 0;
int temp =0;
while ((temp=in.read()) != (-1)) {
all[count++] = (byte)temp;
}
in.close();
System.out.println(new String(all, 0, count)); }
二:通过字符流控制字符的读出,写入
/**
* 字符流测试
* @param args
*/
public static String filePath="G:"+File.separator+"JAVA"+File.separator+"test"+File.separator+"bbb.txt";
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
readAll();
readByByte();
insertCharactor();
} /*
*1.从头开始读取所有的字符
*/
public static void readAll() throws IOException
{
File file=new File(filePath);
Reader reader=new FileReader(file);
int len=(int)file.length(); //获取文件 长度
char[] all=new char[len]; reader.read(all);
reader.close();
System.err.println(new String(all));
} /*
* 2.最好一个个读取,判断最后一个字符不是-1
*/
public static void readByByte() throws IOException
{
File file=new File(filePath);
Reader reader =new FileReader(file);
//声明 一个字符型数组
char[] all=new char[1024]; int count =0;
int temp=0;
//读取的到最后一个字符
while((temp=reader.read())!=(-1))
{
all[count++]=(char)temp; }
reader.close();
System.out.println(new String(all,0,count)); } /*
* 3.写操作,直接写入字符串(直接append )
*/
public static void insertCharactor() throws IOException
{
File file=new File(filePath);
Writer writer=new FileWriter(file); String s="晕啊";
writer.write(s);
writer.append("nimeimie");
writer.close();
} }
总结:字符流和字节流的区别和联系
实际上字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的,但是字符流在操作的 时候下后是会用到缓冲区的,是通过缓冲区来操作文件的。
读者可以试着将上面的字节流和字符流的程序的最后一行关闭文件的代码注释掉,然后运行程序看看。你就会发现使用字节流的话,文件中已经存在内容,但是使用字符流的时候,文件中还是没有内容的,这个时候就要刷新缓冲区。
使用字节流好还是字符流好呢?
答案是字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。
三、文件复制,字节流和字符流的相互转化
/*
*1.文件复制
*/
public static void copyFile() throws IOException
{
File fileSrc=new File(filePath);
File fileDes=new File("G:"+File.separator+"JAVA"+File.separator+"test"+File.separator+"copy.txt");
if(!fileDes.exists())
fileDes.createNewFile(); InputStream in=new FileInputStream(fileSrc);
OutputStream out=new FileOutputStream(fileDes); int count=0;
int temp=0;
while((temp=in.read())!=(-1))
{
out.write(temp);
}
in.close();
out.close(); } /*
* 2.将字节输入流装换成字符输入流,字节输入流转化为字符输入流
*/
public static void byteStreamChangeIntoCharStream() throws IOException
{
File file=new File(filePath);
//OutputStreamWriter 将字节流转化为字符流
Writer writer=new OutputStreamWriter(new FileOutputStream(file));
//通过InputStreamReader 将字节输入流转化成字符输入流
Reader reader =new InputStreamReader(new FileInputStream(file)); }
java IO通过字节流,字符流 读出写入的更多相关文章
- java IO(三):字符流
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- IO 复习字节流字符流拷贝文件
/* 本地文件 URL 文件拷贝 *//*文本文件拷贝 可以通过 字符流,也可以通过字节流*/ /*二进制文件拷贝 只可以通过字节流*//* 希望这个例子能帮助搞懂 字符流与字节流的区别 */ imp ...
- IO—》字节流&字符流
字节流 一.字节输出流OutputStream OutputStream此抽象类,是表示输出字节流的所有类的超类.操作的数据都是字节,定义了输出字节流的基本共性功能方法. FileOutputStre ...
- IO字 节流/字符流 读取/写入文件
流是指一连串流动的数据信号,以先进,先出的方式发送和接收的通道 流的分类根据方向分为输入流所有接收,获得,读取的操作都是属于输入流所有的输入流名字都带有input或Reader 输出流所有发送,写的操 ...
- Java——IO类,字符流写数据
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...
- Java——IO类,字符流读数据
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...
- 缓冲字符流 java.io.BufferedWriter ,java.io.BufferedReader,缓冲字符输出流:PrintWriter
package seday07; import java.io.IOException;import java.io.PrintWriter; /*** @author xingsir * 缓冲字符流 ...
- -1-4 java io java流 常用流 分类 File类 文件 字节流 字符流 缓冲流 内存操作流 合并序列流
File类 •文件和目录路径名的抽象表示形式 构造方法 •public File(String pathname) •public File(String parent,Stringchild) ...
- IO流(字节流,字符流,缓冲流)
一:IO流的分类(组织架构) 根据处理数据类型的不同分为:字节流和字符流 根据数据流向不同分为:输入流和输出流 这么庞大的体系里面,常用的就那么几个,我们把它们抽取出来,如下图: 二:字符字节 ...
随机推荐
- PHP中实现函数重载
转载自:http://cnn237111.blog.51cto.com/2359144/1284085 由于PHP是弱类型语言,因此函数的输入参数类型无法确定(可以使用类型暗示,但是类型暗示无法用在诸 ...
- Codeforces-417D总结&题解
在分析题目前,一定要完全读懂题目意思,否则一些都是白搭.这道理谁都懂,但是能每次都做到的人不多.比如本人,这次又粗心地在这里疯狂地踩坑了. (1)本题有这么一句话:But Gena's friends ...
- Centos7.4 版本环境下安装Mysql5.7操作记录
Centos7.x版本下针对Mysql的安装和使用多少跟之前的Centos6之前版本有所不同的,废话就不多赘述了,下面介绍下在centos7.x环境里安装mysql5.7的几种方法: 一.yum方式安 ...
- HTML5的离线存储有几种方式?
localStorage长期存储数据,浏览器关闭后数据不丢失: sessionStorage数据在浏览器关闭后自动删除.
- pvalue for go kegg enrichment
Simple, fast implementation of Fisher’s exact test. . For example, for the following table: o Havi ...
- 【Oracle】安装Oracle 10gR2 For CentOS
Oracle10gR2安装安装环境项目 版本信息 备注操作系统 CentOS5.364bit Oracle数据库 Oracle10.2.0.4 64bit 硬件信 ...
- Spring之导入和混合配置
在典型的Spring应用中,我们可能会同时使用自动化和显式配置.即便你更喜欢通过JavaConfig实现显式配置,但有的时候XML却是最佳的方案.幸好在Spring中,这些配置方案都不是互斥的.你尽可 ...
- HTTP 状态信息
一.1xx 消息 该类型的状态码代表请求已被接受,需要继续处理. 100 Continue 客户端应当继续发送请求,这个临时响应是用来通知客户端的部分请求已经被服务器接收,且仍未被拒绝.客户端应当继续 ...
- Linux服务管理总结
简介与分类 系统的运行级别 运行级别 含义 0 关机 1 单用户模式,可以想象为windows的安全模式,主要用于系统修复 2 不完全的命令行模式,不含NFS服务 3 完全的命令行模式,就是标准字符界 ...
- 『PLSQL』在oracle表中怎样创建自增长字段?
1.建立测试数据表CREATE TABLE TEST( ID NUMBER, NAME VARCHAR2(20), PRIMARY KEY(ID)); 2.创建序列CREATE SEQUENCE SE ...