//1. File file = new File(pathname); file.createNewFile(); //2. File file = new File(parentFile(File), fileName(String)); file.createNewFile(); //3. File file = new File(parentPath(String), fileName(String));
getName(),getAbsolutePath(),getParent(),length(),exists(),isFile(),isDirectory()
mkdir(),mkdirs(),delete()
演示
public class Test01 { public static void main(String[] args) { String name = "src\\1.txt"; int read = 0; try { FileInputStream fileInputStream = new FileInputStream(name); while((read = fileInputStream.read())!=-1){ System.out.print((char)read); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(); try { byte[] bytes = new byte[1024]; FileInputStream fileInputStream = new FileInputStream(name); while((read = fileInputStream.read(bytes))!=-1){ System.out.println(new String(bytes)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
实例
String name = "src\\1.txt"; try{ FileOutputStream fileOutputStream = new FileOutputStream(name); fileOutputStream.write("asdf".getBytes()); }catch(Exception e){ e.printStackTrace(); }
//1.第一种连接方式 int temp = 0; FileReader fileReader = new FileReader("1.txt"); while((temp = fileReader.read()) != -1){ System.out.print((char)temp); } fileReader.close(); //2.第二种两种方式 char[] temp = new char[8]; int i = 0; FileReader fileReader = new FileReader("1.txt"); while((i = fileReader.read(temp)) != -1){ System.out.print(new String(temp, 0, i)); } fileReader.close();
String thing = "哈哈哈"; FileWriter j = new FileWriter("1.txt", true); j.write(thing.toCharArray()); j.close();
bufferedReader和bufferedWriter是按照字符操作,不要去操作二进制文件例如声音,视频,pdf,doc文档等
BufferedReader(包装reader实现类的节点流)
String name = "1.txt"; //创建字节流 FileReader reader = new FileReader(name); BufferedReader buffer = new BufferedReader(reader); String str; int count = 0; while((str = buffer.readLine())!=null){ System.out.println(str); } //关闭包装流,里边的字节流底层自动关闭 buffer.close();
BufferedWriter(包装writer实现类的节点流)
String name = "1.txt"; FileWriter writer = new FileWriter(name, true); BufferedWriter buffer = new BufferedWriter(writer); buffer.write("你好爱豆if哈子双方都一杭州市地方"); buffer.newLine(); buffer.write("你好爱豆if哈子双方都一杭州市地方"); buffer.newLine(); buffer.write("你好爱豆if哈子双方都一杭州市地方"); buffer.newLine(); buffer.flush();
BufferedInputStream
BufferedOutputStream
实例
String s1 = "1.txt";//源文件 String s2 = "4.txt";//拷贝文件 FileInputStream fileInputStream = new FileInputStream(s1); FileOutputStream fileOutputStream = new FileOutputStream(s2); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); byte[] bytes = new byte[1024]; int len = 0; while((len = bufferedInputStream.read(bytes)) != -1){ bufferedOutputStream.write(bytes, 0, len); } bufferedInputStream.close(); bufferedOutputStream.close();
序列化就是在保存数据时,保存数据的值和数据类型
反序列化就是在恢复数据时,恢复数据的值和数据类型
如果需要类可序列化,需要该类实现以下两个接口之一
String name = "5.txt"; ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(name)); objectOutputStream.writeInt(100); objectOutputStream.writeBoolean(true); objectOutputStream.writeChar('a'); objectOutputStream.writeUTF("wangafasdf"); objectOutputStream.close();
String name = "5.txt"; ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(name)); System.out.println(objectInputStream.readInt()); System.out.println(objectInputStream.readBoolean()); System.out.println(objectInputStream.readChar()); System.out.println(objectInputStream.readUTF()); objectInputStream.close();
转换流InputStreamReader和OutputSteamWriter
InputStreamReader:Reader的子类,可以将InputStream包装成Reader
OutputStreamWriter:Writer的子类,实现将OutputStream包装成Writer
档处理纯文本数据时,如果使用字符流效率更高,并且可以有效解决中文的问题,所以建议将字节流转换成字符流
可以在使用指定编码格式比如utf-8,gbk,gb2312,ISO8859-1
String name = "6.txt"; BufferedReader j = new BufferedReader(new InputStreamReader(new FileInputStream(name), "gbk")); String str; while((str = j.readLine()) != null){ System.out.println(str); } j.close(); //说明:文件是gbk编码,idea是utf-8,所以使用转化流转换
String name = "7.txt"; BufferedWriter k = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(name), "utf-8")); k.write("和嘿嘿"); k.close(); //将字符串以utf-8的方式存储
String name = "8.txt"; System.setOut(new PrintStream(name)); PrintStream i = System.out; i.println("afsasdfads"); i.close(); //改变System.out指向,字符打印到文件中
String name = "9.txt"; PrintWriter i = new PrintWriter(new PrintWriter(name)); i.println("afsasdfads"); i.close();
String name = "db.properties"; Properties properties = new Properties(); properties.load(new FileReader(name)); properties.list(System.out); System.out.println(properties.getProperty("username"));
当文件没有时为创建,有文件时为修改
String name = "new.properties"; Properties properties = new Properties(); properties.setProperty("username", "谁谁谁"); properties.setProperty("password", "123456"); properties.store(new FileWriter(name), null);