Excel如何設(shè)置動態(tài)求和 Excel設(shè)置動態(tài)求和方法
778
2025-03-31
1.理解I/O流的概念,掌握其分類。
2.掌握文本文件讀寫、二進制文件讀寫。
實驗內(nèi)容:
1.編程實現(xiàn)任意文件的復(fù)制功能。
2.利用文件流和緩沖流復(fù)制文件的內(nèi)容。
3.創(chuàng)建文件和顯示文件的名稱和內(nèi)容。
4.接收鍵盤輸入的字符串并顯示在屏幕上。
實驗步驟:
1.編寫一個程序?qū)⒆址懊髟滤砷g照,清泉石上流?!睂懭隒:\a.txt中,并實現(xiàn)對該文件的讀取。
源代碼:
package homework.實驗9_輸入輸出流;
import Java.io.BufferedWriter;
import Java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class sy9_1 {
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
System.out.println("寫入字符串 : "+s);
try {
BufferedWriter bw=new BufferedWriter(new FileWriter("d:\a.txt"));
bw.write(s);
bw.flush();
bw.close(); //關(guān)閉流
} catch (IOException e) {
e.printStackTrace();
}
}
}
運行結(jié)果截圖:
2. 編寫一個程序利用字節(jié)流將C:\a.txt中的內(nèi)容復(fù)制到另一個文件b.txt中,并提示“文件復(fù)制成功?!?。
源代碼:
package homework.實驗9_輸入輸出流;
import java.io.*;
public class sy9_2{
public static void main(String[] args)throws IOException{
FileInputStream read = new FileInputStream(new File("D:\a.txt"));
FileOutputStream wr = new FileOutputStream(new File("D:\b.txt"));
byte[] b = new byte[1024];
int len = 0;
while((len=read.read(b))!=-1){
wr.write(b,0,len);
wr.flush();
}
wr.close();
read.close();
}
}
運行結(jié)果截圖:
3. 編寫一個程序利用字符流將C:\a.txt中的內(nèi)容復(fù)制到另一個文件b.txt中
源代碼:
package homework.實驗9_輸入輸出流;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class sy9_2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileReader fr = null;
FileWriter fw = null;
int c = 0;
try {
fr = new FileReader("d://a.txt");
fw = new FileWriter("d://c.txt");
while((c=fr.read())!=-1) {
fw.write(c);
}
System.out.println("文件復(fù)制成功");
}catch(FileNotFoundException e) {
System.out.println("文件沒找到");
}catch(IOException e) {
System.out.println("輸入錯誤");
}finally {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
4.以下程序?qū)崿F(xiàn)對象序列化和反序列化,請把程序補充完整。
Student 類:
import java.io.Serializable;
public class Student implements Serializable{
private String name;
private int age;
private String address;
public String getName() {
return
name
;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return
age
;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return
address
;
}
public void setAddress(String address) {
this.address = address;
}
}
測試類:
import java.io.*;
public class Test {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
Student mStudent1 =new Student();
mStudent1.
setName
("張三");
mStudent1.setAge(18);
mStudent1.setAddress("武漢市");
//ObjectOutputStream代表對象輸出流,它的writeObject(Object obj)方法可對參數(shù)指定的obj對象進行序列化,
//把得到的字節(jié)序列寫到一個目標(biāo)輸出流中,在這里寫到文件輸出流。
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/Student.txt"));
oos.writeObject(
mStudents1
);
oos.close();
System.out.println("序列化完成");
//ObjectInputStream代表對象輸入流,它的readObject()方法從一個源輸入流中讀取字節(jié)序列,
//再把它們反序列化為一個對象,并將其返回。
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/Student.txt"));
Student mStudent2 = (
Student
) ois.readObject();
ois.close();? System.out.println(mStudent2.getName()+"\t"+mStudent2.getAge()+"\t"+mStudent2.getAddress());
}
}
實驗小結(jié)
通過數(shù)據(jù)流、序列化和文件系統(tǒng)提供系統(tǒng)輸入和輸出。Java把這些不同來源和目標(biāo)的數(shù)據(jù)都統(tǒng)一抽象為數(shù)據(jù)流。Java語言的輸入輸出功能是十分強大而靈活的,美中不足的是看上去輸入輸出的代碼并不是很簡潔,因為你往往需要包裝許多不同的對象。
序列化和反序列化概念
把對象轉(zhuǎn)換為字節(jié)序列的過程稱為對象的序列化。
把字節(jié)序列恢復(fù)為對象的過程稱為對象的反序列化
對象的序列化主要有兩種用途:
1) 把對象的字節(jié)序列永久地保存到硬盤上,通常存放在一個文件中;
2) 在網(wǎng)絡(luò)上傳送對象的字節(jié)序列。
Java
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。