一:通过字节流操作数据的写入,读出

	/**
* 通过字节流写入和读出
* @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通过字节流,字符流 读出写入的更多相关文章

  1. java IO(三):字符流

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  2. IO 复习字节流字符流拷贝文件

    /* 本地文件 URL 文件拷贝 *//*文本文件拷贝 可以通过 字符流,也可以通过字节流*/ /*二进制文件拷贝 只可以通过字节流*//* 希望这个例子能帮助搞懂 字符流与字节流的区别 */ imp ...

  3. IO—》字节流&字符流

    字节流 一.字节输出流OutputStream OutputStream此抽象类,是表示输出字节流的所有类的超类.操作的数据都是字节,定义了输出字节流的基本共性功能方法. FileOutputStre ...

  4. IO字 节流/字符流 读取/写入文件

    流是指一连串流动的数据信号,以先进,先出的方式发送和接收的通道 流的分类根据方向分为输入流所有接收,获得,读取的操作都是属于输入流所有的输入流名字都带有input或Reader 输出流所有发送,写的操 ...

  5. Java——IO类,字符流写数据

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  6. Java——IO类,字符流读数据

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  7. 缓冲字符流 java.io.BufferedWriter ,java.io.BufferedReader,缓冲字符输出流:PrintWriter

    package seday07; import java.io.IOException;import java.io.PrintWriter; /*** @author xingsir * 缓冲字符流 ...

  8. -1-4 java io java流 常用流 分类 File类 文件 字节流 字符流 缓冲流 内存操作流 合并序列流

      File类 •文件和目录路径名的抽象表示形式 构造方法 •public File(String pathname) •public File(String parent,Stringchild) ...

  9. IO流(字节流,字符流,缓冲流)

    一:IO流的分类(组织架构) 根据处理数据类型的不同分为:字节流和字符流 根据数据流向不同分为:输入流和输出流   这么庞大的体系里面,常用的就那么几个,我们把它们抽取出来,如下图:   二:字符字节 ...

随机推荐

  1. Linux学习笔记 - Shell 函数的使用

    基本语法 funname () { action; return -)):如果不加,将以最后一条命令运行结果,作为返回值. } 示例1:定义并调用无返回值的函数 #!/bin/bash a= b= c ...

  2. wkhtmltopdf Windows下 测试demo 成功

    html2pdf 转pdf 中文不换行 然后找到了wkhtmltopdf 支持中文换行 样式也支持 在PHP中生成PDF文件,可以使用 FPDF 和 TCPDF .但是它们只能用于创建简单的表格,当涉 ...

  3. Linux系统级别能够打开的文件句柄的数file-max命令

    简单的说, max-file表示系统级别的能够打开的文件句柄的数量, 而ulimit -n控制进程级别能够打开的文件句柄的数量. man 5 proc, 找到file-max的解释:file-max中 ...

  4. Django 模型层(2)

    多表操作: 创建模型: 作者模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是一对一的关系(one-to-one) 出版商模型:出版商有名称,所在城市以及em ...

  5. python实现cifar10数据集的可视化

    在学习tensorflow的mnist和cifar实例的时候,官方文档给出的讲解都是一张张图片,直观清晰,当我们看到程序下载下来的数据的时候,宝宝都惊呆了,都是二进制文件,这些二进制文件还不小,用文本 ...

  6. day9-数据库操作与Paramiko模块

    堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: 1 ...

  7. 关于ie6中绝对定位或浮动的div中既有向左float也有向右float时候如何让外层div自适应宽度的解决方案--

    一个详细的说明请见: http://www.cnblogs.com/yiyang/p/3265006.html 我的问题大约为,如下代码: <!DOCTYPE html PUBLIC " ...

  8. 基于AT UI实现表格的增删改查遇到的坑

    基于AT UI实现表格的增删改查遇到的坑 坑一.表格数据加载的渲染报错 报错:Error in render: "TypeError: Cannot read property 'isChe ...

  9. ATL接口返回类型&&ATL接口返回字符串BSTR*

    感觉在ATL中做COM组件,添加方法的时候,其返回值只能是HRESULT,我想返回其他数据类型,可以吗? 非也非也 HRESULT指示返回的状态,即正确与否, 返回值是这样的 HRESULT MyMe ...

  10. ffmpeg源码分析一:概述 (转1)

    原帖地址:http://blog.csdn.net/austinblog/article/details/24800381 首先先看ffmpeg.c文件,有类似于如下的一些变量: InputStrea ...