zoukankan      html  css  js  c++  java
  • Java IO学习总结(一)

    一、File 类

    Java中不管文件还是目录都可以使用File类操作,File能新建、删除、重命名文件和目录,但是不能访问文件内容本身,访问文件内容需要使用输入输出流。

    File类主要使用:

    public class FileTest {
        public static void main(String[] args) throws IOException {
            File file = new File("test");
            System.out.println("文件名称:" + file.getName());
            System.out.println("文件路径:" + file.getAbsolutePath());
            System.out.println("是否是文件:" + file.isFile());
            System.out.println("是否是路径:" + file.isDirectory());
            System.out.println("文件是否存在:" + file.exists());
            // 创建文件
            file.createNewFile();
            System.out.println("文件是否存在:" + file.exists());
            // 创建目录
            System.out.println("创建目录是否成功:" + file.mkdir());
            // 文件重命名
            file.renameTo(new File("test1"));
            System.out.println("文件名称:" + file.getName());
            
            String [] fileList =  file.list();
            for (String str : fileList) {
                System.out.println("文件名称:" + str);
            }
        }
    }

    二、IO流

    1)流的分类

    Java的输入输出流都是从程序运行所在内存的角度划分的。

    输入流:只能从中读取数据,不能写入数据

    输出流:只能向其中写入数据,不能读取数据。

    字节流:字节流操作的是8位字节,主要以InputStream和OutStream作为基类

    字符流:字符流操作的是16位的字符,主要以Reader和Writer作为基类

    节点流:可以向一个特定IO设备(如磁盘、网络)读写数据,称为节点流

    处理流:对一个已存在的流进行连接和封装,通过封装后的流来实现数据读写功能

    2)字节流和字符流

    • InputStream和Reader
      主要方法使用
      public class InputStreamTest {
          public static void main(String[] args) {
                  // 创建字节输入流
                  FileInputStream fis = null;
              try {
                  fis = new FileInputStream("d:\test.txt");
                  // 创建每次读取字节数的数组
                  byte[] byt = new byte[1024];
                  // 保存实际读取的字节数
                  int hasRead = 0;
                  while((hasRead = fis.read(byt)) != -1){
                      // 字节转换成字符输出
                      System.out.println(new String(byt,0,hasRead));
                  }
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              } finally
              {
                  // 关闭IO流
                  try {
                      fis.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      }
      
      public class ReaderTest {
          public static void main(String[] args) {
              // 创建字符流
              FileReader fr = null;
              try {
                  fr = new FileReader("d:\test.properties");
                  // 创建每次读取字符数组
                  char [] ch = new char[32];
                  // 保存时间读取字符数
                  int hasRead = 0;
                  while((hasRead = fr.read(ch)) != -1){
                      System.out.println(new String(ch,0,hasRead));
                  }
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }finally{
                  try {
                      fr.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      }
    • OutputStream和Writer
      主要方法使用
      public class OutputStreamTest {
          public static void main(String[] args) {
              FileInputStream fis = null;
              FileOutputStream fos = null;
              try {
                  // 创建输入输出流
                  fis = new FileInputStream("d:\test.txt");
                  fos = new FileOutputStream("d:\test1.txt");
                  
                  byte[] byt = new byte[1024];
                  int read = 0;
                  while((read = fis.read(byt)) != -1){
                      // 读取多少就输出
                      fos.write(byt, 0, read);
                  }
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }finally{
                  try {
                      fos.close();
                      fis.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }
      }
      
      public class WriterTest {
          public static void main(String[] args) {
              FileWriter fw = null;
              try{
                  fw = new FileWriter("d:\test2.txt");
                  fw.write("这是一段测试文字。
      ");
                  fw.write("换行继续展示。
      ");
                  fw.flush();
              } catch (IOException e) {
                  e.printStackTrace();
              }finally{
                  try {
                      fw.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
              
          }
      }
      注意点:在使用完IO时记得关闭输出流,因为关闭流可以保证流的物理资源被回收,还可以将输出流缓冲区的数据flush到物理节点(关闭流之前自动执行flush()方法),保证程序正常运行。 

    3)处理流

      使用处理流来包装节点流,通过处理流来执行输入输出功能,让节点流与底层I/O设备、文件交互。

      处理流和节点流区别:流的构造参数不是一个物理节点,而且是已经存在的流,就是处理流,节点流的构造参数都是以物理节点为构造参数的。 

    public class PrintStreamTest {
        public static void main(String[] args){
            try {
                FileOutputStream fos = new FileOutputStream("d:\test2.txt");
                PrintStream ps = new PrintStream(fos);
                
                ps.print("普通字符串");
                ps.println(new PrintStreamTest());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            
        }
    }
  • 相关阅读:
    二叉排序树
    索引顺序表查找(分块查找)
    wpf中的窗口
    递归算法以及汉诺塔
    Net中资源存储的设置
    AutoResetEvent 和ManualResetEvent
    WPF:跨应用程序会话保持和还原应用程序范围的属性
    Base64编码及其作用
    WPF中的应用程序级别Application
    Ajax经典学习教材,IBM官方Ajax教材
  • 原文地址:https://www.cnblogs.com/quina520/p/7252827.html
Copyright © 2011-2022 走看看