java study文件读写
文件读写
如果在代码中写入大量的数据,会增加代码的冗余度,通过读取文件的方式,可以精简代码,便于数据的修改和代码的维护
IO流的分类:字节流和字符流
字符流
- 字符输出流:写文本文件的,抽象基类java.io.Writer。写的方法write,很多重载形式,写字符数组、单个字符、字符串、字符串组一部分、字符串的一部分,flush数据流的缓冲,close关闭对象。
- 字符输入流:读取文本文件的,抽象基类java.io.Reader。读的方法read,很多重载形式,读取单个字符、字符数组、字符数组的一部分。close关闭流对象。
字节流
- 字节输出流:写任意的文件,抽象基类java.io.OutputStream。写的方法write,很多重载形式,写单个字节,字符数组,字节数组的一部分,close关闭流对象。
- 字节输入流:读取任意文件,抽象基类java.io.InputStream。读的方法read,很多重载形式,读取单个字节,字符数组,字节数组的一部分,close关闭流对象。
IO六种的所有类的命名法则:后缀都是父类名,前面的都是可以操作的文件名,流向名。
FileinputStream FileReader ObjectInputStream ObjectOutputStream
#java
package study1;
import org.testng.annotations.Test;
import java.io.*;
public class IODemo {
@org.junit.Test
public void test() throws IOException {
//如果文件不存在,会自动创建
FileWriter fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt"));
fw.write("hello world\n");
fw.write("123\n");
/**
* 写入文件后,未执行其他操作,写入的内容不会保存
* 写入文件后,close流,会自动保存文件。
* 写入数据较多是,使用write写入后,使用flush保存写入的内容,降低对系统压力
*/
fw.flush();
fw.write(123);//此处写入的是ascii码值
fw.flush();
fw.close();//释放流资源
}
@org.junit.Test
public void test2() {
FileWriter fw= null ;
try{
fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt"));
}catch (IOException e){
e.printStackTrace();
}finally{
try{
fw.close();//此处释放流资源,必须要先初始化
}catch (IOException e){
e.printStackTrace();
}
}
}
@org.junit.Test
public void append() throws IOException{
//追加内容,而非覆盖
FileWriter fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt"),true);
fw.write("hello python\n");
fw.flush();
fw.close();
}
@org.junit.Test
public void test_read() throws IOException{
//读取文件
FileWriter fw = null;
fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt"));
fw.write("abcd");
fw.flush();
fw.close();
FileReader fr = null;
fr = new FileReader(new File("/Users/chenshanju/Desktop/data/a.txt"));
System.out.println(fr.read());//输出字符的ASCII码值,每执行一次就读取1次
System.out.println((char)fr.read());
System.out.println(fr.read());
System.out.println(fr.read());
System.out.println(fr.read());
System.out.println(fr.read());//没有内容,输出-1
fr.close();
}
@org.junit.Test
public void test_read2() throws IOException{
//读取单个字符
FileWriter fw = null;
fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt"));
fw.write("abcd");
fw.flush();
fw.close();
FileReader fr = null;
fr = new FileReader(new File("/Users/chenshanju/Desktop/data/a.txt"));
int i=0;
while((i= fr.read())!=-1){
/**
* 获取字符的ascii码值,并根据ascii码值强转为字符
*/
System.out.println((char)i);
}
fr.close();
}
@org.junit.Test
public void test_read3() throws IOException{
//读取字符数组,一次读取2个字符
FileWriter fw = new FileWriter(new File("Data/b.txt"));
fw.write("java");
char [] cha = {'a','b','c'};
fw.write(cha);
fw.close();
FileReader fr = new FileReader(new File("Data/b.txt"));
int i = 0;
char [] ch = new char[2];//一次读取2个字符
while((i=fr.read(ch))!=-1){
System.out.print(i+"\t");
System.out.println(ch);
/**
* System.out.println(i+"\t"+new String(ch));
* 和int类型同时使用,不加new String,会返回哈希码[C@6a6824be
* 加上new String和上面2行输出结果一致
* 2 ja
* 2 va
* 2 ab
* 1 cb 最后1次只装入了1个字符,只将a替换为c,b并未替换
*/
}
}
@org.junit.Test
//单个字符的写入
public void test_cp() throws IOException{
FileReader fr = null;
FileWriter fw = null;
fr = new FileReader(new File("Data/b.txt"));
fw = new FileWriter(new File("Data/b1.txt"));
int i = 0;
while((i=fr.read())!=-1){
fw.write((char)i);
}
fw.close();
fr.close();
}
@org.junit.Test
//字符数组的写入
public void test_cp1() throws IOException{
FileReader fr = null;
FileWriter fw = null;
fr = new FileReader(new File("Data/b.txt"));
fw = new FileWriter(new File("Data/b2.txt"));
char [] ch = new char[2];
int i = 0;
while((i=fr.read(ch))!=-1){
fw.write(ch,0,i);
}
fw.close();
fr.close();
}
@org.junit.Test
public void test_out() throws IOException{
//字节输出流
FileOutputStream out = null;
out = new FileOutputStream(new File("Data/c.txt"));
out.write("abcdefgh".getBytes());
out.close();
}
@org.junit.Test
public void test_in() throws IOException{
//字节输入流
FileInputStream in = null;
in = new FileInputStream(new File("Data/c.txt"));
int i = 0;
while((i=in.read())!=-1){
System.out.println((char)i);
}
in.close();
}
@org.junit.Test
public void test_in_char() throws IOException{
FileInputStream in = new FileInputStream(new File("Data/c.txt"));
int i = 0;
byte [] bt = new byte[10];
while((i=in.read(bt))!=-1){
System.out.print(i+"\t"+new String(bt));
/**
* System.out.println(bt);返回哈希码[B@6a6824be
* 长度不满10位,会用空位补齐
*/
}
}
@org.junit.Test
public void test_IO() throws IOException{
//将某网页的源码保存为文件
FileInputStream in = new FileInputStream(new File("Data/d.html"));
FileOutputStream out = new FileOutputStream(new File("Data/d1.html"));
int i = 0;
byte [] bt = new byte[1024];
while((i=in.read(bt))!=-1){
out.write(bt,0,i);
}
out.close();
in.close();
System.out.println("success");
}
}
java study文件读写的更多相关文章
- 沉淀再出发:java的文件读写
沉淀再出发:java的文件读写 一.前言 对于java的文件读写是我们必须使用的一项基本技能,因此了解其中的原理,字节流和字符流的本质有着重要的意义. 二.java中的I/O操作 2.1.文件读写的本 ...
- Java的文件读写操作
file(内存)----输入流---->[程序]----输出流---->file(内存) 当我们读写文本文件的时候,采用Reader是非常方便的,比如FileReader,InputStr ...
- Java的文件读写操作 <转>
目录: file内存----输入流----程序----输出流----file内存 java中多种方式读文件 判断文件是否存在不存在创建文件 判断文件夹是否存在不存在创建文件夹 java 写文件的三种方 ...
- [转]Java的文件读写操作
file(内存)----输入流---->[程序]----输出流---->file(内存) 当我们读写文本文件的时候,采用Reader是非常方便的,比如FileReader,InputStr ...
- java 实现文件读写操作
import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /* JAVA IO 读写操作 20 ...
- java大文件读写操作,java nio 之MappedByteBuffer,高效文件/内存映射
java处理大文件,一般用BufferedReader,BufferedInputStream这类带缓冲的Io类,不过如果文件超大的话,更快的方式是采用MappedByteBuffer. Mapped ...
- java IO文件读写例子(OutputStream,InputStream,Writer,Reader)
一,File创建文件 File file = new File("D:" + File.separator + "yi.txt"); 代码示例: package ...
- java写文件读写操作(IO流,字符流)
package copyfile; import java.io.*; public class copy { public static void main(String[] args) throw ...
- java写文件读写操作(IO流,字节流)
package copyfile; import java.io.*; public class copy { public static void main(String[] args) throw ...
随机推荐
- vsftp服务器同步文件
首先vsftp同步文件,并没有好的解决方案,网上有一些软件是可以定时同步文件的,我是用的是linux上的一款工具Rsync 首先先了解一下Rsync与scp的区别:点我 1.安装Rsync 两种安装方 ...
- L212
Just 33 minutes into the New Year, NASA's New Horizons probe made space exploration history, flying ...
- grafana查询中的变量templating
有时我们在管理成百上千台机器的时候,配置grafana无疑是明智的,因为你不需要一个一个的把每个机器的图形都配置一遍,利用templating就可以瞬间实现n台机器的状态显示了. templating ...
- Windows 7 SP1 多国语言包(MUI language packs)官方下载
为了装一款 Gal Game (为毛不是装 H-Game?),使用 APP 还是太辛苦了,反正相信童鞋们也都用上“瘟妻”了嘛,装个日文的语言包基本上就可以解决问题了.大家都知道只有旗舰版(Wind ...
- 51Nod:1134 最长递增子序列
动态规划 修改隐藏话题 1134 最长递增子序列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递 ...
- (8)propetry装饰器
propetry是一个内置函数,用来将一个功能伪装成一个数据属性 property将一个方法伪装成一个数据属性class People: def __init__(self,name,height,w ...
- hdu 2063 二分图匹配
题意:一些女的和一些男的有好感,有好感的能一起坐过山车,问最多能组成多少对 hdu 11 页上少有的算法题,二分图匹配问题,匈牙利算法,对于每一个汉子,看和他有好感的妹子有没有配对了,没有配对过就可以 ...
- day40 python MySQL【四】 之 【索引】【视图】【触发器】【存储过程】【函数】
MySQL[四] 之 [索引][视图][触发器][存储过程][函数] 1.索引 索引相当于图书的目录,可以帮助用户快速的找到需要的内容. 数据库利用各种各样的快速定位技术,能够大大提高查询效率.特 ...
- day43 数据库学习egon的博客 约束
一 介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KEY ...
- Vault 0.10包含了web ui
Vault 是一个很不错的访问控制,secret api key 管理工具 新的0.10 有好多新的功能的添加,最棒的是有一个web ui 了 包含的新特性如下: K/V Secrets Engine ...