JAVA 文件与IO流的使用

来源:互联网 发布:淘宝网怎样修改评价 编辑:程序博客网 时间:2024/05/16 05:57

  File类常见方法

  createNewFile() 只能创建文件,不能创建路径

  exists() 文件或者路径是否存在

  mkdir()创建路径 只能有一级路径没有使用时创建

  mkdirs()  多级目录没有,创建

  delete() 删除文件

  deleteOnExit() 删除目录

  文件简单操作例子:

public class TestFile {public static void main(String[] args) {        File dir=new File("F:\\Desktop\\其他\\新建文件夹/w"); File f1=new File(dir,"英雄联盟.jpg");if(!dir.exists()){   dir.mkdirs();  try {f1.createNewFile();System.out.println("创建成功");   } catch (IOException e) {e.printStackTrace();System.out.println("创建失败");   }}System.out.println(f1.getParent());System.out.println(f1.getName());System.out.println(f1.getPath());System.out.println(f1.length());//f1.delete();//System.out.println("删除文件成功");//dir.deleteOnExit();//System.out.println("删除目录成功");}}

IO

一个IO流的步骤 通道建立,写或读,关闭

写:输出流 从程序往外走,比如在显示器上输出  out

读:输入流 写进程序  in

读文件的流程:文件-----输入(读in)-------->   程序    ------输出(写Out)------> 文件

把一个文件内容读到另一个文件例子:

public class FileInpSTest {public static void main(String[] args) {// 1 建立通道//方向 :输入流(读),单位:字节, 节点流FileInputStream fis=null;FileOutputStream fos=null;try { fis=new FileInputStream("sub1/aa.txt"); fos=new FileOutputStream("sub1/bb.txt");  //2.读写  read(in, 0, in.length)把fis的内容按in数组大小读到in数组里 如果fis比in 大 就采用循环 //while判定是否读完 //返回的是读进去的长度 如果fis比in还小 返回的仍然是文件的大小 不是in的大小 //write(in, 0, len)是把in里的内容 从 0 开始到in.length写到fos里 //使用len 使读多少,写多少 byte[] in=new byte[1024]; int len; while((len=fis.read(in, 0, in.length))!=-1){ fos.write(in, 0, len); fos.flush(); }} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try {//3.close  关闭放在finally里fis.close();fos.close();} catch (IOException e) {e.printStackTrace();}}}}

各种  流

节点流:直接连源文件,一个字节一个字节的读

过滤流 :越来越大 对普通流的扩展,普通数据类型读写操作

字节流:存储任何东西(图片,视频)           **stream

字符流:writer reader

输入流:inputstream reader

输出流:outputstream writer

操作对象的流ObjectInputStream

例子:

public class TestObjectStream {private Map<Integer, Stu> map;private String filename;public TestObjectStream(Map<Integer, Stu> map, String filename) {super();this.map = map;this.filename = filename;}public TestObjectStream() {super();}public static void main(String[] args) {TestObjectStream tos=new TestObjectStream(null, "/stus.dat");try {tos.read();tos.add(new Stu("aa6",6));tos.write();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}System.out.println(tos.map);}public void add(Stu s){map.put(s.getSid(), s);}public void del(Stu s) {map.remove(s.getSid());}public  void read() throws FileNotFoundException, IOException {File file=new File(this.filename);if(!file.exists()){this.map=new HashMap<>();}else{ObjectInputStream ois=new ObjectInputStream(new FileInputStream(this.filename));try {this.map=(Map<Integer, Stu>) ois.readObject();ois.close();} catch (ClassNotFoundException e) {e.printStackTrace();}}}public void write() throws FileNotFoundException, IOException{ObjectOutputStream ois=new ObjectOutputStream(new FileOutputStream(this.filename));    ois.writeObject(map);    ois.close();}}

读取资源文件

public class TestProptis {public static void main(String[] args) {Properties p = new Properties();InputStream is = null;PrintStream ps = null;try {// is = new FileInputStream("sub1/data.properties");// 在src下is = TestProptis.class.getClassLoader().getResourceAsStream("data.properties");// 使用load()方法和资源文件建立联系p.load(is);URL url =TestProptis.class.getClassLoader().getResource("data.properties");ps = new PrintStream(new File(url.toURI()));p.setProperty("hiredate", "2017-01-01");p.list(ps);System.out.println(p.getProperty("id"));System.out.println(p.getProperty("name"));System.out.println(p.getProperty("sex"));System.out.println(p.getProperty("hiredate"));} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (URISyntaxException e) {e.printStackTrace();} finally {try {is.close();ps.close();} catch (IOException e) {e.printStackTrace();}}}}


原创粉丝点击