java切割输入流_Java分割流,对象流,参与序列化,分割读取文件,对象输入或输出流等【诗书画唱】...

自己写的题目:用分割流把一个Java文件分割读取,打印出每次读取的内容,要求每次读取打印500个字节。

6dfcebe58487227948296c1cfd952325.png

package fenGeLiu;

import java.io.*;

public class lizi1 {

public static void main(String[] args) throws Exception{

//分割流:可以将大文件分割为小文件,然后在使用的时候,

//又可以将若干个小文件组合成一个大文件来使用

File File=new File("src/fenGeLiu/copy.java");

int zongDaXiao=(int)File.length();

System.out.println(zongDaXiao);

//startIndex:定义文件开始读取的位置下标,索引

int startIndex=0;

//lastIndexOrLength:默认每次读多少内容的长度或最后的索引

int lastIndexOrLength=500;

//ciShu:这个文件读的次数

int ciShu=(zongDaXiao/lastIndexOrLength)+1;

//遍历这个读的次数:

for(int i=0;i

startIndex=i*lastIndexOrLength;

//startIndex:最后一次读的内容

if(i!=ciShu-1){

//如果i不为可以达到的最大值,就一直-500(这里的lastIndex)

//为的是让lastIndexOrLength在最后可以求出其部分的最后的下标

//zongDaXiao:总大小

zongDaXiao-=lastIndexOrLength;

}else {

lastIndexOrLength=zongDaXiao;

}

System.out.println("第"+(i+1)+"次读取的内容为:");

//调用方法:

fenGeFangFa(File.getAbsolutePath(),

startIndex,lastIndexOrLength);

//File.getAbsolutePath()为路径

//startIndex:为开始的要截取部分的最小的索引,下标

//lastIndex:为最后的要截取部分的最大的索引,下标

}

/*File f=new File("copy.java");

File f1=new File("ww");

//执行一个位置,设置读取为只读

//1600个字节大小

//每次读300个,要读6次

System.out.println(f.length());

//求一下要读几次

int daxiao=(int)f.length();

int meicidu=600;

int start=0;

int duojici=(daxiao/meicidu)+1;

for(int i=0;i

start=meicidu*i;//0,600,1200.....6000 235

if(i!=(duojici-1)){//i!=10

daxiao-=meicidu;//6235-600-600-600...10=235

}else{

meicidu=daxiao;

}

System.out.println("第"+(i+1)+"次读取的内容为");

File ff=new File(f1,i+".java");

putong1(f,start,meicidu,ff);

}

*/

}

private static void fenGeFangFa(String FileSrc,

int startIndex,int length)

throws FileNotFoundException,

IOException {

RandomAccessFile readFengGe=new RandomAccessFile(FileSrc,"r");

readFengGe.seek(startIndex);

byte[] byteArray=new byte[length];

readFengGe.read(byteArray);

System.out.println(new String(byteArray));

readFengGe.close();

}

//下面的是可以在主函数直接调用的方法:

//private static void fenGeReadfangFa(File read,int startIndex,

//int lengthSize,File write) throws FileNotFoundException,

//IOException {

//

//

//

//

//RandomAccessFile readFenGe=new RandomAccessFile(read,"r");

r:是固定不可变的“key”一般的,代表read的部分

//RandomAccessFile writeFenGe=new RandomAccessFile(write,"rw");

//

rw:是固定不可变的“key”一般的,代表read后的write的部分

//readFenGe.seek(startIndex);//设置读取的位置是100的位置

//byte[] byteArray=new byte[lengthSize];

//int len;

//readFenGe.read(byteArray);

//writeFenGe.write(byteArray);

//

//readFenGe.close();

//}

}

af076e2a1addd685223e3925916e7de1.png

5d685bb49f6dc9aaae2d32987fc9e47a.png

