Java分割流,對象流,參與序列化,分割讀取文件,對象輸入或輸出流等【詩書畫唱】

自己寫的題目:用分割流把一個(gè)Java文件分割讀取,打印出每次讀取的內(nèi)容,要求每次讀取打印500個(gè)字節(jié)。

package fenGeLiu;
import java.io.*;
public class lizi1 {
public static void main(String[] args) throws Exception{
//分割流:可以將大文件分割為小文件,然后在使用的時(shí)候,
//又可以將若干個(gè)小文件組合成一個(gè)大文件來使用
File File=new File("src/fenGeLiu/copy.java");
int zongDaXiao=(int)File.length();
System.out.println(zongDaXiao);
//startIndex:定義文件開始讀取的位置下標(biāo),索引
int startIndex=0;
//lastIndexOrLength:默認(rèn)每次讀多少內(nèi)容的長度或最后的索引
int lastIndexOrLength=500;
//ciShu:這個(gè)文件讀的次數(shù)
int ciShu=(zongDaXiao/lastIndexOrLength)+1;
//遍歷這個(gè)讀的次數(shù):
for(int i=0;i<ciShu;i++){
startIndex=i*lastIndexOrLength;
//startIndex:最后一次讀的內(nèi)容
if(i!=ciShu-1){
// 如果i不為可以達(dá)到的最大值,就一直-500(這里的lastIndex)
// 為的是讓lastIndexOrLength在最后可以求出其部分的最后的下標(biāo)
// zongDaXiao:總大小
zongDaXiao-=lastIndexOrLength;
}else {
lastIndexOrLength=zongDaXiao;
}
System.out.println("第"+(i+1)+"次讀取的內(nèi)容為:");
// 調(diào)用方法:
fenGeFangFa(File.getAbsolutePath(),
startIndex,lastIndexOrLength);
// File.getAbsolutePath()為路徑
// startIndex:為開始的要截取部分的最小的索引,下標(biāo)
// lastIndex:為最后的要截取部分的最大的索引,下標(biāo)
}
/*File f=new File("copy.java");
File f1=new File("ww");
// 執(zhí)行一個(gè)位置,設(shè)置讀取為只讀
// 1600個(gè)字節(jié)大小
// 每次讀300個(gè),要讀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<duojici;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)+"次讀取的內(nèi)容為");
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();
}
//下面的是可以在主函數(shù)直接調(diào)用的方法:
// 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);//設(shè)置讀取的位置是100的位置
// byte[] byteArray=new byte[lengthSize];
// int len;
// readFenGe.read(byteArray);
// writeFenGe.write(byteArray);
//
// readFenGe.close();
// }
}


儲存對象的姓名等內(nèi)容,之后就去讀取和打印讀取的內(nèi)容(要求單個(gè)后單行等的打印,與用循環(huán)遍歷的方法把儲存到自己創(chuàng)建的new.txt的文件中的所有的對象【可以用“點(diǎn)某個(gè)文件,之后點(diǎn)鼠標(biāo)的右鍵,之后點(diǎn)最后一個(gè)選項(xiàng),之后就可以改文件的格式,讓txt和java文件都為UTF-8等的格式,防止中文亂碼,但設(shè)置“chengJi(成績)”等變量為transient,用transient來讓成績(chengJi)不參與序列化,這樣的話存在txt文件后就會仍出現(xiàn)亂碼等,以上是個(gè)人的理解或猜測的仍出現(xiàn)亂碼的原因等】:

package fenGeLiu;
import java.io.*;
import java.util.Date;
public class duiXangLiu {
public static void main(String[] args) throws Exception{
// ObjectInputStream:對象輸入流
// ObjectOutputStream:對象輸出流
// 特點(diǎn):
// 以前的流可以存儲文本,其他類型數(shù)據(jù),但是不可以存儲
// 對象,這個(gè)流唯一特點(diǎn)就是可以存儲對象。
// 為什么要存儲對象:
// 因?yàn)樵趥鬏攦?nèi)容的時(shí)候,如果傳輸?shù)氖瞧胀鳎坏?/p>
// 內(nèi)容被獲取到,其他人可以通過轉(zhuǎn)碼將數(shù)據(jù)轉(zhuǎn)會源碼,
// 但是對象流一旦被別人獲取到,別人要需要將其轉(zhuǎn)換為相應(yīng)
// 的對象格式,才可以看到里面的內(nèi)容
student duiXiang1=new student(4,"詩書畫唱",100.0f);
student duiXiang2=new student(5,"三連",99.9f);
student duiXiang3=new student(6,"關(guān)注",99.9f);
File f=new File("new.txt");
//傳入地址
FileOutputStream fis=new FileOutputStream(f);
//傳入字節(jié)流
ObjectOutputStream oos=new ObjectOutputStream(fis);
oos.writeObject(duiXiang1);
oos.writeObject(duiXiang2);
oos.writeObject(duiXiang3);
oos.writeObject(null);
oos.close();
System.out.println("存儲成功");
// ——————————————————————————————
// //讀單個(gè)對象:
File FileOne=new File("new.txt");
FileInputStream byteInputOne=new FileInputStream(FileOne);
//傳入字節(jié)輸入流:
ObjectInputStream byteDuiXiangInput
=new ObjectInputStream(byteInputOne);
Object duiXiangOne=byteDuiXiangInput.readObject();
Object duiXiangTwo=byteDuiXiangInput.readObject();
//用“(student)”的轉(zhuǎn)型,將讀取到的內(nèi)容轉(zhuǎn)為原先的類型:
student duiXiangOneOld=(student)duiXiangOne;
student duiXiangTwoOld=(student)duiXiangTwo;
byteDuiXiangInput.close();
System.out.println("打印第一行的對象的內(nèi)容為\t編號:"
+duiXiangOneOld.bianHao+";姓名 : "
+duiXiangOneOld.name+";成績 : "+duiXiangOneOld.chengJi);
System.out.println("打印第二行的對象的內(nèi)容為\t"
+" 編號: "
+duiXiangTwoOld.bianHao+";名字: "
+duiXiangTwoOld.name+";成績"
+ "(被設(shè)置為不顯示\n真實(shí)成績,默認(rèn)為0.0):"
+duiXiangTwoOld.chengJi+"\n");
// System.out.println(duiXiangTwo.toString());
// ——————————————————————
// 下面是打印所有的存在new.txt的文件的對象的內(nèi)容:
File File=new File("new.txt");
//傳入字節(jié)輸入流:
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();
}
}
//——————————————————————————
//下面為知識點(diǎn):
//一個(gè)類可以實(shí)現(xiàn)多個(gè)接口,接口一旦被實(shí)現(xiàn)后就必須實(shí)現(xiàn)接口中的抽象方法
//serializable:
//英[?s??r??la?z?bl]
//美[?s??ri??la?z?bl]
//[詞典] 【計(jì)】可串行化的;
//Serializable:序列化接口,它沒有其他的作用,接口中也沒有抽象方法
//transient關(guān)鍵字介紹:如果想讓某個(gè)對象的某個(gè)字段不參與序列化,
//可以加上transient關(guān)鍵字進(jìn)行修飾。
//transient 英[?tr?nzi?nt]
//美[?tr?n?nt]
//adj. 短暫的; 轉(zhuǎn)瞬即逝的; 倏忽; 暫住的; 過往的; 臨時(shí)的;
//n. 暫住某地的人; 過往旅客; 臨時(shí)工;
//——————————————————————
class student implements Serializable{
int bianHao;
String name;
transient float chengJi;
// 這里用transient來讓成績(chengJi)不參與序列化,
// 就是后面用toString方法返回的時(shí)候,
// 不會打印出傳入的真實(shí)成績放的內(nèi)容。
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設(shè)置為不能打印"
+ "\n出來的成績默認(rèn)為0.0):" +chengJi+ "]\n";
}
}

這里的”行“可以理解為”個(gè)“的意思:

