一、文件切片
-----------------------------------------------------
@Test
/**
* 测试文件切片
* @throws Exception
*/
public void tsCutFile() throws Exception
{
cutFile("D:\\test\\新建文件夹\\d.txt",3);
}
/**
*文件切片
* @throws Exception
*/
public void cutFile(String filePath,int block) throws Exception
{
File file = new File(filePath);
if(file != null && file.exists() && file.isFile())
{
long len = file.length();
long partlen = len / block;
long remainlen = len % block;
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = null;
for(int i = 0 ; i < block ; i++)
{
int totleBytes = (int)partlen;
if(i == (block - 1))
{
totleBytes = (int)partlen + (int)remainlen;
}
fos = new FileOutputStream(new File(file.getParent(),file.getName() + "-part-" + i));
byte [] buf = new byte[1024];
int count = totleBytes / buf.length;
int remain = totleBytes % buf.length;
for (int j = 0; j < count; j++) {
fis.read(buf);
fos.write(buf);
}
fis.read(buf,0,remain);
fos.write(buf,0,remain);
fos.close();
}
fis.close();
}
}
二、随机访问文件RandomAccessFile
-----------------------------------------------------
1.seek:游标
2.自身具备读写功能
3.通过skipBytes(intx) 和 seek(int x)来达到随机访问的目的
4.getFilePointer()
5.setLength();设置文件长度
@Test
/**
* 测试RandomAccessFile
* @throws Exception
*/
public void teRandomAccessFile() throws Exception
{
RandomAccessFile ras = new RandomAccessFile("D:\\test\\新建文件夹\\d.txt", "rw");
byte [] buf = new byte[1024];
ras.seek(3);
int len = 0;
while((len = ras.read(buf)) != -1)
{
for(int i = 0 ; i < len ; i ++)
{
System.out.print((char)buf[i]);
}
}
ras.seek(ras.getFilePointer() - 1);
while((len = ras.read(buf)) != -1)
{
for(int i = 0 ; i < len ; i ++)
{
System.out.print((char)buf[i]);
}
}
ras.close();
}
三、多线程复制MultiThreadCopier
-----------------------------------------------------
/**
*多线程复制器
*/
public class MultiThreadCopier {
public static void main(String[] args) throws Exception {
//copier("D:\\Downloads\\BDYDownloads\\day18-集合框架.pptx","E:\\a.pptx");
copier("D:\\Downloads\\JLDownloads\\DR7-PLUS.7z","E:\\a.7z");
}
/**
* 多线程复制器MultiThreadCopier
* @throws Exception
*/
public static void copier(String srcFile , String desFile) throws Exception
{
File sf = new File(srcFile);
//线程数
int threadCount = 5;
//文件总大小
long fileLen = sf.length();
//分块大小
long partSize = fileLen / threadCount;
//分块剩余大小
long remainSize = fileLen % threadCount;
for(int i = 0 ; i < threadCount ; i ++)
{
RandomAccessFile srcRaf = new RandomAccessFile(sf, "r");
RandomAccessFile desRaf = new RandomAccessFile(desFile, "rw");
Thread t = null;
if( i == threadCount - 1)
{
t = new copyThread(srcRaf, desRaf, i * partSize , partSize + remainSize);
}
else
{
t = new copyThread(srcRaf, desRaf, i * partSize , partSize);
}
t.start();
}
}
}
class copyThread extends Thread
{
private RandomAccessFile srcRas;
private RandomAccessFile desRas;
private long seek = 0;
private long seekSize = 0;
public copyThread(RandomAccessFile srcRas, RandomAccessFile desRas,
long seek, long seekSize) {
super();
this.srcRas = srcRas;
this.desRas = desRas;
this.seek = seek;
this.seekSize = seekSize;
}
public void run()
{
try {
srcRas.seek(seek);
desRas.seek(seek);
byte [] buf = new byte [1024];
long count = seekSize / buf.length;
int remain = (int)(seekSize % buf.length);
for (int i = 0; i < count; i++) {
srcRas.read(buf);
desRas.write(buf);
}
srcRas.read(buf,0,remain);
desRas.write(buf,0,remain);
srcRas.close();
desRas.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
四、操作对象的流:ObjectInputStream / ObjectOutputStream
-----------------------------------------------------------
五、getClass/instanceof
-----------------------------------------------------------
1.getClass:单类匹配,精准判断,不考虑继承
2.instanceof:家族匹配,带家族树的判断,考虑继承
@Test
//测试getClass和instanceof
public void tsGetClass()
{
Jing8 j = new Jing8("j");
Dog d = new Dog("d");
Class clazz = j.getClass();
//精准判断
System.out.println((clazz = j.getClass()) == Dog.class);
//带继承关系树,家族判断
System.out.println(d instanceof Dog);
}
六、序列化(串行化) / 反序列化
-----------------------------------------------------------
1.序列化:将java对象转换成byte[],用于网络间传输或者本地化保存 ,相当于一种数据格式
2.反序列化:将被序列化的对象的byte[],重新转换成java对象
3.但是注意,被转换的对象所属的类,必须实现序列化接口Serializable,对象才可以序列化
4.序列化对象的时候,请注意生成序列化id:serialVersionUID。否则当类代码发生改变的时候,serialVersionUID也会发生改变,就不能反序列化了
5.反序列化的时候,是不会走对象构造函数的,因为已经有字段和字段值得对应关系,没必要走构造赋值了
@Test
//测试序列化(把对象写到磁盘文件)
public void tsSeq() throws Exception
{
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
//Integer i = 1;
//Jing8 j = new Jing8("JJJJJ");
Dog d = new Dog("dog1");
Cat cat = new Cat("加菲");
Person p = new Person("张三");
d.setOwner(p);
d.setPartner(cat);
cat.setOwner(p);
//FileOutputStream out = new FileOutputStream("d:\\a.dat");
//oos.writeObject(d);
//oos.writeObject(d);
oos.writeObject(d);
oos.writeObject(d);
oos.close();
bos.close();
byte [] bs = bos.toByteArray();
System.out.println(bs.length);
ByteArrayInputStream bis = new ByteArrayInputStream(bs);
ObjectInputStream ois = new ObjectInputStream(bis);
Object o = ois.readObject();
if(o instanceof Dog)
{
Dog d1 = (Dog)o;
System.out.println("0" + d1.getColor());
System.out.println("1" + d1.getPartner().getOwner());
System.out.println("2" + d1.getOwner());
}
Object o1 = ois.readObject();
if(o1 instanceof Dog)
{
Dog d1 = (Dog)o1;
System.out.println("0" + d1.getColor());
System.out.println("1" + d1.getPartner().getOwner());
System.out.println("2" + d1.getOwner());
}
} catch (Exception e) {
e.printStackTrace();
}
//out.close();
}
@Test
//测试反序列化
public void tsDeSeq() throws Exception
{
FileInputStream in = new FileInputStream("d:\\a.dat");
ObjectInputStream ois = new ObjectInputStream(in);
Object o = ois.readObject();
// if(o.getClass() == Dog.class)
// {
// Dog d = (Dog)o;
// System.out.println("0" + d.getName());
// }
// if(o.getClass() == Jing8.class)
// {
// Dog d = (Dog)o;
// System.out.println("1" + d.getName());
// }
if(o instanceof Dog)
{
Dog d = (Dog)o;
System.out.println("2" + d.getColor());
}
//System.out.println(o);
ois.close();
in.close();
}
七、深度复制
-----------------------------------------------------------
1.深度复制是指:复制整个对象图,对象能达到的关系对象都会复制
2.这样复制出来的对象和原来的对象就完全是两个对象,引用资源也不会冲突,因为是两份,复制了整个图
3.好处是复制了一个完全独立又完全一样的新对象,弊端是比较消耗内存,可以通过一些手段优化内存(不完全复制,没必要的个体不复制)。
4.要达到上述的不必要的内容不复制,就需要在不需要深度复制的字段前,添加transient关键字(临时对象,不串行化)