java使用流文件转移_java中IO流对文件操作的代码示例

本篇文章给大家带来的内容是关于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到另一个文件

package com.wpbxx.stream;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class BufferFileCopy {

public static void main(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"));

int temp;

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();

}

}

}

}

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

package com.wpbxx.stream;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class CodeFile {

public static void main(String[] args) {

//jdk7新写法

try (

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

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

) {

int temp;

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

bos.write(temp ^ 88);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

文件解密

package com.wpbxx.stream;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

public class DecodeFile {

public static void main(String[] args) {

try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("code.png"));

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("decode.png"));) {

int temp;

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");

int temp;

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文件

package com.wpbxx.chario;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

/**

* 使用缓冲流拷贝文件

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

*/

public class BufferFileCopy {

public static void main(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 = new Date(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);

}

几个小练习

package com.wpbxx.exercise;

import java.io.File;

import java.util.Scanner;

/**

* 问题:从键盘接收一个路径,将这个路径下的所有文件和文件夹的名字按照层级打印。

* 例如:

* wpbxx

* java

* XXX.java

* XXX.jpg

* php

* XXX.php

* readme.txt

*

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

* 遍历数组,取得File对象

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

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

*/

public class FileNames {

//用来记录缩进的次数

private static int count = 0;

public static void main(String[] args) {

File file = getFile();

getFileNames(file);

}

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

private static void getFileNames(File file) {

//获取路径File对象中的File数组

File[] fileArray = file.listFiles();

//遍历数组,取得File对象

for(int i=0; i

//通过遍历count来控制打印几个缩进

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 static File getFile() {

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

Scanner sc = new Scanner(System.in);

//获取用户输入的路径,用户输入的路径有可能是错误的,需要进行判断

while(true){

String input = sc.nextLine();

File file = new File(input);

if(!file.exists()){

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

}else if(file.isFile()){

//如果用户输入的路径是一个文件

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

}else{

return file;

}

}

}

}

package com.wpbxx.exercise;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

/**

* 问题:收费版软件有试用次数,利用IO流的知识,模拟一个可以试用3次的功能,打开3次之后提示用户购买正版软件

*

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

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

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

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

*/

public class Trial {

public static void main(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 void code() {

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();

}

}

}

}

package com.wpbxx.exercise;

import java.io.File;

/**

* 问题:统计项目根目录下以.txt结尾的文件数量,并将文件名打印出来

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

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

*/

public class FindTxt {

public static void main(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 + "个");

}

}

package com.monkey1024.file;

import java.io.File;

import java.io.FilenameFilter;

/**

* 问题:统计项目根目录下以.txt结尾的文件数量,并将文件名打印出来

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

*/

public class FilenameFilterTest01 {

public static void main(String[] args) {

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

String[] nameArray = file.list(new FilenameFilter() {

@Override

public boolean accept(File dir, String name){

//获取根目录下每个文件的File对象

File file1 = new File(dir, name);

//编写筛选条件

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

}

});

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

for(String name : nameArray){

System.out.println(name);

}

}

}

package com.wpbxx.file;

import java.io.File;

import java.io.FilenameFilter;

/**

* 问题:统计项目根目录下以.txt结尾的文件数量,并将文件名打印出来

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

*/

public class FilenameFilterTest01 {

public static void main(String[] args) {

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

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

@Override

public boolean accept(File dir, String name){//将过滤的规则写进来

//获取根目录下每个文件的File对象

File file1 = new File(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有两 种生成方式:

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

有一个类

package com.monkey1024.serializable;

import java.io.Serializable;

/**

* 如果一个类的对象支持序列化和反序列化,需要实现Serializable

* Serializable中没有任何方法

*/

public class Student implements Serializable{

/**

* 自动生成序列化版本号

*/

private static final long serialVersionUID = -716323668524282676L;

private String name;

//添加属性后,使用反序列化时会报出InvalidClassException

//transient修饰的变量不会被序列化

transient private int age;

private boolean sex;

public boolean isSex() {

return sex;

}

public void setSex(boolean sex) {

this.sex = sex;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

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

Student zhangsan = new Student();

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());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值