IO流习题
-
多级复制文件夹
public class test2 { public static void main(String[] args) throws IOException { File srcFolder = new File("C:\\Users\\gaga\\Desktop\\abc"); File tergetFolder = new File("C:\\Users\\gaga\\Desktop\\5"); if(!tergetFolder.exists()){ tergetFolder.mkdirs(); } copyFolder(srcFolder,tergetFolder); } private static void copyFolder(File srcFolder, File tergetFolder) throws IOException { File[] files = srcFolder.listFiles(); for (File f : files) { if(f.isFile()){ copy(f,tergetFolder); }else { File file = new File(tergetFolder, f.getName()); if(!file.exists()){ file.mkdirs(); } copyFolder(f,file); } } } private static void copy(File srcFolder, File tergetFolder) throws IOException { FileInputStream fis = new FileInputStream(srcFolder); FileOutputStream fos = new FileOutputStream(new File(tergetFolder,srcFolder.getName())); byte[] bytes=new byte[1024*8]; int len=0; while ((len=fis.read(bytes))!=-1){ fos.write(bytes,0,len); fos.flush(); } fis.close(); fos.close(); } }
-
将文件夹中所有的.jpg格式改为.png格式
public static void main(String[] args) throws IOException { File srcFolder = new File("C:\\Users\\gaga\\Desktop\\abc"); File tergetFolder = new File("C:\\Users\\gaga\\Desktop\\5"); if(!tergetFolder.exists()){ tergetFolder.mkdirs(); } copyFolder(srcFolder,tergetFolder); } private static void copyFolder(File srcFolder, File tergetFolder) throws IOException { File[] files = srcFolder.listFiles(); for (File f : files) { if(f.isFile()){ copy(f,tergetFolder); }else { File file = new File(tergetFolder, f.getName()); if(!file.exists()){ file.mkdirs(); } copyFolder(f,file); } } } private static void copy(File srcFolder, File tergetFolder) throws IOException { FileInputStream fis = new FileInputStream(srcFolder); FileOutputStream fos=null; String name = srcFolder.getName(); if (name.endsWith(".jpg")){ String newname=name.substring(0,name.lastIndexOf(".")); String lastname=newname+".png"; fos = new FileOutputStream(new File(tergetFolder,lastname)); }else { fos = new FileOutputStream(new File(tergetFolder,srcFolder.getName())); } byte[] bytes=new byte[1024*8]; int len=0; while ((len=fis.read(bytes))!=-1){ fos.write(bytes,0,len); fos.flush(); } fis.close(); fos.close(); }
-
删除文件夹
public static void main(String[] args) throws FileNotFoundException { File file = new File("C:\\Users\\gaga\\Desktop\\5"); delate(file); } private static void delate(File file) { File[] files = file.listFiles(); for (File f : files) { if(f.isFile()){ f.delete(); }else { File file1 = new File(file, f.getName()); delate(file1); } } file.delete(); }
各种流
-
数据输入输出流
特点:能够读写基本数据类型。
public static void main(String[] args) throws IOException { write(); DataInputStream in = new DataInputStream(new FileInputStream("a.txt")); //怎么写,就怎么读,顺序不要乱。 int i = in.readInt(); System.out.println(i); boolean b = in.readBoolean(); System.out.println(b); double v = in.readDouble(); System.out.println(v); String s = in.readUTF(); System.out.println(s); } private static void write() throws IOException { DataOutputStream out = new DataOutputStream(new FileOutputStream("a.txt")); out.writeInt(100); out.writeBoolean(true); out.writeDouble(1.1); out.writeUTF("你好世界"); out.close(); }
-
内存操作流
-
ByteArrayOutputStream
特点:不关联任何文件,只在内存中进行读写。
他在内存中维护了一个字节数组,作为缓冲区,随着数据的不断写入,缓冲区会不断的扩充。
public static void main(String[] args) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write("asdf".getBytes()); out.write("qwer".getBytes()); //toByteArray(); 取出ByteArrayOutputStream所维护的字符数组。 byte[] bytes = out.toByteArray(); String s = new String(bytes); System.out.println(s); //toString();直接转换 String s1 = out.toString(); System.out.println(s1); //将bytes中的数据读到bytes1数组中去; byte[] bytes1 = new byte[1024]; ByteArrayInputStream in = new ByteArrayInputStream(bytes); int read = in.read(bytes1); String s2 = new String(bytes1, 0, read); System.out.println(s2); }
-
CharArrayWriter
CharArrayWriter charArrayWriter = new CharArrayWriter(); charArrayWriter.write("一行字符串"); charArrayWriter.write(new char[]{'a','b','c'}); //与ByteArrayOutputStream用法一致 char[] chars = charArrayWriter.toCharArray(); System.out.println(String.valueOf(chars));//一行字符串abc System.out.println(charArrayWriter.toString());//一行字符串abc
-
StringWriter
StringWriter stringWriter = new StringWriter(); stringWriter.write("abc"); stringWriter.write("abc"); stringWriter.write("abc"); stringWriter.write("abc"); System.out.println(stringWriter.toString());//abcabcabcabc
-
-
将两首歌合并成一首
-
用ByteArrayOutputStream流
public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("C:\\Users\\gaga\\Desktop\\A.mp3"); FileInputStream fis2 = new FileInputStream("C:\\Users\\gaga\\Desktop\\b.mp3"); byte[] bytes = new byte[1024 * 8]; int len =0; ArrayList<FileInputStream> list = new ArrayList<>(); list.add(fis); list.add(fis2); ByteArrayOutputStream out = new ByteArrayOutputStream(); for (FileInputStream fileInputStream : list) { while((len =fileInputStream.read(bytes))!=-1){ out.write(bytes,0,len); } fileInputStream.close(); } byte[] bytes1 = out.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(bytes1); FileOutputStream fos = new FileOutputStream("C:\\Users\\gaga\\Desktop\\C.mp3"); int len1=0; byte[] bytes2 = new byte[1024 * 8]; while ((len1=bis.read(bytes2))!=-1) { fos.write(bytes2,0,len1); fos.flush(); } fis.close(); fos.close(); }
-
用FileOutputStream流
public static void main(String[] args) throws IOException { FileInputStream fis1 = new FileInputStream("C:\\Users\\gaga\\Desktop\\A.mp3"); FileInputStream fis2 = new FileInputStream("C:\\Users\\gaga\\Desktop\\b.mp3"); FileOutputStream out = new FileOutputStream("C:\\Users\\gaga\\Desktop\\D.mp3"); ArrayList<FileInputStream> list = new ArrayList<>(); list.add(fis1); list.add(fis2); byte[] bytes = new byte[1024 * 8]; int len =0; for (FileInputStream fis : list) { while ((len=fis.read(bytes))!=-1){ out.write(bytes,0,len); out.flush(); } fis.close(); } }
-
打印流
特点:只能写出数据,不能读取数据,单个的,不是成对。
字节打印流:PrintStream
字符打印流:PrintWriter
public static void main(String[] args) throws IOException { PrintStream stream = new PrintStream("a.txt"); stream.write("asdf".getBytes()); stream.println("qweer");// asdfqweer } public static void main(String[] args) throws IOException { PrintWriter write = new PrintWriter("a.txt"); write.write("asdf"); write.println("qwer"); write.flush();//asdfqwer } //自动刷新,只有一些方法才能使用 public static void main(String[] args) throws IOException { PrintWriter writer = new PrintWriter(new FileOutputStream("a.txt"), true); writer.write("asdf"); writer.println("zxcv"); }
-
用PrintWriter复制文件
public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\gaga\\Desktop\\1.txt")); PrintWriter writer = new PrintWriter("2.txt"); while (true) { String s=reader.readLine(); if ((s != null)) { writer.println(s); } else { break; } } reader.close(); writer.close(); }
-
-
随机访问流
能读能写
这个流有文件指针,可以指定位置。
RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStream和OutputStream的功能。
支持对随机访问文件的读取和写入。public static void main(String[] args) throws IOException { RandomAccessFile in = new RandomAccessFile("e.txt", "rw"); int i = in.readInt(); long filePointer = in.getFilePointer(); System.out.println("指针位置:"+filePointer);//4 int 4个字节 double v = in.readDouble(); filePointer = in.getFilePointer(); System.out.println("指针位置:"+filePointer);//12 double 8个字节 boolean b = in.readBoolean(); filePointer = in.getFilePointer(); System.out.println("指针位置:" + filePointer);//13 Boolean 1个字节 String s = in.readUTF(); filePointer = in.getFilePointer(); System.out.println("指针位置:" + filePointer);//21 一个汉字3个字节,在调用writeUTF方法 时会先写两个字节 in.seek(13);//定位文件指针的位置 } private static void writeData() throws IOException { RandomAccessFile out = new RandomAccessFile("e.txt", "rw"); out.writeInt(200); out.writeDouble(32.2); out.writeBoolean(false); out.writeUTF("你好"); }
-
用RandomAccessFile实现断点复制
public static void main(String[] args) throws IOException { RandomAccessFile in = new RandomAccessFile("C:\\Users\\gaga\\Desktop\\A.mp3","rw"); RandomAccessFile out = new RandomAccessFile("C:\\Users\\gaga\\Desktop\\E.mp3","rw"); try { //点继续下载,先判断之前下载的文件在不在,如果不在 File file = new File("C:\\Users\\gaga\\Desktop\\E.mp3"); if(file.exists()){ FileInputStream fis = new FileInputStream("a.txt"); long read = fis.read(); in.seek(read+1); out.seek(read+1); }else { in.seek(0); out.seek(0); } int len =0; int i=1; while ((len =in.read())!=-1){ out.write(len); i++; // if(i>=6000){ // System.out.println(1/0); // } 第一次运行时删除注释符号,第二次运行时加上注释 } in.close(); out.close(); } catch (Exception e) { long filePointer = in.getFilePointer(); System.out.println(filePointer); PrintWriter writer = new PrintWriter("a.txt"); writer.println(filePointer); writer.flush(); writer.close(); } }
-
-
序列化流和反序列化流
序列化:将对象通过流的方式存到文件当中
反序列化:把文件中的对象以流的方式还原回来
public class tudent1 implements Serializable //如果想要一个类的对象,能被序列化,要求该类要实现 Serializable 标记接口。 { private String name; private int num; private transient int age;// transient 可以排除某些属性,不要被序列化。 public tudent1(String name, int num, int age) { this.name = name; this.num = num; this.age = age;} public String getName() {return name;} public void setName(String name) {this.name = name;} public int getNum() {return num;} public void setNum(int num) {this.num = num;} public int getAge() {return age;} public void setAge(int age) {this.age = age;} } public class test { public static void main(String[] args) throws IOException, ClassNotFoundException { ds(); ObjectInputStream in = new ObjectInputStream(new FileInputStream("ds.txt")); tudent1 o = (tudent1) in.readObject(); System.out.println(o.getAge()); } private static void ds() throws IOException { tudent1 ds = new tudent1("ds", 2, 5); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("ds.txt")); out.writeObject(ds); out.close(); } }
-
属性集合
//配置文件 //存入配置文件 public static void main(String[] args) throws FileNotFoundException { HashMap<String, String> hm = new HashMap<>(); hm.put("username","zhangsan"); hm.put("password","123456"); PrintWriter printWriter = new PrintWriter(new FileOutputStream("user.properties"), true); Set<Map.Entry<String, String>> entries = hm.entrySet(); for (Map.Entry<String, String> entry : entries) { String key = entry.getKey(); String value = entry.getValue(); printWriter.println(key+"="+value); } printWriter.close(); } //读取配置文件 public static void main(String[] args) throws IOException { HashMap<String, String> hm = new HashMap<>(); BufferedReader reader = new BufferedReader(new FileReader("user.properties")); while (true){ String line = reader.readLine(); if(line!=null){ String[] s = line.split("="); hm.put(s[0],s[1]); }else{ break; } } System.out.println(hm);//{password=123456, username=zhangsan} } //配置文件写入和读取 public static void main(String[] args) { //Properties 他的键和值的数据类型,已经默认为String Properties properties = new Properties(); properties.setProperty("username", "zhangsan"); String username = properties.getProperty("username2"); System.out.println(username);//null 键找值 System.out.println(properties);//{username=zhangsan} } //配置文件存储 public static void main(String[] args) throws IOException { Properties properties = new Properties(); properties.setProperty("username","李四"); properties.setProperty("password","654321"); properties.store(new FileOutputStream("MyUser.properties"),null); } //配置文件的读取 public static void main(String[] args) throws IOException { // 使用 Properties 来读取配置文件,配置文件有要求 1,一般配置文件的后缀名 .properties //2. 配置文件中的键值 以 = 分割。 Properties properties = new Properties(); properties.load(new FileInputStream("MyUser.properties")); System.out.println(properties); }