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流的分类(组织架构) 根据处理数据类型的不同分为:字节流和字符流 根据数据流向不同分为:输入流和输出流 这么庞大的体系里面,常用的就那么几个,我们把它们抽取出来,如下图: 二:字符字节 ...
随机推荐
- linux 文件结构
Linux常见的目录解释: 目录 描述 / 根目录 /bin 做为基础系统所需要的最基础的命令就是放在这里.比如 ls.cp.mkdir等命令:功能和/usr/bin类似,这个目录中的文件都是可执行的 ...
- ListView下拉刷新,上拉自动加载更多
下拉刷新,Android中非常普遍的功能.为了方便便重写的ListView来实现下拉刷新,同时添加了上拉自动加载更多的功能.设计最初是参考开源中国的Android客户端源码.先看示例图. ...
- Oracle 10 Recycle Bin回收站
这个功能从10g开始有了. (1)什么是Recycle Bin实际上,Recycle Bin只是一个保存被drop的对象的一个数据字典表.所以,可以通过如下语句查询回收站中的信息:select * f ...
- NIO buffer 缓冲区 API
package bhz.nio.test; import java.nio.IntBuffer; public class TestBuffer { public static void main(S ...
- centos 6.3 kickstart 装机卡在 unsupported hardware detected 页面
起因 最近厂里一批 dell 新机器 centos 6.3 装机卡在 Unsupported Hardware Detected 页面,要人肉点击 OK 才能继续装机: Unsupported Har ...
- 关于data-属性
关于data-属性 现有需求如下,也就是类似做一个tab页的切换如下图: 因为这里要记录一下jquery里的“data-属性”的用法,所以忽略类似的组件. 往HTML标签上添加任意以 "da ...
- MyBatis 学习记录1 一个简单的demo
主题 最近(N个月前)clone了mybatis的源码..感觉相比于spring真的非常小...然后看了看代码觉得写得很精简...感觉我的写代码思路和这个框架比较相似(很难具体描述...就是相对来说比 ...
- C++防止文件重复包含
引用自:https://blog.csdn.net/xhfight/article/details/51550446 为了避免同一个文件被include多次,C/C++中有两种方式,一种是#ifnde ...
- Mysql 源码编译安装 ( 5.5 、5.6 共存 )
简介: 如何在一台服务器同时运行两 ( 多 ) 个 MySQL 服务. 1.MySQL 5.6 shell > useradd -r -s /sbin/nologin mysql shell & ...
- Log4j编写
来自: http://www.blogjava.net/zJun/archive/2006/06/28/55511.html Log4J的配置文件(Configuration File)就是用来设置记 ...