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<fileArray.length; i++){ //经由过程遍历count来掌握打印几个缩进 for(int j=0; j<count; 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());
以上就是java中IO流对文件操纵的代码示例的细致内容,更多请关注ki4网别的相干文章!