關于大數據的分析作業,大數據預科班作業13

 2023-10-07 阅读 301 评论 0

摘要:大數據預科班作業13 1. (File 類)以下關于 File 類說法正確的是: A. 一個 File 對象代表了操作系統中的一個文件或者文件夾 B. 可以使用 File 對象創建和刪除一個文件 C. 可以使用 File 對象創建和刪除一個文件夾 D. 當一個

大數據預科班作業13

1. (File 類)以下關于 File 類說法正確的是:

A. 一個 File 對象代表了操作系統中的一個文件或者文件夾
B. 可以使用 File 對象創建和刪除一個文件
C. 可以使用 File 對象創建和刪除一個文件夾
D. 當一個 File 對象被垃圾回收時,系統上對應的文件或文件夾也被刪除

ABC

2. (File 類)有如下代碼:

public class TestFile{ 
public static void main(String args[]){ 
File file = new File(“chp13/corejava.txt”); 
} 
} 
請選擇一個正確答案:
A. corejava.txt 文件在系統中被創建
B. 在 windows 系統上運行出錯,因為路徑分隔符不正確
C. corejava.txt 文件在系統中沒有被創建
D. 如果 corejava.txt 文件已存在,則拋出一個異常

C--只有路徑,還沒被正真創建 

關于大數據的分析作業,

3. (File 類)將下列代碼補充完整

class TestMyFile{ 
public static void main(String args[]) throws Exception{ 
File file; 
//創建一個 File 對象表示當前目錄下的“hello.txt”文件
//判斷該文件是否存在
//如果該文件存在,則輸出該文件的完整路徑
} 
} 

package com.peng.demo;import java.io.File;public class TestMyFile {public static void main(String args[]) throws Exception {File file;// 創建一個 File 對象表示當前目錄下的“hello.txt”文件file = new File("hello.txt");// 判斷該文件是否存在// 如果該文件存在,則輸出該文件的完整路徑if (file.exists()) {System.out.println(file.getAbsolutePath());}}
}

4. (流的分類)

對于 FileInputStream 來說,從方向上來分,它是_________
流,從數據單位上分,它是__________流,從功能上分,它是____________流。

1. 輸入流
2. 字節流
3. 文件流

大學少數民族預科數學答案。

5. (字節流, FileInputStream)

FileInputStream 有三個重載的 read 方法,
其中 1) 無參的 read 方法返回值為___類型,表示_________________ 2) int 
read(byte[] bs)方法返回值表示______________,參數表示________________ 
3) int read(byte[] bs, int offset, int len) 方 法 返 回 值 表 示
_______________,參數分別表示___________________________。

1. int  
2. 下一個數據字節;如果已到達文件末尾,則返回 -1。 
3. 讀入緩沖區的字節總數,如果因為已經到達文件末尾而沒有更多的數據,則返回 -1。
4. b - 存儲讀取數據的緩沖區。 
5. 讀入緩沖區的字節總數,如果因為已經到達文件末尾而沒有更多的數據,則返回 -1。
6. b - 存儲讀取數據的緩沖區。off - 目標數組 b 中的起始偏移量。len - 讀取的最大字節數。 

6. (FileInputStream)下面關于 FileInputStream 類型說法正確的是:

A. 創建 FileInputStream 對象是為了讀取硬盤上的文件
B. 創建 FileInputStream 對象時,如果硬盤上對應的文件不存在,則拋出
一個異常
C. 利用 FileInputStream 對象可以創建文件
D. FileInputStream 對象讀取文件時,只能讀取文本文件。

A
B--FileNotFoundException

大學預科數學答案,

7. (FileOutputStream)填空:

創建 FileOutputStream 對象時,如果對應的文件在硬盤上不存在,則會
___________;如果對應的文件在硬盤上已經存在,則_______________;
如果使用 FileOutputStream(String path, boolean append) 這個構造方法創
建 FileOutputStream 對 象 , 并 給 定 第 二 個 參 數 為 true , 則 效 果 為
__________________。 創建 FileOutputStream 時_______(會|不會)產生異
常。

1. 創建文件
2. 創建新的文件并覆蓋
3. 在后面追加數據
4. 會(FileNotFoundException)