储存对象的姓名等内容,之后就去读取和打印读取的内容(要求单个后单行等的打印,与用循环遍历的方法把储存到自己创建的new.txt的文件中的所有的对象【可以用“点某个文件,之后点鼠标的右键,之后点最后一个选项,之后就可以改文件的格式,让txt和java文件都为UTF-8等的格式,防止中文乱码,但设置“chengJi(成绩)”等变量为transient,用transient来让成绩(chengJi)不参与序列化,这样的话存在txt文件后就会仍出现乱码等,以上是个人的理解或猜测的仍出现乱码的原因等】:

2891875a3c12a660651e3b672e95d80e.png

package fenGeLiu;

import java.io.*;

import java.util.Date;

public class duiXangLiu {

public static void main(String[] args) throws Exception{

//ObjectInputStream:对象输入流

//ObjectOutputStream:对象输出流

//特点:

//以前的流可以存储文本,其他类型数据,但是不可以存储

//对象,这个流唯一特点就是可以存储对象。

//为什么要存储对象:

//因为在传输内容的时候,如果传输的是普通流,一旦

//内容被获取到,其他人可以通过转码将数据转会源码,

//但是对象流一旦被别人获取到,别人要需要将其转换为相应

//的对象格式,才可以看到里面的内容

student duiXiang1=new student(4,"诗书画唱",100.0f);

student duiXiang2=new student(5,"三连",99.9f);

student duiXiang3=new student(6,"关注",99.9f);

File f=new File("new.txt");

//传入地址

FileOutputStream fis=new FileOutputStream(f);

//传入字节流

ObjectOutputStream oos=new ObjectOutputStream(fis);

oos.writeObject(duiXiang1);

oos.writeObject(duiXiang2);

oos.writeObject(duiXiang3);

oos.writeObject(null);

oos.close();

System.out.println("存储成功");

//——————————————————————————————

读单个对象:

File FileOne=new File("new.txt");

FileInputStream byteInputOne=new FileInputStream(FileOne);

//传入字节输入流:

ObjectInputStream byteDuiXiangInput

=new ObjectInputStream(byteInputOne);

Object duiXiangOne=byteDuiXiangInput.readObject();

Object duiXiangTwo=byteDuiXiangInput.readObject();

//用“(student)”的转型,将读取到的内容转为原先的类型:

student duiXiangOneOld=(student)duiXiangOne;

student duiXiangTwoOld=(student)duiXiangTwo;

byteDuiXiangInput.close();

System.out.println("打印第一行的对象的内容为\t编号:"

+duiXiangOneOld.bianHao+";姓名 : "

+duiXiangOneOld.name+";成绩 : "+duiXiangOneOld.chengJi);

System.out.println("打印第二行的对象的内容为\t"

+" 编号: "

+duiXiangTwoOld.bianHao+";名字: "

+duiXiangTwoOld.name+";成绩"

+ "(被设置为不显示\n真实成绩,默认为0.0):"

+duiXiangTwoOld.chengJi+"\n");

//System.out.println(duiXiangTwo.toString());

//——————————————————————

//下面是打印所有的存在new.txt的文件的对象的内容:

File File=new File("new.txt");

//传入字节输入流:

FileInputStream byteInputAll=new FileInputStream(File);

//duiXiangInput【“拼音+英文”的自己的命名】(对象输入流)。

ObjectInputStream duiXiangInput

=new ObjectInputStream(byteInputAll);

Object duiXiang="";

while((duiXiang=duiXiangInput.readObject())!=null){

System.out.println(duiXiang.toString());

}

duiXiangInput.close();

}

}

//——————————————————————————

//下面为知识点:

//一个类可以实现多个接口,接口一旦被实现后就必须实现接口中的抽象方法

//serializable:

//英[ˈsɪərɪəlaɪzəbl]

//美[ˈsɪˌriəˌlaɪzəbl]

//[词典]【计】可串行化的;

//Serializable:序列化接口,它没有其他的作用,接口中也没有抽象方法

//transient关键字介绍:如果想让某个对象的某个字段不参与序列化,

//可以加上transient关键字进行修饰。

//transient英[ˈtrænziənt]

//美[ˈtrænʃnt]

//adj.短暂的; 转瞬即逝的; 倏忽; 暂住的; 过往的; 临时的;

//n.暂住某地的人; 过往旅客; 临时工;

//——————————————————————

class student implements Serializable{

int bianHao;

String name;

transient float chengJi;

//这里用transient来让成绩(chengJi)不参与序列化,

//就是后面用toString方法返回的时候,

//不会打印出传入的真实成绩放的内容。

public student(int bianHao, String name, float chengJi) {

this.bianHao = bianHao;

this.name = name;

this.chengJi = chengJi;

}

@Override

public String toString() {

return "student [bianhao=" +

bianHao + ", name=" + name + ", chengji("

+ "被transient设置为不能打印"

+ "\n出来的成绩默认为0.0):" +chengJi+ "]\n";

}

}

ab828c9bde49ee6162f123f887883751.png

这里的”行“可以理解为”个“的意思:4b924d6409e6625619ce660f089b18c1.png

68dbe85966402502d089c517870b7e10.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值