當前位置:網站首頁>筆記 第1章 流與文件(6) 文件隨機比特置讀取與Zip文件讀取
筆記 第1章 流與文件(6) 文件隨機比特置讀取與Zip文件讀取
2022-05-14 19:42:19【鈺娘娘】
隨機訪問文件
RandomAccessFile: 可在文件任何比特置讀寫文件,通過設置讀寫參數"rw",判斷可進行的操作。
作者提供的示例(close關閉部分有修改):
RandomFileTest.java
🧡🤎🧡🤎🧡🤎
import java.io.*;
public class RandomFileTest {
public static void main(String[] args) {
Employee[] staff = new Employee[3];
staff[0] = new Employee("Carl Cracker", 75000,1987, 12, 15);
staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
DataOutputStream out = null;
RandomAccessFile in = null;
try{
out = new DataOutputStream(new FileOutputStream("employee.dat"));
for(Employee e:staff)
e.writeData(out);
in = new RandomAccessFile("employee.dat", "r");
int n = (int)(in.length()/Employee.RECORD_SIZE);
Employee[] newStaff = new Employee[n];
for(int i = n-1; i >= 0; i--){
newStaff[i] = new Employee();
in.seek(i*Employee.RECORD_SIZE);
newStaff[i].readData(in);
}
for(Employee e:newStaff)
System.out.println(e);
}catch (IOException e){
e.printStackTrace();
}finally {
close(out);
close(in);
}
}
private static void close(Closeable c){
if(c == null) return;
try {
c.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
🧡🤎🧡🤎🧡🤎
Employee.java
🧡🤎🧡🤎🧡🤎
import java.io.*;
public class RandomFileTest {
public static void main(String[] args) {
Employee[] staff = new Employee[3];
staff[0] = new Employee("Carl Cracker", 75000,1987, 12, 15);
staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
DataOutputStream out = null;
RandomAccessFile in = null;
try{
out = new DataOutputStream(new FileOutputStream("employee.dat"));
for(Employee e:staff)
e.writeData(out);
in = new RandomAccessFile("employee.dat", "r");
int n = (int)(in.length()/Employee.RECORD_SIZE);
Employee[] newStaff = new Employee[n];
for(int i = n-1; i >= 0; i--){
newStaff[i] = new Employee();
in.seek(i*Employee.RECORD_SIZE);
newStaff[i].readData(in);
}
for(Employee e:newStaff)
System.out.println(e);
}catch (IOException e){
e.printStackTrace();
}finally {
close(out);
close(in);
}
}
private static void close(Closeable c){
if(c == null) return;
try {
c.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
🧡🤎🧡🤎🧡🤎
運行結果:
*🦽🦼*
*🦽🦼*
1.4 ZIP文檔
ZipInputStream/ZipOutputStream 可以處理 Zip 文檔的壓縮
特別注意對於單條信息的關閉方法:zin.closeEntry()
🧡🤎🧡🤎🧡🤎
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class ZipTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ZipTestFrame frame = new ZipTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class ZipTestFrame extends JFrame {
private int WIDTH = 400;
private int HEIGHT = 300;
private JComboBox fileCombo;
private JTextArea fileText;
private String zipname;
public ZipTestFrame(){
setTitle("ZipTest");
setSize(WIDTH,HEIGHT);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open");
menu.add(openItem);
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
int r = chooser.showOpenDialog(ZipTestFrame.this);
if(r == JFileChooser.APPROVE_OPTION){
zipname = chooser.getSelectedFile().getPath();
fileCombo.removeAllItems();
scanZipFile();
}
}
});
JMenuItem exitItem = new JMenuItem("Exit");
menu.add(exitItem);
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menuBar.add(menu);
setJMenuBar(menuBar);
fileText = new JTextArea();
fileCombo = new JComboBox();
fileCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loadZipFile((String)fileCombo.getSelectedItem());
}
});
add(fileCombo,BorderLayout.SOUTH);
add(new JScrollPane(fileText),BorderLayout.CENTER);
}
public void scanZipFile(){
new SwingWorker<Void, String>(){
@Override
protected Void doInBackground() throws Exception {
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname));
ZipEntry entry;
while((entry = zin.getNextEntry())!=null){
publish(entry.getName());
zin.closeEntry();
}
zin.close();
return null;
}
@Override
protected void process(List<String> names) {
for(String name:names){
fileCombo.addItem(name);
}
}
}.execute();
}
public void loadZipFile(final String name){
fileCombo.setEnabled(false);
fileText.setText("");
new SwingWorker<Void,Void>(){
@Override
protected Void doInBackground() throws Exception {
try{
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname));
ZipEntry entry;
while((entry = zin.getNextEntry())!=null){
if(entry.getName().equals(name)){
Scanner in = new Scanner(zin);
while(in.hasNextLine()){
fileText.append(in.nextLine());
fileText.append("\n");
}
}
zin.closeEntry();
}
}catch (IOException e){
e.printStackTrace();
}
return null;
}
@Override
protected void done() {
fileCombo.setEnabled(true);
}
}.execute();
}
}
🧡🤎🧡🤎🧡🤎
運行結果:
讀取zip文件內部的文件(一層)
通過下拉框可選擇文件名,上方顯示文件內容
*🦽🦼*
實際結構:
*🦽🦼*
注意,因為新建文件夾不是文件,所以讀不出來可考慮使用遞歸結構繼續讀取子文件
相關內容:選擇 《Java核心技術 卷1》查找相關筆記
評論點贊收藏關注,是送給作者最好的禮物,願我們共同學習,一起進步
如果對作者發布的內容感興趣,可點擊下方關注公眾號 鈺娘娘知識匯總 查看更多作者文章哦!
版權聲明
本文為[鈺娘娘]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2022/134/202205141804256830.html
邊欄推薦
猜你喜歡
隨機推薦
- VMware虛擬機 之 NAT模式詳解
- 【Devops】kubernetes網絡
- 新式茶飲“拿捏”年輕人,“八馬茶業”們的出路在哪?
- 機器學習之金融風控
- 1.67版本vscode括號著色(Bracket Pair Colorizer)取消
- MySQL日期查詢使用的方法函數
- HugeGraph客戶端APP開發(一)
- [.Net]使用Soa庫+Abp搭建微服務項目框架(五):服務發現和健康監測
- 添加虛擬內存,不添加硬盤的方式
- Redis源碼學習(25),雙端鏈錶學習,adlist.h
- 虛幻5新特性之EnhancedInput
- 緩存命中錶示什麼?
- sencha touch 在線實戰培訓 第一期 第四節
- “我們從 Google 離職了”
- yolov5訓練測試與源碼解讀
- 原生JS 實現輪播圖效果
- 邏輯回歸 解决報錯:ValueError: Solver lbfgs supports only ‘l2‘ or ‘none‘ penalties, got l1 penalty.
- Oracle OCI 計算、存儲、網絡工具旨在降低雲複雜性
- Go項目實戰之日志必備篇[開源十年項目第11次更新]
- Shell脚本變量和運算符
- 聊聊找工作
- 是能力更是文化,談談IT系統的安全發布
- tensorflow學習筆記(五)
- vitest支持cjs的workaround(TypeScript產物commonjs場景)
- 並發編程系列之Lock鎖可重入性與公平性
- 淺談 Fiori Fundamentals 和 SAP UI5 Web Components 的關系
- RAM/FIFO學習回顧
- 最新版2022年任我行管家婆工貿版ERP M7 V22.0進銷存財務生產管理軟件網絡版——雲上的集團化制造管理系統
- 【機器學習05】LASSO回歸與ElasticNet(彈性網)
- Idea快捷鍵
- 關於創建模態窗口和非模態窗口的研究
- An End-to-End Steel Surface Defect Detection Approach via Fusing Multiple Hierarchical Features-閱讀筆記
- 【性能測試】第五篇 | Jmeter環境安裝
- Matplotlib使用指南,100個案例從入門到進階!(附源代碼)
- Dots + interval stats and geoms
- SIGIR2022 | 基於用戶價格偏好及興趣偏好的會話推薦
- Cloudreve自建雲盤實站:容量和速度自己來决定
- 利用騰訊雲函數搭建免費代理池
- Redis的安裝及基本數據類型
- js輪播圖效果,透明度漸變實現