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流的分类(组织架构) 根据处理数据类型的不同分为:字节流和字符流 根据数据流向不同分为:输入流和输出流 这么庞大的体系里面,常用的就那么几个,我们把它们抽取出来,如下图: 二:字符字节 ...
随机推荐
- 建立SIP通话
建立SIP: 点击下的出现的页面: 选择submit,只用填写用户名和密码就OK了,secret是密码,填写完以后记得应用 创建完毕以后,使用xlite去连接:xlite的配置:域名是asterisk ...
- VS2015 异常 :遇到异常。这可能是由某个扩展导致的
原因是安装程序时将注册表修改了,解决方案: 修改注册表: 64位机器: [HKEY_CLASSES_ROOT\CLSID\{73B7DC00-F498-4ABD-AB79-D07AFD52F395}\ ...
- apache commons lang包中的StringUtils
计算一个字符串某个字符的出现次数 a, 使用charAt方法截取之后,循环判断. b, 使用apache commons lang包中的StringUtils: int n = StringUtils ...
- sql语句中避免使用mysql函数,提升mysql处理能力。
如下sql中不要用mysql内置函数now()等,这样第一可以提高sql执行效率,第二统一程序层处理sql中时间参数,避免因服务器时间差导致问题产生. 使用PDO预处理,第一可以提高sql效率,第二可 ...
- Warning: require(): open_basedir restriction in effect. File(/www/wwwroot/../thinkphp/start.php) is not within the allowed path(s):
Warning: require(): open_basedir restriction in effect. File(/www/wwwroot//../thinkphp/start.php) is ...
- python中django的第一课
今天找遍了,PYTHON项目中的文件,也没发现后台管理的这些页面去哪里修改 原来在pycharm中的外部库中,查找django,最终找到 $python\django\django\contrib\a ...
- 版本控制git之一 - 仓库管理
git 再开始这个话题之前,让我想起了一件很痛苦的事情,在我大学写毕业论文的时候,我当时的文件是这样保存的 毕业论文_初稿.doc 毕业论文_修改1.doc 毕业论文_修改2.doc 毕业论文_修改3 ...
- 【Oracle】Oracle数据库DATABASE LINK 的命名
当前数据库的GLOBAL_NAMES参数设置为 TRUE,使用DATABASE LINK 时,DATABASE LINK的名称必须与被连接库的 GLOBAL_NAME一致. 而要建多个 DBLINK到 ...
- Rhythmk 一步一步学 JAVA(11)Ibatis 环境配置
1.项目文件分布. 2.example1.java: package com.rhythmk.example1; import java.io.IOException; import java.io. ...
- C#中StreamReader读取中文出现乱码
转自yhrun原文C#中StreamReader读取中文出现乱码 原因是自Windows 2000之后的操作系统在文件处理时默认编码采用Unicode 所以.NET文件的默认编码也是Unicode.除 ...