class File
文件和目录路径的抽象表示
//length();//getAbsolutelyPath();///File[] listFiles();//boolean mkdirs();//boolean renameTo(File dest);public static void main(String[] args) throws IOException {File dir = new File("C://haha");dir.mkdir(); //创建文件夹File a = new File("C:", "a.txt");a.createNewFile(); //创建新文件File b = new File("C://haha","b.txt");b.createNewFile();//a.delete(); //删除操作//b.delete();a.renameTo(b);System.out.println(File.pathSeparator);System.out.println(File.separator);}//
文件遍历
public static void main(String[] args) {File e = new File("D://");File[] files = e.listFiles();fileSerach(files);}public static void fileSerach(File[] files){if(files!=null && files.length>0){for(File file:files){if(file.isFile()){//文件if(file.getName().endsWith(".avi") && file.length()>(1*1024*1024*1024)){System.out.println("路径是:"+file.getAbsolutePath());}}else{//文件夹File[] files2 = file.listFiles();fileSerach(files2);}}}}
文件过滤器
public static void main(String[] args) {File a = new File("D://");listfile(a);}public static void listfile(File file){//创建一个过滤器,并且创建规则File[] files = file.listFiles(new FileFilter() {@Overridepublic boolean accept(File pathname) {if(pathname.getName().equals("matlab")){return false;}else if(pathname.getName().endsWith(".avi") || pathname.isDirectory()){return true;}return false;}});if(files !=null && files.length>0)for(File f : files){if(f.isDirectory()){listfile(f);}else{System.out.println(f.getAbsolutePath());}}}
相对路径与绝对路径
- 绝对路径:从盘符开始,是一个完整的路径,例如:C://users
- 相对路径:在 Java 代码中是相对于项目目录路径,这是一个不完整的便捷路径。
IO 流
java.io包下的常用类的使用,通过这些类对数据进行读取(Input)和写出(Output)
- 流的方向:输出流 输入流
- 流动的数据类型:字节流(InputStream,OutputStream) 字符流(Reader,Writer)
字节流 OutputStream
一切皆字节,二进制形式存储void close();void flush(); 刷新此输出流并强制写出任何缓冲的输出字节。
void |
[write](#write(byte%5B%5D))(byte[] b) |
将 b.length字节从指定的字节数组写入此输出流。 |
|---|---|---|
void |
[write](#write(byte%5B%5D,int,int))(byte[] b, int off, int len) |
将从偏移量 off开始的指定字节数组中的 len字节写入此输出流。 |
abstract void |
[write](#write(int))(int b) |
将指定的字节写入此输出流。 |
FileOutputStream
public static void main(String[] args) throws IOException {FileOutputStream fos = new FileOutputStream("d://test.txt");//创建一个流对象//FileOutputStream fos = new FileOutputStream("d://test.txt",true);追加byte[] bytes = "ABCDE".getBytes();fos.write(bytes,2,1);//2下标开始,写1个字符 输出Cfos.close();System.out.println("写出了");}
FileInputStream
//读取一个字符 int read()public static void main(String[] args) throws IOException {FileInputStream fil = new FileInputStream("D://test.txt");//byte b = (byte)fil.read();//强制类型转换//System.out.println(b);//byte b1 = (byte)fil.read();//System.out.println((char)b1);//转换成字符打印while(true){byte a = (byte)fil.read();//超过总字节数就会返回-1if(a==-1)break;System.out.println(a);}}//读取一组字节public static void main(String[] args) throws IOException {FileInputStream fis = new FileInputStream("D://test.txt");byte[] bytes = new byte[10];int len1 = fis.read(bytes);System.out.println(new String(bytes,0,len1));//改进之后len1 = fis.read(bytes);System.out.println(new String(bytes,0,len1));len1 = fis.read(bytes);System.out.println(new String(bytes,0,len1));len1 = fis.read();System.out.println(len1);fis.close();//abcdefghij//klmnopqrst//uvwxyz//-1//abcdefghij//klmnopqrst//uvwxyzqrst 多了qrst 因为上依次读取的后面四个没有被清空}
字符编码
UTF-8 可变长度字符编码
字符输出
public static void main(String[] args) throws IOException {FileWriter fw = new FileWriter("D://12.txt",true);//追加模式fw.write("一二三四五");fw.append("上山打老虎").append(",").append("老虎达到了");fw.close();}
flush
一定要刷新管道
刷新缓存空间,强制把缓存的内容写入到文件中去
字符读取
public static void main(String[] args) throws IOException {FileReader fr = new FileReader("a.txt");/*int c = fr.read();System.out.println((char)c);while(true) {//循环读取c = fr.read();if (c==-1)break;System.out.print((char)c+" ");}*/char[] chars = new char[100];//读取这样的数据一定要确认长度int len = fr.read(chars);System.out.println(new String(chars, 0, len));//一二三四五草拟吗System.out.println(new String(chars,0,len).length());//8fr.close();}
字节流读取文字
可能存在只读一半的问题
File s = new File("D://asd.txt");s.createNewFile();FileInputStream fis = new FileInputStream("D://asd.txt");byte[] bytes = new byte[10];、、读取一半的问题int len = fis.read(bytes);System.out.println(new String(bytes, 0, len));fis.close();
字节流转换(装饰)成字符流
使用了装饰者设计模式
public static void main(String[] args) throws IOException {FileOutputStream fis = new FileOutputStream("a.txt");OutputStreamWriter osw = new OutputStreamWriter(fis);//转换成字符流osw.write("12344aui");osw.flush();osw.close();}
Print 与 BufferedReader
PrintStream / PrintWriter
public static void main(String[] args) throws IOException {FileOutputStream fos = new FileOutputStream("a.txt");PrintWriter pw = new PrintWriter(fos);//字节流转换成字符流pw.println("一二三四五我草");pw.println("一二三四五我草");pw.println("一二三四五我草");pw.flush();}// 将字符输入流,转换为带有缓存,可以一次读取一行的字符读取流public static void main(String[] args) throws IOException {FileReader fw = new FileReader("a.txt");BufferedReader br = new BufferedReader(fw);String text = br.readLine();System.out.println(text);}
收集异常日志
public static void main(String[] args) throws FileNotFoundException {try{String s = null;s.toString();}catch(Exception e){PrintWriter pw = new PrintWriter("a.txt");SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm");pw.println(sdf.format(new Date()));e.printStackTrace(pw);pw.flush();}}
properties
问题:后续存储快递包裹的时候的 key 与 value 的对应问题
该怎样去存储包裹
public static void main(String[] args) throws IOException {/*//Properties 文件Properties ppt = new Properties();//存储键值对ppt.put("快递单号", "12345");ppt.put("快递公司", "顺丰快递");FileWriter fw = new FileWriter("D://package.properties", true);ppt.store(fw,"存储的快递信息");fw.close();*/Properties ppt = new Properties();FileReader fr = new FileReader("D://package.properties");ppt.load(fr);System.out.println(ppt.get("快递单号"));System.out.println(ppt.get("快递公司"));}
序列化
public static void main(String[] args) throws IOException, ClassNotFoundException {//序列化/*Package p = new Package("123456", "顺丰快递","823616");ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D://1.txt"));oos.writeObject(p);oos.close();*///反序列化ObjectInputStream ois = new ObjectInputStream(new FileInputStream(("D://1.txt")));Package o = (Package)ois.readObject();//存的是什么,就读出来什么System.out.println(o);System.out.println(o.getCompany());}static class Package implements Serializable{//添加标记//判断private String number;private String company;private String code;@Overridepublic String toString() {return "Package{" +"number='" + number + '\'' +", company='" + company + '\'' +", code='" + code + '\'' +'}';}public String getNumber() {return number;}public void setNumber(String number) {this.number = number;}public String getCompany() {return company;}public void setCompany(String company) {this.company = company;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}public Package() {}public Package(String number, String company, String code) {this.number = number;this.company = company;this.code = code;}}
部分属性的序列化和反序列化
- 使用 transient 修饰符:修改实体类,将实体类中不想序列化的属性添加 transient 修饰词
- 使用 static 修饰符 :static 修饰的属性不会参与序列化
- 使用默认方法
writeObject 和 readObject 方法Externalizeable实现Java序列化
try-with-resources
public static void main(String[] args) throws FileNotFoundException {//FileReader fr = new FileReader("a.txt");PrintWriter pw = new PrintWriter("a.txt");try(fr;pw){//实现自动关闭 需要实现他的 close 方法int c = fr.read();System.out.println((char)c);} catch (IOException e) {e.printStackTrace();}}