8. 代碼改錯

class TestFileInputStream{ 
public static void main(String args[]){ 
FileInputStream fin = new FileInputStream(“test.txt”); 
try{ 
System.out.println( fin.read() ); 
fin.close(); 
}catch(Exception e){} 
} 
} 

/*簡單改錯--拋出異常*/
package com.peng.demo;import java.io.FileInputStream;
import java.io.FileNotFoundException;class TestFileInputStream {public static void main(String args[]) throws FileNotFoundException {FileInputStream fin = new FileInputStream("test.txt");try {System.out.println(fin.read());fin.close();} catch (Exception e) {}}
}/*詳細改錯--捕獲異常*/
package com.peng.demo;import java.io.FileInputStream;class TestFileInputStream {public static void main(String args[]) {FileInputStream fin = null;try {fin = new FileInputStream("test.txt");System.out.println(fin.read());} catch (Exception e) {e.printStackTrace();} finally {if (null != fin) {try {fin.close();} catch (Exception e) {e.printStackTrace();} finally {fin = null;}}}}
}

v大預科作業。

9. ( FileInputStream 和 FileOutputStream )

 利 用 FileInputStream 和
FileOutputStream,完成下面的要求:
1) 用 FileOutputStream 在當前目錄下創建一個文件“test.txt”,并向文件
輸出“Hello World”,如果文件已存在,則在原有文件內容后面追加。
2)用 FileInputStream 讀入 test.txt 文件,并在控制臺上打印出 test.txt 中
的內容。
3) 要求用 try-catch-finally 處理異常,并且關閉流應放在 finally 塊中。

package com.peng.demo;import java.io.FileInputStream;
import java.io.FileOutputStream;class TestFileInputStream {public static void main(String args[]) {FileOutputStream fo = null;FileInputStream fin = null;try {fo = new FileOutputStream("test.txt", true);fo.write("hello world".getBytes());fo.flush();fin = new FileInputStream("test.txt");int len = -1;byte[] data = new byte[10];while ((len = fin.read(data)) != -1) {System.out.println(new String(data, 0, len));}} catch (Exception e) {e.printStackTrace();} finally {if (null != fo) {try {fo.close();} catch (Exception e) {e.printStackTrace();} finally {fo = null;}}}}
}

10. (Data 流)利用 Data 流,完成下面操作:

1) 判斷當前目錄下是否存在一個“test.dat”的文件,如果該文件不存在,
則往該文件中寫入一個 long 類型的數值:10000L 
2) 如果該文件存在,則從該文件中讀出數值,并把該數值加 1 之后,再存
回文件中。

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class data {public static void main(String[] args) {try {File file=new File("test.dat");if(!file.exists()){DataOutputStream out=new DataOutputStream(new
FileOutputStream("test.dat"));out.writeLong(10000);out.close();}else {DataInputStream in=new DataInputStream(new
FileInputStream("test.dat"));Long ll=in.readLong();ll=ll+1;DataOutputStream out=new DataOutputStream(new
FileOutputStream("test.dat"));out.writeLong(ll);System.out.println(ll);out.close();in.close();}} catch (Exception e) {e.printStackTrace();// TODO: handle exception}}
}

v大預科作業包下載。

11. (字符流、橋轉換)要想從某個文件中獲得一個字符輸出流,則至少有以下三種方式

A. 利用 FileWriter 類
B. 利用 PrintWriter 類
C. 利用 FileOutputStream 類,并通過 OutputStreamWriter 類獲得 Writer 
請簡述這三種方式獲得 Writer 的區別。

類型描述
FileWriter用來寫入字符文件的便捷類。此類的構造方法假定默認字符編碼和默認字節緩沖區大小都是可接受的。要自己指定這些值,可以先在 FileOutputStream 上構造一個 OutputStreamWriter。
PrintWriter向文本輸出流打印對象的格式化表示形式。此類實現在 PrintStream 中的所有 print 方法。它不包含用于寫入原始字節的方法,對于這些字節,程序應該使用未編碼的字節流進行寫入。與 PrintStream 類不同,如果啟用了自動刷新,則只有在調用 println、printf 或 format 的其中一個方法時才可能完成此操作,而不是每當正好輸出換行符時才完成。這些方法使用平臺自有的行分隔符概念,而不是換行符。此類中的方法不會拋出 I/O 異常,盡管其某些構造方法可能拋出異常。客戶端可能會查詢調用 checkError() 是否出現錯誤。
FileOutputStream+FileOutputStreamWriter文件輸出流是用于將數據寫入 File 或 FileDescriptor 的輸出流。文件是否可用或能否可以被創建取決于基礎平臺。特別是某些平臺一次只允許一個 FileOutputStream(或其他文件寫入對象)打開文件進行寫入。在這種情況下,如果所涉及的文件已經打開,則此類中的構造方法將失敗。FileOutputStream 用于寫入諸如圖像數據之類的原始字節的流。要寫入字符流,請考慮使用 FileWriter。
??
FileWriter可以直接讀取文件,可以直接寫入字符串
PrintWrite加一個TRUE可以自動刷新緩沖區
FileOutStream活動的是字節

12. (字節流、字符流)以下幾種文件格式,應當使用字節流還是字符流?

1) .java 源文件
2) .class 字節碼文件
3) .html 網頁文件
4) .jpg 圖像文件
5) .mp3 音樂文件
6) 配置文件.bash_profile 
7) .jar 文件

