package haixinpackage; import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Random; public class How2jTest { public static void main(String[] args) { byte[] data = new byte[]{88,99}; //write("/Users/haixin/IdeaProjects/javatext/src/test1.txt",data); Father chen = new Father("chen"); File f = new File("/Users/haixin/IdeaProjects/javatext/src/test.txt"); try(FileInputStream fis = new FileInputStream(f); ObjectInputStream ois = new ObjectInputStream(fis); FileOutputStream fos = new FileOutputStream(f); ObjectOutputStream oos = new ObjectOutputStream(fos) ){ oos.writeObject(chen); Father father = null; try { father = (Father) ois.readObject(); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println(father.getName()); }catch(IOException e){ e.printStackTrace(); } } public static long searchFile(File f){ long max = 0; long min = Long.MAX_VALUE; File[] fs = f.listFiles(); for (File f2:fs){ if(f2.isDirectory()){ searchFile(f2); } if(f2.isFile()){ max = f2.length() > max ? f2.length() : max; min = f2.length() < min ? f2.length() : max; } } return min; } public static void write(String str,byte[] data) { File f = new File(str); String parentstr = f.getParent(); File parent = new File(parentstr); parent.mkdirs(); try { f.createNewFile(); FileOutputStream fos = new FileOutputStream(f); fos.write(data); fos.close(); System.out.println("完成"); } catch (IOException e) { e.printStackTrace(); } } public static void copyFile(String srcFile, String destFile){ File srcfile = new File(srcFile); File destfile = new File(destFile); File destParent = destfile.getParentFile(); //获取父目录路径 destParent.mkdirs(); try( FileInputStream srcfis = new FileInputStream(srcfile); FileOutputStream desfos = new FileOutputStream(destfile);){ byte[] alldata = new byte[(int)srcfile.length()]; if(-1 != srcfis.read(alldata)){ desfos.write(alldata); } }catch(IOException e){ e.printStackTrace(); } } //查询文件尾为search的文件 public static void search(File folder, String search){ //File parentfile = folder.getParentFile(); while(folder.isDirectory()){ while(folder.list() != null){ File[] filelis = folder.listFiles(); for (int i = 0; i < filelis.length; i++){ if(search.equals(filelis[i].getName().substring(filelis[i].getName().length()-4))){ } } } } } } class Father implements Serializable{ private String name; public Father(String name){ this.name = name; } public String getName(){ return this.name; } }
java io流
最新推荐文章于 2023-02-20 15:09:50 发布