当前位置: 首页 > news >正文

io流 java代码_java IO流 对文件操作的代码集合

Io流

按照分类 有两种分类

流向方向: 有输入流和输出流

按照操作类型有:字节流和字符流

按照流向方向

字节流的一些操作//读文件

FileInputStream fis = new FileInputStream("java.txt");int temp = fis.read()//一次读取一个字节

System.out.println(temp); //打印的字母的码值 读取完返回-1

System.out.println((char)temp);//打印字母

byte[] arr = new byte[6]; //定义byte数组告诉系统一次读取几个字节,减少内存和硬盘之间的通信,可以提高效率

int temp = bis.read(arr); //有参的read方法返回的int值是读取了几个字节

System.out.println(new String(arr, 0, temp)); //

//写文件

FileOutputStream fos = new FileOutputStream("file" + File.separator + "1024.txt",true);//如果该文件不存在,则会自动创建//传入true会在文件内容的后面写入文字,而不会覆盖之前的内容//开发中文件分隔符最好不要直接写\ 而是写 File.separator

String msg= "Hello World";

fos.write("\n".getBytes());//换行,并向文件写入 String.getBytes() 因为要以字节的形式传入

fos.write(msg.getBytes());

String msg= "好好学习";//一个汉字占2个字节,向里面一次传入3个字节会导致乱码

fos.write(msg.getBytes(), 0, 3);byte[] arr = new byte[6];//一次性写这么多字节

int temp =fis.read(arr);

fos.write(arr,0, temp);

fos.flush();//刷新//新的jdk7写法是在try括号()里面写文件的链接, 这样最后就不用关闭了,会自动关闭//缓冲输入流底层默认创建一个大小是8192长度的byte数组

BufferedInputStream bis = new BufferedInputStream(new FileInputStream("java.txt"));

BufferedOutputStream bos= new BufferedOutputStream(new FileOutputStream("file" + File.separator + "good.txt"));int temp = bis.read();//temp依然为ascii玛 每次一个

一些练习

利用BufferedInputStream 和 BufferedOutputStream 实现将一个文件copy到另一个文件

