java IO

编程技术  /  houtizong 发布于 3年前   48
IO流
一、 列出指定目录下所有文件
import java.io.*;class IOTest1 {public static void main(String[] args) {File dir = new File("D:\\图库");new IOTest1().showDir(dir, 1);}public void showDir(File dir, int level) {File[] files = dir.listFiles();StringBuilder sb = new StringBuilder();sb.append("|--");for(int i = 1; i < level; i++) {sb.insert(0,"   ");}for(int i = 0; i < files.length; i++) {System.out.println(sb.toString() + files[i].getName());if(files[i].isDirectory()) { showDir(files[i], level + 1);}}}}

递归使用的两个条件:1、要有判断,能够让程序结束;2、注意递归运算的次数,避免内存溢出。
二、 删除文件夹下面所有目录及文件
import java.io.*;class DeleteDemo {public static void main(String[] args) {File dir = new File("d:\\Test");deleteAll(dir);}public static void deleteAll(File dir) {File[] files = dir.listFiles();for(int i = 0; i < files.length; i++) {if(files[i].isDirectory())deleteAll(files[i]);elseSystem.out.println(files[i].toString() + "==file==" + files[i].delete());}System.out.println(dir + "--dir--" + dir.delete());}}

三、 Properties类的使用(继承Hashtable,是集合类)
import java.util.*;class PropertiesTest {public static void main(String[] args) {getAndSet();}public static void getAndSet() {Properties prop = new Properties();prop.setProperty("qin","rui");prop.setProperty("c","yuan");//System.out.println(prop);Set<String> keys = prop.stringPropertyNames();for(String key : keys) {System.out.println(key + "==" + prop.getProperty(key));}}}

四、 Properties类结合IO流读写配置文件
import java.util.*;import java.io.*;class PropertiesIOTest {public static void main(String[] args) {Properties props = new Properties();InputStreamReader fr = null;try{fr = new InputStreamReader(new FileInputStream("D:\\java1\\IO\\info.txt"));//从文件中获取配置信息存入Properties中props.load(fr);getInfo(props);setInfo(props);getInfo(props);}catch (IOException e){e.printStackTrace();} finally {try {if(fr != null) {fr.close();fr = null;}}catch (IOException e){e.printStackTrace();}}}public static void getInfo(Properties props) {//使用stringPropertyNames()获取,配置信息的键,返回一个Set集合Set<String> keys = props.stringPropertyNames();for(String key : keys) {System.out.println(key + "==" + props.getProperty(key));}}//设置配置信息并存入文件中public static void setInfo(Properties props) throws IOException {props.setProperty("love","yuanrui");PrintWriter pw = new PrintWriter(new PrintStream("D:\\java1\\IO\\info.txt"));//使用store方法把更改的信息存入文件中,会自动调用list()方法props.store(pw,null);//关闭IO流是必须要做的操作,减少内在占用pw.close();}}

五、 软件使用计数器
import java.util.*;import java.io.*;class countDemo {public static void main(String[] args) throws IOException{Properties props = new Properties();File file = new File("conf.ini");//judge whether the file exist, if not creat a new oneif(!file.exists()) {file.createNewFile();}BufferedReader br = new BufferedReader(new FileReader(file));//load the data from the fileprops.load(br);//declare a value to note the use timeint count = 0;String value = props.getProperty("time");if(value != null) { count = Integer.parseInt(value);if(count >= 5) {System.out.println("您的使用时间快到!请注册后再继续使用!");return ;}}count++;props.setProperty("time",count + "");//declare a writer to save the data to the fileBufferedWriter bw = new BufferedWriter(new FileWriter(file));props.store(bw , null);System.out.println(props);}}

六、 使用SequenceInputStream将多个文件整合
import java.io.*;import java.util.*;class SequenceDemo {public static void main(String[] args) throws IOException{//save the inputstream in the vectorVector<FileInputStream> vec = new Vector<FileInputStream>();vec.add(new FileInputStream("info.txt"));vec.add(new FileInputStream("conf.ini"));//use the method elements() to get the enumEnumeration<FileInputStream> en = vec.elements();//create a sequenceInputStream contains more than one inputStreamSequenceInputStream sis = new SequenceInputStream(en);FileOutputStream fos = new FileOutputStream("add.ini");//create a space to restore the data read from the filebyte[] bt = new byte[1024];int len = 0;while((len = sis.read(bt)) > 0) {fos.write(bt, 0, len);}sis.close();fos.close();}}

七、 文件切割
import java.io.*;class SplitDemo {public static void main(String[] args) throws IOException{splitFile();}public static void splitFile() throws IOException {File file = new File("D:\\图库\\我的图片\\个人图库\\八里沟\\1-4.jpg");FileInputStream fis = new FileInputStream(file);FileOutputStream fos = null;int len = 0;int count = 1;byte[] bt = new byte[1024*1024];while((len = fis.read(bt)) != -1) {fos = new FileOutputStream("D:\\java1\\IO\\split\\" + (count++) + ".part");fos.write(bt, 0, len);fos.close();}if(fos != null)fos.close();fis.close();}}


上一篇:JSP 自定义标签
下一篇:java Collection

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客