Java中的IO

    技术2025-03-19  21

    Java中的IO

    一、File类

    java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建、查找和删除等操作。

    文件和目录的创建

    //文件的创建 public class Test01 { public static void main(String[] args) throws IOException { File file = new File("D:\\Users\\26212\\Desktop\\m.txt"); file.createNewFile(); } } //单级目录的创建 public class Test01 { public static void main(String[] args) throws IOException { File file = new File("D:\\Users\\26212\\Desktop\\m"); file.mkdir(); } } //多级目录的创建 public class Test01 { public static void main(String[] args) throws IOException { File file = new File("D:\\Users\\26212\\Desktop\\a\\b\\c"); file.mkdirs(); } }

    文件和目录的查找

    public String getAbsolutePath() :返回此File的绝对路径名字符串。

    public String getPath() :将此File转换为路径名字符串。

    public String getName() :返回由此File表示的文件或目录的名称。

    public long length() :返回由此File表示的文件的长度。

    public boolean exists() :此File表示的文件或目录是否实际存在。

    public boolean isDirectory() :此File表示的是否为目录。

    public boolean isFile() :此File表示的是否为文件。

    public class Test01 { public static void main(String[] args) throws IOException { File file = new File("D:\\Users\\26212\\Desktop\\m.txt"); String absolutePath = file.getAbsolutePath(); System.out.println(absolutePath);//D:\Users\26212\Desktop\m.txt String path = file.getPath(); System.out.println(path);//D:\Users\26212\Desktop\m.txt String name = file.getName(); System.out.println(name);//m.txt long length = file.length(); System.out.println(length);//18 } }

    文件和目录的删除

    都是delete方法

    public class Test01 { public static void main(String[] args) throws IOException { File file = new File("D:\\Users\\26212\\Desktop\\a"); file.delete(); } }

    二、字节流

    我们都知道一句话叫做一切皆为字节,我们平时什么图片、文档、视频等都是字节

    字节输出流(OutputStream)

    OutputStream是一个抽象类,我们来研究他的实现类,最简单的就是FileOutputStream

    public class Test01 { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("a.txt"); fos.write(97); fos.close(); } } 输出为a //和前面的知识连起来用 public class Test01 { public static void main(String[] args) throws IOException { File file = new File("D:\\Users\\26212\\Desktop\\m.txt"); file.createNewFile(); FileOutputStream fos = new FileOutputStream("D:\\Users\\26212\\Desktop\\m.txt",true);//这里加了true的话可以续写 byte[] bytes = "我是王".getBytes(); fos.write(bytes); fos.close(); } }

    字节输入流(InpuStream)

    同样也是来讲FileInpuStream这个实现类

    public class Test01 { public static void main(String[] args) throws IOException { FileInputStream file = new FileInputStream("m.txt"); int read = file.read(); System.out.println((char)read); } }

    这里是一个一个字节读取,知道没有就是-1,结束,所以进行循环读取的话,判断file.read()返回值是不是-1即可。

    int b ; // 循环读取 while ((b = fis.read())!=-1) { System.out.println((char)b); }

    三、字符流

    字符流的功能实现和字节流差不多,那为什么不用字节流呢?先看一个例子:

    public class Test01 { public static void main(String[] args) throws IOException { // File file = new File("a.txt"); // FileOutputStream fos = new FileOutputStream("a.txt"); // byte[] bytes = "我很帅".getBytes(); // fos.write(bytes); FileInputStream fis = new FileInputStream("a.txt"); int read; while((read = fis.read()) != -1 ){ System.out.println((char)read); } fis.close(); } } 输出结果为: æ ˆ ‘ å ¾ ˆ å ¸

    是不是很奇怪,会出现这样的乱码,其实我是把汉字写进去了,那么一个汉字不是一个字节,所以二字节流是一个一个字节读取,所以就会出现 这样的乱码啊,这就由此引入了字符流,一个字符一个字符的读,这样就不会出错了。

    字节输入流:

    这里只是介绍了FileReader类,是抽象类Reader的实现类

    public class Test { public static void main(String[] args) { FileReader fr = null; try { fr = new FileReader("a.txt"); int len; char[] ch = new char[3]; while ((len=fr.read(ch))!=-1){ System.out.println(new String(ch)); } } catch (IOException e) { e.printStackTrace(); } finally { try { if(fr != null){ fr.close(); } } catch (IOException e) { e.printStackTrace(); } } } } 字节输出流 public class Test { public static void main(String[] args) { FileReader fr = null; FileWriter fw = null; try { fr = new FileReader("a.txt"); fw = new FileWriter("b.txt"); int len; char[] ch = new char[2]; while ((len=fr.read(ch))!=-1){ fw.write(ch); } } catch (IOException e) { e.printStackTrace(); }finally{ try { if(fr != null){ fr.close(); } if(fw != null){ fw.close(); } } catch (IOException e) { e.printStackTrace(); } } } }

    四、属性集

    4.1 概述

    java.util.Properties 继承于Hashtable ,来表示一个持久的属性集。它使用键值结构存储数据,每个键及其对应值都是一个字符串。该类也被许多Java类使用,比如获取系统属性时,System.getProperties 方法就是返回一个Properties对象。

    4.2 Properties类

    构造方法
    public Properties() :创建一个空的属性列表。
    基本的存储方法
    public Object setProperty(String key, String value) : 保存一对属性。public String getProperty(String key) :使用此属性列表中指定的键搜索属性值。public Set<String> stringPropertyNames() :所有键的名称的集合。 public class ProDemo { public static void main(String[] args) throws FileNotFoundException { // 创建属性集对象 Properties properties = new Properties(); // 添加键值对元素 properties.setProperty("filename", "a.txt"); properties.setProperty("length", "209385038"); properties.setProperty("location", "D:\\a.txt"); // 打印属性集对象 System.out.println(properties); // 通过键,获取属性值 System.out.println(properties.getProperty("filename")); System.out.println(properties.getProperty("length")); System.out.println(properties.getProperty("location")); // 遍历属性集,获取所有键的集合 Set<String> strings = properties.stringPropertyNames(); // 打印键值对 for (String key : strings ) { System.out.println(key+" -- "+properties.getProperty(key)); } } } 输出结果: {filename=a.txt, length=209385038, location=D:\a.txt} a.txt 209385038 D:\a.txt filename -- a.txt length -- 209385038 location -- D:\a.txt
    与流相关的方法
    public void load(InputStream inStream): 从字节输入流中读取键值对。

    参数中使用了字节输入流,通过流对象,可以关联到某文件上,这样就能够加载文本中的数据了。文本数据格式:

    filename=a.txt length=209385038 location=D:\a.txt

    加载代码演示:

    public class ProDemo2 { public static void main(String[] args) throws FileNotFoundException { // 创建属性集对象 Properties pro = new Properties(); // 加载文本中信息到属性集 pro.load(new FileInputStream("read.txt")); // 遍历集合并打印 Set<String> strings = pro.stringPropertyNames(); for (String key : strings ) { System.out.println(key+" -- "+pro.getProperty(key)); } } } 输出结果: filename -- a.txt length -- 209385038 location -- D:\a.txt
    Processed: 0.010, SQL: 9