class File

文件和目录路径的抽象表示

  1. //length();
  2. //getAbsolutelyPath();
  3. ///File[] listFiles();
  4. //boolean mkdirs();
  5. //boolean renameTo(File dest);
  6. public static void main(String[] args) throws IOException {
  7. File dir = new File("C://haha");
  8. dir.mkdir(); //创建文件夹
  9. File a = new File("C:", "a.txt");
  10. a.createNewFile(); //创建新文件
  11. File b = new File("C://haha","b.txt");
  12. b.createNewFile();
  13. //a.delete(); //删除操作
  14. //b.delete();
  15. a.renameTo(b);
  16. System.out.println(File.pathSeparator);
  17. System.out.println(File.separator);
  18. }//

文件遍历

  1. public static void main(String[] args) {
  2. File e = new File("D://");
  3. File[] files = e.listFiles();
  4. fileSerach(files);
  5. }
  6. public static void fileSerach(File[] files){
  7. if(files!=null && files.length>0){
  8. for(File file:files){
  9. if(file.isFile()){//文件
  10. if(file.getName().endsWith(".avi") && file.length()>(1*1024*1024*1024)){
  11. System.out.println("路径是:"+file.getAbsolutePath());
  12. }
  13. }else{
  14. //文件夹
  15. File[] files2 = file.listFiles();
  16. fileSerach(files2);
  17. }
  18. }
  19. }
  20. }

文件过滤器

  1. public static void main(String[] args) {
  2. File a = new File("D://");
  3. listfile(a);
  4. }
  5. public static void listfile(File file){
  6. //创建一个过滤器,并且创建规则
  7. File[] files = file.listFiles(new FileFilter() {
  8. @Override
  9. public boolean accept(File pathname) {
  10. if(pathname.getName().equals("matlab")){
  11. return false;
  12. }else if(pathname.getName().endsWith(".avi") || pathname.isDirectory()){
  13. return true;
  14. }
  15. return false;
  16. }
  17. });
  18. if(files !=null && files.length>0)
  19. for(File f : files){
  20. if(f.isDirectory()){
  21. listfile(f);
  22. }else{
  23. System.out.println(f.getAbsolutePath());
  24. }
  25. }
  26. }

相对路径与绝对路径

  • 绝对路径:从盘符开始,是一个完整的路径,例如: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

  1. public static void main(String[] args) throws IOException {
  2. FileOutputStream fos = new FileOutputStream("d://test.txt");//创建一个流对象
  3. //FileOutputStream fos = new FileOutputStream("d://test.txt",true);追加
  4. byte[] bytes = "ABCDE".getBytes();
  5. fos.write(bytes,2,1);//2下标开始,写1个字符 输出C
  6. fos.close();
  7. System.out.println("写出了");
  8. }

FileInputStream

  1. //读取一个字符 int read()
  2. public static void main(String[] args) throws IOException {
  3. FileInputStream fil = new FileInputStream("D://test.txt");
  4. //byte b = (byte)fil.read();//强制类型转换
  5. //System.out.println(b);
  6. //byte b1 = (byte)fil.read();
  7. //System.out.println((char)b1);//转换成字符打印
  8. while(true){
  9. byte a = (byte)fil.read();//超过总字节数就会返回-1
  10. if(a==-1)
  11. break;
  12. System.out.println(a);
  13. }
  14. }
  15. //读取一组字节
  16. public static void main(String[] args) throws IOException {
  17. FileInputStream fis = new FileInputStream("D://test.txt");
  18. byte[] bytes = new byte[10];
  19. int len1 = fis.read(bytes);
  20. System.out.println(new String(bytes,0,len1));//改进之后
  21. len1 = fis.read(bytes);
  22. System.out.println(new String(bytes,0,len1));
  23. len1 = fis.read(bytes);
  24. System.out.println(new String(bytes,0,len1));
  25. len1 = fis.read();
  26. System.out.println(len1);
  27. fis.close();
  28. //abcdefghij
  29. //klmnopqrst
  30. //uvwxyz
  31. //-1
  32. //abcdefghij
  33. //klmnopqrst
  34. //uvwxyzqrst 多了qrst 因为上依次读取的后面四个没有被清空
  35. }

字符编码

UTF-8 可变长度字符编码

字符输出

  1. public static void main(String[] args) throws IOException {
  2. FileWriter fw = new FileWriter("D://12.txt",true);//追加模式
  3. fw.write("一二三四五");
  4. fw.append("上山打老虎").append(",").append("老虎达到了");
  5. fw.close();
  6. }

flush

一定要刷新管道
刷新缓存空间,强制把缓存的内容写入到文件中去

字符读取

  1. public static void main(String[] args) throws IOException {
  2. FileReader fr = new FileReader("a.txt");
  3. /*int c = fr.read();
  4. System.out.println((char)c);
  5. while(true) {//循环读取
  6. c = fr.read();
  7. if (c==-1)
  8. break;
  9. System.out.print((char)c+" ");
  10. }*/
  11. char[] chars = new char[100];//读取这样的数据一定要确认长度
  12. int len = fr.read(chars);
  13. System.out.println(new String(chars, 0, len));//一二三四五草拟吗
  14. System.out.println(new String(chars,0,len).length());//8
  15. fr.close();
  16. }

