一、使用I/O操作文件
1、java的流
流:是指一连串流动的字符,是以先进先出的方式发送和接收数据的通道。
2、流分为输入流和输出流;这些输入/输出流类的对象称为对象流;
输入流:数据输入到内存——>关联的是源数据——>读取文件
输出流:内存中输出———>关联目标数据——->写入文件
3、创建目录与文件删除等方法
public static void main(String[] args) {Lianxi lx=new Lianxi();lx.creatFile( "1.txt","D:/newFile");lx.write("1.txt", "D:/newFile", "abcdefg");lx.getSize("1.txt", "D:/newFile");lx.read("1.txt", "D:/newFile");lx.delete("3.txt", "D:/file2");lx.deletePath("D:/file3");}//删除目录private void deletePath(String path){File file=new File(path);if(file.exists()){file.delete();}}//删除文件private void delete(String fileName,String path){File file=new File(path+"/"+fileName);if(file.exists()){file.delete();}}//读取字节private void read(String fileName,String path){FileInputStream fos=null;try {fos=new FileInputStream(path+"/"+fileName);System.out.println("可读取的字节数是:"+fos.available()+"个");int a;while((a=fos.read())!=-1){System.out.println((char)a);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{if(fos!=null){try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}//获取修改时间private void getDate(String fileName,String path){File file=new File(path+"/"+fileName);long l=file.lastModified();Date date=new Date();date.setTime(l);SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(date));}//获取文件大小private void getSize(String fileName,String path){File file=new File(path+"/"+fileName);System.out.println("文件大小:"+file.length());}//写入文件private void write(String fileName,String path,String word){FileOutputStream fos=null;try {fos=new FileOutputStream(path+"/"+fileName);fos.write(word.getBytes(), 0, word.length());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{if(fos!=null){try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}//获得路径private void getPath(String fileName,String path){File filePath=new File(path+"/"+fileName);System.out.println("相对"+filePath.getPath());System.out.println("绝对"+filePath.getAbsolutePath());}//创建文件夹和文件private void creatFile(String fileName,String path){File filePath=new File(path);if(!filePath.isDirectory()){if(!filePath.exists()){//创建一个filePath.mkdirs();}}File file=new File(path+"/"+fileName);if(!file.exists()){try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}}
二、字节流主方法读写文件
/** 读取文件*/public static void main(String[] args) {FileInputStream fis=null;//输入流读取文件try {fis=new FileInputStream("D:/file2/1.txt");System.out.println("可读取的字节数:"+fis.available());//循环读取文件内容int data;//data=-1,读取完成while((data=fis.read())!=-1){System.out.println((char)data);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{//关闭流if(fis!=null){try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}
5、主方法写入文件
/** 写入文件*/public static void main(String[] args) {FileOutputStream fos=null;try {//true 表示追加内容,不写true就是直接覆盖fos=new FileOutputStream("D:/file2/1.txt",true);String s="def";fos.write(s.getBytes(),0,s.length());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{if(fos!=null){try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}
5、file2/1.txt复制到file2/3.txt
将某文件的内容复制到新开辟的文件夹的文件内
public static void main(String[] args) {FileInputStream fis=null;FileOutputStream fos=null;try {fis=new FileInputStream("D:/file2/1.txt");fos=new FileOutputStream("D:/file2/3.txt");//循环读取文件内容int data;//data=-1,读取完成while((data=fis.read())!=-1){fos.write(data);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{if(fos!=null){try {fos.close();} catch (IOException e) {e.printStackTrace();}}//关闭流if(fis!=null){try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}
三、字符流读写文件

1、读文件
public static void main(String[] args) {FileInputStream fis=null;InputStreamReader isr=null;BufferedReader br=null;//自带缓冲区去读文件try {fis=new FileInputStream("D:/file/a.txt");isr=new InputStreamReader(fis);//如果出乱码,在fis后面,“utf-8”或者“GBK”br=new BufferedReader(isr);//读取文件中一行数据String s=br.readLine();while(s!=null){System.out.println(s);s=br.readLine();}System.out.println("读取完毕");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try{if(br!=null){br.close();}if(isr!=null){isr.close();}if(fis!=null){fis.close();}}catch(IOException e){e.printStackTrace();}}}
2、写文件
public static void main(String[] args) {FileOutputStream fos=null;OutputStreamWriter osw=null;BufferedWriter bw=null;try {fos=new FileOutputStream("D:/file/b.txt");osw=new OutputStreamWriter(fos);bw=new BufferedWriter(osw);bw.write("3.15");bw.newLine();//另起一行换行在写bw.write("名表维修-消磁天价收费");bw.newLine();bw.write("智联招聘企业账号可以随意下载");//刷新缓冲区bw.flush();System.out.println("写入完成");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try{if(bw!=null){bw.close();}if(osw!=null){osw.close();}if(fos!=null){fos.close();}}catch(IOException e){e.printStackTrace();}}}
1、复制文件的几种方法
public static void main(String[] args) {CopyFile cf=new CopyFile();// String sourceFile="D:/file/b.txt";// String targetFile="D:/file/6.txt";String sourceFile="D:/myDoc/1.png";String targetFile="D:/file/1-copy.png";cf.copy6(sourceFile, targetFile);}//复制图片--->二进制流private void copy6(String sourceFile,String targetFile){FileInputStream fis=null;DataInputStream dis=null;//二进制流FileOutputStream fos=null;DataOutputStream dos=null;try {//输入流读文件fis=new FileInputStream(sourceFile);dis=new DataInputStream(fis);fos=new FileOutputStream(targetFile);dos=new DataOutputStream(fos);int data;//读while((data=dis.read())!=-1){//写dos.write(data);}System.out.println("图片复制成功");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try{if(dos!=null){dos.close();}if(fos!=null){fos.close();}if(dis!=null){dis.close();}if(fis!=null){fis.close();}}catch(IOException e){e.printStackTrace();}}}//含有中文的复制private void copy5(String sourceFile,String targetFile){FileInputStream fis=null;InputStreamReader isr=null;BufferedReader br=null;//自带缓冲区去读文件//FileOutputStream fos=null;OutputStreamWriter osw=null;BufferedWriter bw=null;try {fis=new FileInputStream(sourceFile);isr=new InputStreamReader(fis);//如果出乱码,在fis后面,“utf-8”或者“GBK”br=new BufferedReader(isr);//fos=new FileOutputStream(targetFile);osw=new OutputStreamWriter(fos);bw=new BufferedWriter(osw);//读取文件中一行数据String s=br.readLine();while(s!=null){//写入bw.write(s);bw.newLine();s=br.readLine();}bw.flush();System.out.println("复制成功");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try{if(bw!=null){bw.close();}if(osw!=null){osw.close();}if(fos!=null){fos.close();}if(br!=null){br.close();}if(isr!=null){isr.close();}if(fis!=null){fis.close();}}catch(IOException e){e.printStackTrace();}}}//文件大适合private void copy4(String sourceFile,String targetFile){FileInputStream fis=null;FileOutputStream fos=null;try {fis=new FileInputStream(sourceFile);fos=new FileOutputStream(targetFile);byte[] b=new byte[1024];int n=0;//去除占用空间while((n=fis.read(b,0,b.length))!=-1){fos.write(b,0,n);}System.out.println("操作成功");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try{if(fos!=null){fos.close();}if(fis!=null){fis.close();}}catch(IOException e){e.printStackTrace();}}}//文件居中适合private void copy3(String sourceFile,String targetFile){FileInputStream fis=null;FileOutputStream fos=null;StringBuffer s=new StringBuffer();//读文件,读取到Stringtry {fis=new FileInputStream(sourceFile);int data;//fis.read()读文件while((data=fis.read())!=-1){s.append((char)data);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{if(fis!=null){try {fis.close();} catch (IOException e) {e.printStackTrace();}}}//写文件try {fos=new FileOutputStream(targetFile);fos.write(s.toString().getBytes());} catch (Exception e) {e.printStackTrace();}finally{if(fos!=null){try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}private void copy2(String sourceFile,String targetFile){FileInputStream fis=null;FileOutputStream fos=null;String s="";//读文件,读取到Stringtry {fis=new FileInputStream(sourceFile);int data;//fis.read()读文件while((data=fis.read())!=-1){s+=(char)data;}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{if(fis!=null){try {fis.close();} catch (IOException e) {e.printStackTrace();}}}//写文件try {fos=new FileOutputStream(targetFile);fos.write(s.getBytes());} catch (Exception e) {e.printStackTrace();}finally{if(fos!=null){try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}//文件小适用private void copy1(String sourceFile,String targetFile){//读取文件FileInputStream fis=null;FileOutputStream fos=null;try {fis=new FileInputStream(sourceFile);fos=new FileOutputStream(targetFile);int data;//fis.read()读文件while((data=fis.read())!=-1){//fos.write(data)写入文件fos.write(data);}System.out.println("文件复制成功");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{if(fos!=null){try {fos.close();} catch (IOException e) {e.printStackTrace();}}if(fis!=null){try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}
四、利用二进制字符流复制图片
//复制图片--->二进制流private void copy6(String sourceFile,String targetFile){FileInputStream fis=null;DataInputStream dis=null;//二进制流FileOutputStream fos=null;DataOutputStream dos=null;try {//输入流读文件fis=new FileInputStream(sourceFile);dis=new DataInputStream(fis);fos=new FileOutputStream(targetFile);dos=new DataOutputStream(fos);int data;//读while((data=dis.read())!=-1){//写dos.write(data);}System.out.println("图片复制成功");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try{if(dos!=null){dos.close();}if(fos!=null){fos.close();}if(dis!=null){dis.close();}if(fis!=null){fis.close();}}catch(IOException e){e.printStackTrace();}}}
五、序列化和反序列化
先写入再输出
FileOutputStream fos=null;
ObjectOutputStream oos=null;
FileInputStream fis=null;
ObjectInputStream ois=null;
1、首先类被实例化之前,要实现接口Serializable
import java.io.Serializable;///*要想Student被序列化,必须实现Serializable接口**/public class Student implements Serializable {/*** -8173462577973577860L---->ID号*/private static final long serialVersionUID = -8173462577973577860L;private String name;private Integer age;//由transient 关键字修饰的属性不被序列化private transient String pwd;public Student(){}public Student(String name, Integer age, String pwd) {super();this.name = name;this.age = age;this.pwd = pwd;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}}
2、序列化
程序(内存)———>(存储)文件
关键代码:实例化对象
//※序列化操作(自动加密)
oos.writeObject(list);
如果想要属性限制被序列化,用transient来修饰就可以
public static void main(String[] args) {FileOutputStream fos=null;ObjectOutputStream oos=null;try {fos=new FileOutputStream("D:/file/stu.txt");oos=new ObjectOutputStream(fos);//实例化对象Student stu1=new Student("莉莉",18,"123123");Student stu2=new Student("明明",19,"123123");Student stu3=new Student("壮壮",20,"123123");List<Student> list=new ArrayList<Student>();list.add(stu1);list.add(stu2);list.add(stu3);//※序列化操作(自动加密)oos.writeObject(list);System.out.println("序列化完成");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try{if(oos!=null){oos.close();}if(fos!=null){fos.close();}}catch(IOException e){e.printStackTrace();}}}
3、反序列化
文件中的对象——>程序(内存)
//※反序列化操作
List
public static void main(String[] args) {FileInputStream fis=null;ObjectInputStream ois=null;try {fis=new FileInputStream("D:/file/stu.txt");ois=new ObjectInputStream(fis);//※反序列化操作List<Student> list=(ArrayList<Student>)ois.readObject();// Student stu=(Student)ois.readObject();for (Student stu1 : list) {System.out.println("学生姓名:"+stu1.getName());System.out.println("年龄:"+stu1.getAge());System.out.println("密码:"+stu1.getPwd());}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}finally{try{if(ois!=null){ois.close();}if(fis!=null){fis.close();}}catch(IOException e){e.printStackTrace();}}}
六、java反射机制
1、/
(记住这句话即可)java的反射 可以动态的创建类,并且动态的获得类的属性和方法
还可以动态的调用和执行相应的方法
/
2、stu. eclipse 动态获取该类的属性和方法
3、//获取反射类的方法
(1)、getClass()—->创建了对象的情况(了解即可)
(2)、类名.class——>不创建对象的情况
(3)、(用的更多).Class.forName(“全类名”);
4、//获取clazz对象的属性和方法(私有属性无法获取)
Field[] fields=clazz.getFields();//返回属性数组,私有属性反射获取不到
5、clazz=Class.forName(“cn.bdqn.reflect.Student”);括号里面是类所在的全部地址
6、
/** (记住这句话即可)java的反射 可以动态的创建类,并且动态的获得类的属性和方法* 还可以动态的调用和执行相应的方法*/public static void main(String[] args) {////Object o=new Object();//stu. eclipse 动态获取该类的属性和方法//获取反射类的方法//(了解即可)1.getClass()--->创建了对象的情况// Student stu=new Student();// Class clazz=stu.getClass();//2.类名.class---->不创建对象的情况// Class clazz=Student.class;//(用的更多)3.Class.forName("全类名");Class clazz=null;try {clazz=Class.forName("cn.bdqn.reflect.Student");} catch (ClassNotFoundException e) {e.printStackTrace();}System.out.println("---------获取属性---------");//获取clazz对象的属性和方法(私有属性无法获取)Field[] fields=clazz.getFields();//返回属性数组,私有属性反射获取不到for (Field field : fields) {System.out.println(field.getName());}System.out.println("----------获取方法--------------");//获取方法Method[] methods=clazz.getMethods();for (Method method : methods) {System.out.println(method.getName());}}
7、反射还可以动态的调用和执行相应的方法
例如:setName再getName
/** 反射还可以动态的调用和执行相应的方法*/public static void main(String[] args) {// 1Student反射类创建好Class clazz=null;clazz=Student.class;//getMethod("方法名", 参数)try {//2newInstance 获取clazz的实例对象Student stu=(Student)clazz.newInstance();//3Method m=clazz.getMethod("setName", String.class);//4调用方法m.invoke(stu, "李雷");//setName需要传参,getName不需要传参,所以是nullMethod m1=clazz.getMethod("getName", null);//调用方法Object o=m1.invoke(stu, null);System.out.println(o);} catch (NoSuchMethodException e) {e.printStackTrace();} catch (SecurityException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();}}
8、拓展
main方法内{
T2.a(“a”,”b”);
}
private static void a(String…s){
//拓展,…表示可以传入1个或者多个参数
System.out.println(s[0]);
System.out.println(s[1]);
}
9、线程安全性
public static void main(String[] args) {//HashMap线程不安全Map map1=new HashMap();//Hashtable线程安全(全锁)Map map2=new Hashtable();//ConcurrentHashMap线程安全 (1.7分段锁,1.8自旋锁CAS+synchronized)Map map3=new ConcurrentHashMap();}
