new File(String pathname)
//根据路径构建一个File对象new File(File parent,String child)
//根据父目录文件+子路径构建new File(String parent,String child)
//根据父目录+子路径构建// 方法1 new File(String filePath) @Test public void creatNewFile(){ String filePath = "D:\\news1.txt"; File file = new File(filePath); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } System.out.println("文件创建成功"); }
public void info(){ String filePath = "D://news5.txt"; File file = new File(filePath); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } System.out.println("文件名字="+file.getName()); System.out.println("文件绝对路径="+file.getAbsolutePath()); System.out.println("文件父级目录="+file.getParent()); System.out.println("文件大小(字节)="+file.length()); System.out.println("文件是否存在="+file.exists()); System.out.println("是不是一个文件="+file.isFile()); System.out.println("是不是一个目录="+file.isDirectory()); }
@Test public void m2(){ String filePath = "D:\\news3.txt"; File file = new File(filePath); if(file.exists()){ if(file.delete()){ System.out.println(filePath+"删除成功"); }else{ System.out.println(filePath+"删除失败"); } }else{ System.out.println("该目录不存在"); } }
file.mkdir
file.mkdirs
抽象基类 | 字节流 | 字符流 |
---|---|---|
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
(1)Java的IO流设计40多个类,实际上非常规则,都是从如上4个抽象基类派生的
(2)有这四个类派生出来的子类名称都是以其父类作为子类后缀
基本介绍
节点流和处理流的区别和联系
处理流的功能主要体现在以下两个方面:
模拟修饰器设计模式BufferedReader
Reader_抽象类:
public abstract class Reader_ { //抽象类 public void readString(){} public void readFile(){} }
两个节点流类实现抽象类:FileReader_、StringReader_:
public class FileReader_ extends Reader_{ public void readFile(){ System.out.println("对文件进行读取。。。"); } } public class StringReader_ extends Reader_{ public void readString(){ System.out.println("对字符串进行读取。。。"); } }
BufferedReader_也继承抽象类:
public class BufferedReader_ extends Reader_{ private Reader_ reader_;//属性是Reader_类型 //接收Reader_子类对象 public BufferedReader_(Reader_ reader_){ this.reader_=reader_; } //让方法更加灵活,多次读取文件 public void readFiles(int num){ for (int i = 0; i < num; i++) { reader_.readFile(); } } // 扩展 readString,批量处理字符串数据 public void readStrings(int num){ for (int i = 0; i < num; i++) { reader_.readString(); } } }
测试:
public class Test_ { public static void main(String[] args) { BufferedReader_ bufferedReader_ = new BufferedReader_(new FileReader_()); bufferedReader_.readFiles(10); BufferedReader_ bufferedReader_1 = new BufferedReader_(new StringReader_()); bufferedReader_1.readStrings(10); } }
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class BufferedWriter_ { public static void main(String[] args) throws IOException { String filePath = "D:\\ok.txt"; // 创建BufferedWriter //new FileWriter(filePath)表示以覆盖的方式写入 //new FileWriter(filePath,true)在节点流里加一个true,表示以覆盖的方式写入 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath,true)); bufferedWriter.write("哈哈哈哈"); //插入一个和系统相关的换行符 bufferedWriter.newLine(); bufferedWriter.close(); } }
总结:
//1、创建BufferedWriter对象: BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath,true)); //2、写入 bufferedWriter.write("哈哈哈哈"); bufferedWriter.newLine(); //3、关闭 bufferedWriter.close();
import java.io.*; public class BufferedCopy { public static void main(String[] args) { BufferedReader br = null; BufferedWriter bw = null; String line; try { br = new BufferedReader(new FileReader("D:\\ok.txt")); bw = new BufferedWriter(new FileWriter("D:\\ok1.txt")); //读取写入 while((line= br.readLine())!=null){ bw.write(line);//每读取一行,就写入 bw.newLine(); } } catch (IOException e) { e.printStackTrace(); }finally { if(bw!=null){ try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } if (br!=null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
import java.io.*; public class BufferedCopy2 { public static void main(String[] args) { //创建BufferedInputStream对象和BufferedOutputStream对象 BufferedInputStream bis = null; BufferedOutputStream bos = null; try { //因为FileInputStream是InputStream的子类 bis = new BufferedInputStream(new FileInputStream("D:\\壁纸1.jpg")); bos = new BufferedOutputStream(new FileOutputStream("D:\\bizi.jpg")); //循环读取,并写入 byte[] buff = new byte[1024]; int readLen = 0; while((readLen = bis.read(buff))!= -1){ bos.write(buff,0,readLen); } } catch (IOException e) { e.printStackTrace(); }finally { //关闭流,关闭外层流即可,底层会去关闭节点流 if(bis != null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if(bos != null){ try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
类型 | 默认设备 | |
---|---|---|
System.in标准输入 | InputStream | 键盘 |
System.out标准输出 | PrintStream | 显示器 |
Scanner scanner = new Scanner(System.in); String next = scanner.next();
System.out编译类型 PrintStream
System.out运行类型PrintStream
对象流ObjectInputStream和ObjectOutputStream
基本介绍:
功能:提供了对基本类型或对象类型的序列化和反序列化的方法
ObjectOutputStream 提供序列化功能
ObjectInputStream 提供反序列功能
序列化和反序列化
Serializable
//这是一个标记接口,没有方法Externalizable
//该接口有方法需要实现,因此我们一般实现的Serialzable接口注意事项和细节说明
序列化实例:
Dog类:
import java.io.Serializable; public class Dog implements Serializable { private String name; private int age; public Dog(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Dog{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
序列化:
import java.io.FileOutputStream; import java.io.ObjectOutputStream; //完成数据的序列化 public class ObjectStream_ { public static void main(String[] args) throws Exception { //序列化后,保存的文件格式不是文本,而是按照它的格式来保存 String filePath= "D:\\data.dat"; ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath)); oos.writeInt(100); //int->Integer实现了Serializable oos.writeBoolean(true); //boolean->Boolean实现了Serializable oos.writeChar('a'); //char->Character实现了Serializable oos.writeDouble(9.7);//double->Double实现了Serializable oos.writeUTF("哈哈哈啊哈哈"); //保存一个dog对象 oos.writeObject(new Dog("小罗",10)); oos.close(); } }
反序列化:
import java.io.FileInputStream; import java.io.ObjectInputStream; public class ObjectInputStream_ { public static void main(String[] args) throws Exception { String filePath="D:\\data.dat"; ObjectInputStream ios = new ObjectInputStream(new FileInputStream(filePath)); //读取(反序列化)的顺序要和保存数据(序列化)的顺序一致 System.out.println(ios.readInt()); System.out.println(ios.readBoolean()); System.out.println(ios.readChar()); System.out.println(ios.readDouble()); System.out.println(ios.readUTF()); System.out.println(ios.readObject()); //关闭流 ios.close(); } }
转换流InputStreamReader和OutputStreamWriter
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; public class InputStreamReader_ { public static void main(String[] args) throws Exception { String filePath="D:\\ok.txt"; //new FileInputStream(filePath)转成InputStreamReader,指定编码为utf-8 InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath),"utf-8"); //把InputStreamReader传入BufferedReader BufferedReader br =new BufferedReader(isr); String s=br.readLine(); System.out.println(s); br.close(); } }
import java.io.FileOutputStream; import java.io.OutputStreamWriter; //把字节流转成字符流 public class OutputStreamWriter_ { public static void main(String[] args) throws Exception { OutputStreamWriter gbk = new OutputStreamWriter(new FileOutputStream("D:\\ok1111.txt"),"utf8"); gbk.write("哈哈哈哈很好"); gbk.close(); } }
当处理纯文本数据时,如果使用字符里效率较高,并且可以有效解决中文问题,所以建议将字节流转换为字符流
可以在使用时指定编码格式
打印流PrintStream和PrintWriter
import java.io.IOException; import java.io.PrintStream; public class PrintStream_ { public static void main(String[] args) throws IOException { PrintStream out = System.out; //在默认的情况, PrintStream 输出数据的位置是 标准输出,即显示器 out.print("ddddddsssdss"); //因为print底层使用的是write,所以可以直接调用write进行打印/输出 out.write("1223sdsd".getBytes()); out.close(); //可以修改打印流输出的文职/设备 ,这里修改为D:\ok111.txt System.setOut(new PrintStream("D:\\ok111.txt")); System.out.write("sdsfsdf".getBytes()); System.out.print("ddddddsssdss"); out.close(); } }
import java.io.PrintWriter; public class PrintWriter_ { public static void main(String[] args) { //默认打印/输出在显示器 PrintWriter printWriter = new PrintWriter(System.out); //修改到文件 // PrintWriter printWriter = new PrintWriter(new FileWriter("D:\\aa.txt")); printWriter.print("哈哈哈"); printWriter.close();//flush+关闭,才会将数据写入到文件 } }
基本介绍
Properties的常用方法
Properties读取文件
import java.io.FileReader; import java.util.Properties; //使用Properties类来读取mysql.properties文件 public class Properties1 { public static void main(String[] args) throws Exception { // 1. 创建你Properties对象 Properties properties = new Properties(); //2、加载指定配置文件 properties.load(new FileReader("src\\mysql.properties")); //3、把k-v显式控制台 properties.list(System.out); // 4.根据key获取对应的值 String user = properties.getProperty("user"); System.out.println(user); } }
import java.io.FileOutputStream; import java.util.Properties; public class Properties2 { public static void main(String[] args) throws Exception { //使用Properties类来创建 配置文件,修改配置文件类容 Properties properties = new Properties(); //创建 //1、如果该文件没有key,就是创建 //2、如果该文件有key,就是修改 properties.setProperty("charset","utf8"); properties.setProperty("user","汤姆123"); properties.setProperty("pwd","abc111"); //将k-v存储文件中,后面的null可以用字符串来写注释 properties.store(new FileOutputStream("src\\mysql2.properties"),null); } }