字节流读取文字

可能存在只读一半的问题

  1. File s = new File("D://asd.txt");
  2. s.createNewFile();
  3. FileInputStream fis = new FileInputStream("D://asd.txt");
  4. byte[] bytes = new byte[10];、、读取一半的问题
  5. int len = fis.read(bytes);
  6. System.out.println(new String(bytes, 0, len));
  7. fis.close();

字节流转换(装饰)成字符流

使用了装饰者设计模式

  1. public static void main(String[] args) throws IOException {
  2. FileOutputStream fis = new FileOutputStream("a.txt");
  3. OutputStreamWriter osw = new OutputStreamWriter(fis);//转换成字符流
  4. osw.write("12344aui");
  5. osw.flush();
  6. osw.close();
  7. }

Print 与 BufferedReader

PrintStream / PrintWriter

  1. public static void main(String[] args) throws IOException {
  2. FileOutputStream fos = new FileOutputStream("a.txt");
  3. PrintWriter pw = new PrintWriter(fos);//字节流转换成字符流
  4. pw.println("一二三四五我草");
  5. pw.println("一二三四五我草");
  6. pw.println("一二三四五我草");
  7. pw.flush();
  8. }
  9. // 将字符输入流,转换为带有缓存,可以一次读取一行的字符读取流
  10. public static void main(String[] args) throws IOException {
  11. FileReader fw = new FileReader("a.txt");
  12. BufferedReader br = new BufferedReader(fw);
  13. String text = br.readLine();
  14. System.out.println(text);
  15. }

收集异常日志

  1. public static void main(String[] args) throws FileNotFoundException {
  2. try{
  3. String s = null;
  4. s.toString();
  5. }catch(Exception e){
  6. PrintWriter pw = new PrintWriter("a.txt");
  7. SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm");
  8. pw.println(sdf.format(new Date()));
  9. e.printStackTrace(pw);
  10. pw.flush();
  11. }
  12. }

properties

问题:后续存储快递包裹的时候的 key 与 value 的对应问题
该怎样去存储包裹

  1. public static void main(String[] args) throws IOException {
  2. /*//Properties 文件
  3. Properties ppt = new Properties();
  4. //存储键值对
  5. ppt.put("快递单号", "12345");
  6. ppt.put("快递公司", "顺丰快递");
  7. FileWriter fw = new FileWriter("D://package.properties", true);
  8. ppt.store(fw,"存储的快递信息");
  9. fw.close();*/
  10. Properties ppt = new Properties();
  11. FileReader fr = new FileReader("D://package.properties");
  12. ppt.load(fr);
  13. System.out.println(ppt.get("快递单号"));
  14. System.out.println(ppt.get("快递公司"));
  15. }

序列化

  1. public static void main(String[] args) throws IOException, ClassNotFoundException {
  2. //序列化
  3. /*Package p = new Package("123456", "顺丰快递","823616");
  4. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D://1.txt"));
  5. oos.writeObject(p);
  6. oos.close();*/
  7. //反序列化
  8. ObjectInputStream ois = new ObjectInputStream(new FileInputStream(("D://1.txt")));
  9. Package o = (Package)ois.readObject();//存的是什么,就读出来什么
  10. System.out.println(o);
  11. System.out.println(o.getCompany());
  12. }
  13. static class Package implements Serializable{//添加标记
  14. //判断
  15. private String number;
  16. private String company;
  17. private String code;
  18. @Override
  19. public String toString() {
  20. return "Package{" +
  21. "number='" + number + '\'' +
  22. ", company='" + company + '\'' +
  23. ", code='" + code + '\'' +
  24. '}';
  25. }
  26. public String getNumber() {
  27. return number;
  28. }
  29. public void setNumber(String number) {
  30. this.number = number;
  31. }
  32. public String getCompany() {
  33. return company;
  34. }
  35. public void setCompany(String company) {
  36. this.company = company;
  37. }
  38. public String getCode() {
  39. return code;
  40. }
  41. public void setCode(String code) {
  42. this.code = code;
  43. }
  44. public Package() {
  45. }
  46. public Package(String number, String company, String code) {
  47. this.number = number;
  48. this.company = company;
  49. this.code = code;
  50. }
  51. }

部分属性的序列化和反序列化

  • 使用 transient 修饰符:修改实体类,将实体类中不想序列化的属性添加 transient 修饰词
  • 使用 static 修饰符 :static 修饰的属性不会参与序列化
  • 使用默认方法writeObject 和 readObject 方法

    Externalizeable实现Java序列化

此接口继承了Serializable接口
开发相对复杂

try-with-resources

  1. public static void main(String[] args) throws FileNotFoundException {
  2. //
  3. FileReader fr = new FileReader("a.txt");
  4. PrintWriter pw = new PrintWriter("a.txt");
  5. try(fr;pw){//实现自动关闭 需要实现他的 close 方法
  6. int c = fr.read();
  7. System.out.println((char)c);
  8. } catch (IOException e) {
  9. e.printStackTrace();
  10. }
  11. }