java IO

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]);
else
System.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 one
if(!file.exists()) {
file.createNewFile();
}
BufferedReader br = new BufferedReader(new FileReader(file));
//load the data from the file
props.load(br);

//declare a value to note the use time
int 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 file
BufferedWriter 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 vector
Vector<FileInputStream> vec = new Vector<FileInputStream>();
vec.add(new FileInputStream("info.txt"));
vec.add(new FileInputStream("conf.ini"));
//use the method elements() to get the enum
Enumeration<FileInputStream> en = vec.elements();

//create a sequenceInputStream contains more than one inputStream
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("add.ini");

//create a space to restore the data read from the file
byte[] 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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值