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();}}}}
阅读全文
0 0
- JAVA 文件与IO流的使用
- IO与文件读写---Java的IO流架构
- IO与文件读写---Java的IO流架构
- IO与文件读写---Java的IO流架构
- Java中使用IO流实现大文件的分裂与合并
- Java中使用IO流实现大文件的分割与合并
- JAVA中使用IO流实现大文件的分拆与合并
- Java中使用IO流实现大文件的分裂与合并
- JAVA中使用IO流实现大文件的分裂与合并
- Java IO 文件与文件流输入输出
- Java ,IO流,文件的分割与合并
- 【Java基础专题】IO与文件读写---Java的IO流架构
- Java中高级IO流的使用-Copy文件
- JAVA IO使用高速缓冲区的字节流复制文件
- Java IO流之File文件类的使用
- Java文件IO的使用总结
- 使用Java Io文件的读写
- JAVA笔记---IO流与文件读写
- 转载:Spring Boot (十):邮件服务
- vmware虚拟机三种网卡
- STL之list操作
- 新手上手Tensorflow之手写数字识别应用(3)
- hadoop完全分布式集群搭建全部流程之三:完全分布式集群搭建
- JAVA 文件与IO流的使用
- Git cherry-pick后再merge出现一个“奇怪”的现象
- 给RecyclerView设置分隔距离,和CarView的属性
- java静态初始化块、实例初始化块
- oracle 基本命令(一)
- 内存寻址之分页机制
- 1018. 锤子剪刀布 (20)
- 转载:Spring Boot (十一):Spring boot中mongodb的使用
- 通过cmd命令查看某个端口的进程pid和并关闭该进程的操作