1. 字節流【2、4、5、7】
2. 字符流【1、3、6】

大數據大作業報告。

13. (過濾流)連線題。把過濾流和相應的功能用線連起來。注意,左右兩邊不是一一對應的關系。

ObjectInputStream 字節流
ObjectOutputStream 字符流
BufferInputStream 讀八種基本類型
BufferedOutputStream 寫八種基本類型
DataInputStream 讀對象
DataOutputStream 寫對象
PrintWriter 緩沖功能
PrintStream 讀入一行文本
BufferedReader 寫字符串并換行

ObjectInputStream(E)            A字節流
ObjectOutputStream(F)          B字符流
BufferInputStream(AG)          C讀八種基本類型
BufferedOutputStream(AG)         D寫八種基本類型
DataInputStream(AC)              E讀對象
DataOutputStream(AD)             F寫對象
PrintWriter(BJI)                  G緩沖功能
PrintStream(A)                  H讀入一行文本
BufferedReader(BGH)               I寫字符串并換行
BufferedWriter(BGj)               J寫字符串

14. (對象序列化)

為了讓某對象能夠被序列化,要求其實現________________________接口;
為了讓該對象某個屬性不參與序列化,應當使用____________修飾符。

1. __Serializable____
2. __transient______

v大預科作業2.0?

15. (字符流、橋轉換)完成下面功能:(字符流、橋轉換)完成下面功能:

事先在當前目錄下準備好一個 test.txt 的文本文件,要求該文本文件是使用
GBK 編碼的多行文本文件。如:
test.txt 
窗前明月光
疑是地上霜
舉頭望明月
低頭思故鄉
利用字節流+橋轉換讀入這個文本文件,然后按照行的順序,以 UTF-8 的編碼方
式,寫到 test2.txt 文件中,例:
test2.txt 
低頭思故鄉
舉頭望明月
疑是地上霜
窗前明月光

16. 16. (Data 流)有以下代碼

public class Check{ 
public static void main(String args[]) throws Exception{ 
FileOutputStream fout = new FileOutputStream(“test.dat”); 
DataOutputStream dout = new DataOutputStream(fout); 
dout.writeInt(1); 
dout.writeDouble(0.01); 
dout.close(); 
} 
} 
問:這個程序總共往文件中寫入了多少字節?
A. 2 
B. 8 
C. 12 
D. 16 
E. 字節數取決于具體平臺

