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(); } } } }java.util.Properties 继承于Hashtable ,来表示一个持久的属性集。它使用键值结构存储数据,每个键及其对应值都是一个字符串。该类也被许多Java类使用,比如获取系统属性时,System.getProperties 方法就是返回一个Properties对象。
参数中使用了字节输入流,通过流对象,可以关联到某文件上,这样就能够加载文本中的数据了。文本数据格式:
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