io包的综合运用

package io;

import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessFileDemo implements FileFilter
{
public static void main(String[] args)
{
RandomAccessFileDemo r = new RandomAccessFileDemo();
File file1 = new File("G:\\a.txt");

// try
// {
// r.Read(file1);
// }
// catch (IOException e)
// {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// try
// {
// r.cut(file1);
// }
// catch (IOException e)
// {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// File f = new File("G:\\");
// try
// {
// r.connectFile(f.listFiles());
//
// }
// catch (IOException e)
// {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
try
{
r.compress(file1);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("ok");
}

/**
* 读文件
* 
* @param file
* @throws IOException
*/
public void Read(File file) throws IOException
{
RandomAccessFile raf = new RandomAccessFile(file, "r");
byte b;
b = raf.readByte();
System.out.println((char) b);

}

/**
* 把一个文件拆分为每个大小为16k小的文件
* 
* @param file
* @throws IOException
*/
public void cut(File file) throws IOException
{
RandomAccessFile raf = new RandomAccessFile(file, "r");
// 获取文件的长度
int n = (int) raf.length();
for (int i = 0; i < n / (16 * 1024); i++)
{// 从file中读取16k然后创建一个新的文件,写入新建的文件中
byte[] bt = new byte[16 * 1024];
raf.read(bt);
File newFile = new File("G:\\" + i + "CutFile.txt");
newFile.createNewFile();
RandomAccessFile rafWrite = new RandomAccessFile(newFile, "rw");
rafWrite.write(bt);
}
}

/**
* 删除文件名以a.txt结束的文件
*/
public void del()
{
File file = new File("G:\\");
File f[] = file.listFiles();
for (File file2 : f)
{
if (this.accept(file2))
file2.delete();
}
}

/**
* 拼接文件
* 
* @param file
* @throws IOException
*/
public void connectFile(File[] file) throws IOException
{
RandomAccessFile raf1, raf2;
File connect = new File("G:\\connect.txt");
connect.createNewFile();
raf2 = new RandomAccessFile(connect, "rw");

StringBuffer sbf = new StringBuffer();
for (File file2 : file)
{
if (accept(file2))
{
raf1 = new RandomAccessFile(file2, "rw");
for (int i = 0; i < raf1.length() / 2; i++)
{
sbf.append(raf1.readChar());
}
}
}

raf2.writeChars(new String(sbf));
}

/**
* 压缩文件,剔除空格
* 
* @param file
* @throws IOException
*/
public void compress(File file) throws IOException
{
/* 还有问题,无法到达目的 */
FileReader fr = new FileReader(file);
RandomAccessFile raf = new RandomAccessFile(file, "r");
char[] ch = new char[(int) raf.length() / 2];
fr.read(ch);
StringBuffer sbf = new StringBuffer();
for (int i = 0; i < ch.length; i++)
{
if (ch[i] != ' ')
sbf.append(ch[i]);

}
File f = new File("G:\\compressFile.txt");
f.createNewFile();
RandomAccessFile raf1 = new RandomAccessFile(f, "rw");
raf1.writeChars(new String(sbf));
}

/**
* 
*/
public boolean accept(File pathname)
{
if (pathname.getName().endsWith("File.txt"))
return true;
else
return false;
}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值