packagecom.wpbxx.stream;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;public classBufferFileCopy {public static voidmain(String[] args) {

BufferedInputStream bis= null;

BufferedOutputStream bos= null;try{

bis= new BufferedInputStream(new FileInputStream("java.txt"));

bos= new BufferedOutputStream(new FileOutputStream("file" + File.separator + "good.txt"));inttemp;while((temp = bis.read()) != -1){

bos.write(temp);

}

bos.flush();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{try{

bis.close();

bos.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

}

文件的加密  将一个字节异或一个数字实现 在传输时进行文件的加密

packagecom.wpbxx.stream;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;public classCodeFile {public static voidmain(String[] args) {//jdk7新写法

try(

BufferedInputStream bis= new BufferedInputStream(new FileInputStream("图片.png"));

BufferedOutputStream bos= new BufferedOutputStream(new FileOutputStream("code.png"));

) {inttemp;while((temp = bis.read()) != -1){

bos.write(temp^ 88);

}

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

}

}

文件解密

packagecom.wpbxx.stream;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;public classDecodeFile {public static voidmain(String[] args) {try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("code.png"));

BufferedOutputStream bos= new BufferedOutputStream(new FileOutputStream("decode.png"));) {inttemp;while ((temp = bis.read()) != -1) {

bos.write(temp^ 88);

}

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

}

}

字符流的一些操作  可以解决乱码的问题

//读//注意:字符流不能读取非文本文件

FileReader fr = new FileReader("java.txt");inttemp;while ((temp = fr.read()) != -1) {

System.out.println((char) temp); //一次一个字符

}//使用缓冲字符流

BufferedReader br = new BufferedReader(new FileReader("word.txt"));

String msg;while((msg = br.readLine()) != null){ //一次可以读取一行

System.out.println(msg);

}//写

FileWriter fw = new FileWriter("word.txt");

fw.write("我喜欢学习java");

fw.write(97);

BufferedWriter bw= new BufferedWriter(new FileWriter("newbuffered.txt"));

bw.write("你好");

bw.newLine();//回车换行

bw.write("java");

同样是copy文件

packagecom.wpbxx.chario;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;/*** 使用缓冲流拷贝文件

* 注意:字符流不能读取非文本文件*/

public classBufferFileCopy {public static voidmain(String[] args) {try (BufferedReader br = new BufferedReader(new FileReader("java.txt"));

BufferedWriter bw= new BufferedWriter(new FileWriter("file" + File.separator + "hellojava.txt"));

) {

String msg;while((msg = br.readLine()) != null){

bw.write(msg);

bw.newLine();

}

bw.flush();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e1) {

e1.printStackTrace();

}

}

}

通过java中的File类实现对文件的一些操作

File file1 = new File("D:\\hello.txt");//如果文件存在,就不创建了,返回false,如果不存在就会创建,返回true

System.out.println(file1.createNewFile());

File file2= new File("D:\\new");//如果文件夹存在,就不创建了,返回false,如果不存在就会创建,返回true

System.out.println(file2.mkdir());

File file3= new File("D:\\wpbxx\\1024");//可以创建多级目录,如果文件夹存在,就不创建了,返回false,如果不存在就会创建,返回true

System.out.println(file3.mkdirs());

File file4= new File("D:\\wpbxx\\1024.txt");//只能创建文件夹

System.out.println(file4.mkdirs());

File file5= new File("1026.txt");//如果不写盘符,会默认在项目的根目录里面创建

System.out.println(file5.createNewFile());

System.out.println(file5.exists());//旧名字

File oldFile1 = new File("D:\\world.txt");//新名字

File newFile1 = new File("D:\\wpbxx\\java.txt");//如果两个文件路径不一致,则会将旧文件剪切到新的文件路径中再重命名

oldFile1.renameTo(newFile1);//不会将文件放到回收站中,而是直接删除

File del = new File("D:\\wpbxx\\java.txt");

File del1= new File("D:\\wpbxx");//如果文件夹下有其他文件,则不会删除

System.out.println(del1.delete());

File file2= new File("D:\\new.txt");//判断是否是文件夹

System.out.println(file2.isDirectory());//判断是否是文件

System.out.println(file2.isFile());//判断文件是否存在

System.out.println(file2.exists());

File file3= new File("D:\\hidden");//判断文件是否隐藏

System.out.println(file3.isHidden());

File file1= new File("1024.txt");//查看绝对路径

System.out.println(file1.getAbsolutePath());//文件的大小,单位是字节

System.out.println(file1.length());//最后修改时间

Date date = newDate(file1.lastModified());

SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");

System.out.println(sdf.format(date));

File file2= new File("F:\\wpbxx\\代码\\code\\chapter-08");//获取目录下的同级文件或文件夹的名称

String[] nameArray =file2.list();for(String name : nameArray){

System.out.println(name);

}

几个小练习

packagecom.wpbxx.exercise;importjava.io.File;importjava.util.Scanner;/*** 问题:从键盘接收一个路径,将这个路径下的所有文件和文件夹的名字按照层级打印。

* 例如:

* wpbxx

* java

* XXX.java

* XXX.jpg

* php

* XXX.php

* readme.txt

*

* 分析:获取路径File对象中的File数组

* 遍历数组,取得File对象

* 打印文件或文件夹的名字

* 如果是一个文件夹的话,使用递归重复上面的操作*/

public classFileNames {//用来记录缩进的次数

private static int count = 0;public static voidmain(String[] args) {

File file=getFile();

getFileNames(file);

}//每次调用该方法时,说明进入到一个新的文件夹的内部,需要增加一个缩进

private static voidgetFileNames(File file) {//获取路径File对象中的File数组

File[] fileArray =file.listFiles();//遍历数组,取得File对象

for(int i=0; i

for(int j=0; j

System.out.print("\t");

}//打印文件或文件夹的名字

System.out.println(fileArray[i]);//如果是一个文件夹的话,使用递归重复上面的操作

if(fileArray[i].isDirectory()){

count++;

getFileNames(fileArray[i]);//数组遍历完最后一个File对象时,说明当前文件夹已经遍历结束,需要做自减运算

count--;

}

}

}//获取用户输入路径的File对象

private staticFile getFile() {

System.out.println("请输入一个文件夹路径:");

Scanner sc= newScanner(System.in);//获取用户输入的路径,用户输入的路径有可能是错误的,需要进行判断

while(true){

String input=sc.nextLine();

File file= newFile(input);if(!file.exists()){

System.out.println("您输入的文件路径有误,请重新输入文件路径:");

}else if(file.isFile()){//如果用户输入的路径是一个文件

System.out.println("您输入的路径是一个文件,请输入一个文件夹的路径");

}else{returnfile;

}

}

}

}

packagecom.wpbxx.exercise;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;/*** 问题:收费版软件有试用次数,利用IO流的知识,模拟一个可以试用3次的功能,打开3次之后提示用户购买正版软件

*

* 分析:将试用的次数做加密处理后写到txt文件中

* 使用IO流相关的知识将txt文件中的内容读取到内存中

* 如果读取的内容小于0时提示用户购买正版软件

* 如果大于0小于等于3时,将试用次数做自减运算之后写出到txt文件中*/

public classTrial {public static voidmain(String[] args) {//code();

BufferedInputStream bis = null;

BufferedOutputStream bos= null;try{

bis= new BufferedInputStream(new FileInputStream("src" + File.separator + "com"

+ File.separator + "monkey1024" + File.separator + "exercise" + File.separator + "config.txt"));int temp =bis.read();//解密处理

int count = temp ^ 66;if(count > 0 && count <= 3){

count--;

System.out.println("您的试用次数还剩余" + count + "次");

bos= new BufferedOutputStream(new FileOutputStream("src" + File.separator + "com"

+ File.separator + "monkey1024" + File.separator + "exercise" + File.separator + "config.txt"));//做加密处理

bos.write(count ^ 66);

bos.flush();

}else{

System.out.println("您的试用次数已超出限制,请购买正版软件!");

}

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{try{//避免出现空指针

if(bis != null){

bis.close();

}if(bos != null){

bos.close();

}

}catch(IOException e) {

e.printStackTrace();

}

}

}//试用次数加密处理

private static voidcode() {

BufferedOutputStream bos= null;try{

bos= new BufferedOutputStream(new FileOutputStream("src" + File.separator + "com"

+ File.separator + "monkey1024" + File.separator + "exercise" + File.separator + "config.txt"));//加密处理

bos.write(3 ^ 66);

bos.flush();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{try{//避免出现空指针异常

if(bos != null){

bos.close();

}

}catch(IOException e) {

e.printStackTrace();

}

}

}

}

packagecom.wpbxx.exercise;importjava.io.File;/*** 问题:统计项目根目录下以.txt结尾的文件数量,并将文件名打印出来

* 分析:获取项目根目录下的文件名

* 对文件名进行判断是否是以.txt结尾*/

public classFindTxt {public static voidmain(String[] args) {

File file= new File("F:\\wpbxx\\01-JavaSE\\代码\\code\\chapter-08");

File[] fileArray= file.listFiles(); //返回一个File列表就是该目录下的File列表//统计出现次数

int count = 0;for(File name : fileArray){

String s=name.toString();//判断是否是以.txt文件结尾

if(s.endsWith(".txt")){if(name.isFile()){

count++;

System.out.println(name);

}

}

}

System.out.println("以.txt文件结尾的数量是" + count + "个");

}

}

packagecom.monkey1024.file;importjava.io.File;importjava.io.FilenameFilter;/*** 问题:统计项目根目录下以.txt结尾的文件数量,并将文件名打印出来

* 使用文件过滤器实现上述需求*/

public classFilenameFilterTest01 {public static voidmain(String[] args) {

File file= new File("F:\\monkey1024\\01-JavaSE\\代码\\code\\chapter-08");

String[] nameArray= file.list(newFilenameFilter() {

@Overridepublic booleanaccept(File dir, String name){//获取根目录下每个文件的File对象

File file1 = newFile(dir, name);//编写筛选条件

return file1.isFile() && file1.getName().endsWith(".txt");

}

});

System.out.println("以.txt结尾的文件个数是" + nameArray.length + "个");for(String name : nameArray){

System.out.println(name);

}

}

}

packagecom.wpbxx.file;importjava.io.File;importjava.io.FilenameFilter;/*** 问题:统计项目根目录下以.txt结尾的文件数量,并将文件名打印出来

* 使用文件过滤器实现上述需求*/

public classFilenameFilterTest01 {public static voidmain(String[] args) {

File file= new File("F:\\wpbxx\\01-JavaSE\\代码\\code\\chapter-08");

String[] nameArray= file.list(new FilenameFilter() {//重写accept方法

@Overridepublic boolean accept(File dir, String name){//将过滤的规则写进来//获取根目录下每个文件的File对象

File file1 = newFile(dir, name);//编写筛选条件

return file1.isFile() && file1.getName().endsWith(".txt");

}

});

System.out.println("以.txt结尾的文件个数是" + nameArray.length + "个");for(String name : nameArray){

System.out.println(name);

}

}

}

对对象的读取

为啥要对对象读取?

平时我们在Java内存中的对象,是无 法进行IO操作或者网络通信的,因为在进行IO操作或者网络通信的时候,人家根本不知道内存中的对象是个什么东西,因此必须将对象以某种方式表示出来,即 存储对象中的状态。一个Java对象的表示有各种各样的方式,Java本身也提供给了用户一种表示对象的方式,那就是序列化。换句话说,序列化只是表示对 象的一种方式而已。OK,有了序列化,那么必然有反序列化,我们先看一下序列化、反序列化是什么意思。

序列化:将一个对象转换成一串二进制表示的字节数组,通过保存或转移这些字节数据来达到持久化的目的。

反序列化:将字节数组重新构造成对象。

序列化只需要实现java.io.Serializable接口就可以了。序列化的时候有一个serialVersionUID参数,Java序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。 在进行反序列化,Java虚拟机会把传过来的字节流中的serialVersionUID和本地相应实体类的serialVersionUID进行比较, 如果相同就认为是一致的实体类,可以进行反序列化,否则Java虚拟机会拒绝对这个实体类进行反序列化并抛出异常。serialVersionUID有两 种生成方式:

摘自:https://www.cnblogs.com/szlbm/p/5504166.html  这个也比较详细

如果一个类的对象支持序列化和反序列化,需要实现Serializable,Serializable中没有任何方法,只是相当于一个标记

有一个类

packagecom.monkey1024.serializable;importjava.io.Serializable;/*** 如果一个类的对象支持序列化和反序列化,需要实现Serializable

* Serializable中没有任何方法*/

public class Student implementsSerializable{/*** 自动生成序列化版本号*/

private static final long serialVersionUID = -716323668524282676L;privateString name;//添加属性后,使用反序列化时会报出InvalidClassException//transient修饰的变量不会被序列化

transient private intage;private booleansex;public booleanisSex() {returnsex;

}public void setSex(booleansex) {this.sex =sex;

}public intgetAge() {returnage;

}public void setAge(intage) {this.age =age;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}

}

然后我们对这个类进行序列化和反序列化

Student zhangsan = newStudent();

zhangsan.setName("张三");

zhangsan.setAge(20);//写

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("zhangsan"));

oos.writeObject(zhangsan);

oos.flush();//读

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("zhangsan"));

Student s=(Student)ois.readObject();

System.out.println(s.getName());

System.out.println(s.getAge());

相关文章:

  • java中重写的作用_Java中的重写
  • java def_java 8 default关键字
  • java url xml_从Java中的URL读取XML字符串
  • java 8 关键字_JAVA 8 '::' 关键字
  • java类中静态块_java – 为什么.class不调用类中的静态块?
  • java referrer_JavaScript中document.referrer的用法详解
  • java代码压缩文件_使用Java代码压缩文件或文件夹
  • 石子合并 动态规划 java_动态规划:圆形石子合并问题
  • java修饰方法_Java 修饰符
  • arduino timer频率_Arduino利用TimerOne库产生固定频率和占空比的方波
  • flask数据库mysql增删查改_flask_sqlalchemy简单增删查改操作
  • java基础语法的意义_关于java基础语法的学习笔记
  • java中容器试题_Java最常见208道面试题_容器
  • java 开启线程扫描程序_当多个线程在Java中使用System.in上的扫描仪...
  • java中结构体实现_JAVA中如何实现C中的结构体数组的功能?
  • [PHP内核探索]PHP中的哈希表
  • 5、React组件事件详解
  • es6(二):字符串的扩展
  • ES6简单总结(搭配简单的讲解和小案例)
  • jquery ajax学习笔记
  • js继承的实现方法
  • node入门
  • Otto开发初探——微服务依赖管理新利器
  • swift基础之_对象 实例方法 对象方法。
  • Webpack 4x 之路 ( 四 )
  • 动手做个聊天室,前端工程师百无聊赖的人生
  • 聚类分析——Kmeans
  • 前端学习笔记之观察者模式
  • 区块链将重新定义世界
  • 使用putty远程连接linux
  • 文本多行溢出显示...之最后一行不到行尾的解决
  • 译有关态射的一切
  • 应用生命周期终极 DevOps 工具包
  • 【干货分享】dos命令大全
  • 好程序员web前端教程分享CSS不同元素margin的计算 ...
  • 摩拜创始人胡玮炜也彻底离开了,共享单车行业还有未来吗? ...
  • ​linux启动进程的方式
  • #mysql 8.0 踩坑日记
  • $.each()与$(selector).each()
  • (1)Nginx简介和安装教程
  • (11)MSP430F5529 定时器B
  • (52)只出现一次的数字III
  • (Redis使用系列) SpirngBoot中关于Redis的值的各种方式的存储与取出 三
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (理论篇)httpmoudle和httphandler一览
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (转)ABI是什么
  • (转)Android学习笔记 --- android任务栈和启动模式
  • .bat批处理(十):从路径字符串中截取盘符、文件名、后缀名等信息
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .NET Core工程编译事件$(TargetDir)变量为空引发的思考
  • .Net Web项目创建比较不错的参考文章
  • .NET 设计一套高性能的弱事件机制
  • .net使用excel的cells对象没有value方法——学习.net的Excel工作表问题
  • .ui文件相关