package com.peng.demo;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;public class test2 {public static void main(String[] args) {String str = null;int i = 0;String con[] = new String[4];try {BufferedReader rw = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt")));BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("test2.txt")));while ((str = rw.readLine()) != null) {con[i] = str;i++;}for (int j = con.length - 1; j >= 0; j--) {String ss = new String(con[j].getBytes("UTF-8"));out.write(con[j]);out.newLine();}out.close();rw.close();} catch (Exception e) {e.printStackTrace();}}
}

預科高數、

(對象序列化)*(對象序列化)

在 PrintWriter 中,有一個方法 print(Object obj) 
在 ObjectOutputStream 中,有一個方法 writeObject(Object obj) 
請簡述這兩個方法的區別

1. PrintWriter按照平臺的默認字符串編碼將 String.valueOf(Object) 方法生成的字符串轉換為字節,并完全以 write(int) 方法的方式寫入這些字節。
2. 將指定的對象寫入 ObjectOutputStream。對象的類、類的簽名,以及類及其所有超類型的非瞬態和非靜態字段的值都將被寫入。可以使用 writeObject 和 readObject 方法重寫類的默認序列化。由此對象引用的對象是以可變遷的方式寫入的,這樣,可以通過 ObjectInputStream 重新構造這些對象的完全等價的圖形。當 OutputStream 中出現問題或者遇到不應序列化的類時,將拋出異常。所有異常對于 OutputStream 而言都是致命的,使其處于不確定狀態;并由調用者來忽略或恢復流的狀態。

18. (對象序列化)寫出下面代碼運行結果(對象序列化)寫出下面代碼運行結果

import java.io.*; 
class Address implements Serializable{ 
private String addressName; 
private String zipCode; 
//構造方法
//set/get 方法
public String toString(){ 
return addressName + “ ” + zipCode; 
} 
} 
class Student implements Serializable { 
private String name; 
private transient int age; 
private Address addr; 
//構造方法?
//set/get 方法?
public String toString(){ 
return name + “ ” + age + “” + addr.toString(); 
} 
} 
public class TestObjectStream{ 
public static void main(String args[]) throws Exception{ 
Address addr = new Address(“Beijing”, “100000”); 
Student stu = new Student(“Tom”, 18, addr); 
ObjectOutputStream oos = new ObjectOutputStream( 
new FileOutputStream(“stu.dat”) ); 
oos.writeObject(stu); 
oos.close(); 
ObjectInputStream oin = new ObjectInputStream( 
new FileInputStream(“stu.dat”) ); 
Student stu2 = (Student) oin.readObject(); 
oin.close(); 
System.out.println(stu2); 
} 
} 

Tom 0 Beijing 100000

19. 19. (對象序列化)有以下代碼:

import java.io.*; 
class Address{ 
private String addressName; 
private String zipCode; 
//構造方法?
//get/set 方法?
} 
class Worker implements Serializable{ 
private String name; 
private int age; 
private Address address; 
//構造方法?
//get/set 方法?
} 
public class TestSerializable { 
public static void main(String args[]) throws Exception{ 
Address addr = new Address("Beijing", "100000"); 
Worker w = new Worker("Tom", 18, addr); 
ObjectOutputStream oout = new ObjectOutputStream( 
new FileOutputStream("fout.dat") ); 
oout.writeObject(w); 
oout.close(); 
} 
} 
選擇正確答案
A. 該程序編譯出錯
B. 編譯正常,運行時異常
C. 編譯正常,運行時也正常。

B

(字節流,BufferedReader)完成下面操作。*(字節流,BufferedReader)完成下面操作。

在當前目錄下創建一個 worldcup.txt 的文本文件,其格式如下:
2006/意大利
2002/巴西
?
該文件采用“年份/世界杯冠軍”的方式保存每一年世界杯冠軍的信息。
要求:讀入該文件的基礎上,讓用戶輸入一個年份,輸出該年的世界杯冠軍。如
果該年沒有舉辦世界杯,則輸出“沒有舉辦世界杯”

package com.peng.demo;import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Scanner;public class test5 {public static void main(String[] args) {try {HashMap<String, String> hs = new HashMap<String, String>();String str;String con[] = new String[2];BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("worldcup.txt")));while ((str = in.readLine()) != null) {con = str.split("/");hs.put(con[0], con[1]);}String ss;int count = 0;Scanner s = new Scanner(System.in);System.out.println("請輸入年份:");ss = s.next();for (String mm : hs.keySet()) {if (mm.equals(ss)) {count++;System.out.println(mm + "年的世界杯冠軍是:" + hs.get(mm));}}if (count == 0)System.out.println("該年沒有舉辦世界杯!");} catch (Exception e) {// TODO: handle exception}}
}

21. (Buffered 流,緩沖區)有下面代碼(Buffered 流,緩沖區)有下面代碼

import java.io.*; 
public class TestBufferedWriter{ 
public static void main(String args[]) throws Exception{ 
FileWriter fw = new FileWriter(“test.txt”); 
BufferedWriter bw = new BufferedWriter(fw); 
String str = “Hello World”; 
bw.write(str); 
/*1*/ 
} 
} 
在/*1*/處放入什么代碼,能夠使得 test.txt 文件被正確寫入?
A. bw.close() 
B. bw.flush(); 
C. fw.close(); 

AB

22. 22. (Data 流)在原有自動分配 id 的 Account 對象基礎上,利用 Data 流,完成下面的要求: 要求每次啟動程序時,id 的自動分配都能在上一次運行的基礎上繼續。例如, 假設有以下代碼:

public class TestAccount{ 
public static void main(String args[]){ 
Account a1 = new Account(); 
Account a2 = new Account(); 
Account a3 = new Account(); 
System.out.println(a1.getId() ); 
System.out.println(a2.getId() ); 
System.out.println(a3.getId() ); 
} 
} 
編譯之后,第一次運行
java TestAccount 時,輸出
100001 
100002 
100003 
第二次運行
java TestAccount 時,輸出
100004 
100005 
100006 

(綜合)**(綜合)

從命令行中讀入一個文件名,判斷該文件是否存在。如果該文件存在,則在原文
件相同路徑下創建一個文件名為“copy_原文件名”的新文件,該文件內容為原
文 件 的 拷 貝 。 例 如 : 讀 入 /home/java/photo.jpg 則 創 建 一 個 文 件
/home/java/copy_photo.jpg 新文件內容和原文件內容相同。

package com.peng.demo;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;public class test5 {@SuppressWarnings("rawtypes")public static void main(String[] args) {Long lon = 100000L;File file = new File("tt.dat");if (file.exists()) {try {DataInputStream in = new DataInputStream(new FileInputStream("tt.dat"));lon = in.readLong();in.close();} catch (Exception e) {e.printStackTrace();// TODO: handle exception}}List list = new ArrayList();list.add(new Account(10.00, "1234"));list.add(new Account(15.00, "5678"));list.add(new Account(0, "1010"));TreeMap<Long, Account> acc = new TreeMap<Long, Account>();for (int i = 0; i < list.size(); i++) {acc.put((long) (lon + i), (Account) list.get(i));}int i = 0;for (Long mm : acc.keySet()) {Account ff = (Account) list.get(i);i++;ff.setId(mm);}for (Object ss : list) {System.out.println(((Account) ss).getId() + ":"+ ((Account) ss).getBalance());}try {DataOutputStream out = new DataOutputStream(new FileOutputStream("tt.dat"));out.writeLong((Long) (lon + list.size()));out.flush();out.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}

24. (可選,綜合)用兩種方式保存對象。(可選,綜合)用兩種方式保存對象。

有 Worker 對象,部分代碼如下:
class Worker{ 
private String name; 
private int age 
private double salary; 
//構造方法
?
//get/set 方法
?
//toString 方法
?
} 
1) 完善 Worker 對象,并使其能使用對象序列化機制。
2) 利用 ObjectOutputStream 存入兩個 Worker 對象, 然后使用
ObjectInputStream 讀出這兩個對象,并打印這兩個對象的信息。
3) 寫一個方法 saveWorkerToFile(Worker w, File file),該方法完成下面的
功能:
假設有一個 Worker 對象 w1,File 對象 f1 如下:
Worker w1 = new Worker(“Tom”, 30, 5000); 
File f1 = new File(“test.txt”); 
則調用 saveWorkerToFile(w1, f1),會在 test.txt 中增加一行:
Tom/30/5000 
4) 寫一個方法 List<Worker> readWorkerFromFile(File file),該方法讀某
個文件,從文件信息中創建一個 Worker 類型的 List。
例如,假設文件內容如下:
Tom/30/5000 
Jim/25/3000 
Terry/33/4500 
則返回一個包含三個 Worker 對象的 List。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/133435.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息