# 线程案例01

请结合所学知识实现下述程序:

image-20230916111619851

zip压缩包的文件内容。

image-20230916111643092

# 需求描述

1、线程1的功能:读取“2022-2023学年Java程序设计期末作业”文件夹中的sport.zip中的内容,并将该内容写入result.txt中,sport.zip中各个文件的内容用“*************************************”分隔。如图1所示。

image-20230916111105593

2、线程2的功能:将“2022-2023学年Java程序设计期末作业”文件夹中的 news_4.txt中的内容写入result.txt中。

3、要求实现线程1和线程2的同步访问。如图2所示。

image-20230916111512561

4、在线程1和线程2的功能都完成以后,才能激活线程3,线程3的功能:在result.txt中添加文本“2020级 电子商务专业 姓名 学号”,其中姓名和学号写考生自己的学号和姓名。如图3所示。

image-20230916111301103

5、最终的程序以工程的方式提交,工程以“学号+姓名”方式命名,作业截止时间为2023年1月3日。

# 分析执行逻辑图

image-20230916112330321

# 代码实现

工程结构

image-20230916112458867

创造主类。

package com.gun.demo01;

public class Test01 {
	public static void main(String[] args) {
		// 1.实现线程1和线程2的同步访问
		System.out.println(Thread.currentThread().getName()+"主线程执行了");
		//创建线程一:读取下面的文件
		Thread task01 =new Thread(new Work01());
		Thread task02 =new Thread(new Work02());


		// 创建了线程 3
		Thread task03 =new Thread(new Work03(task01,task02));
		task01.start();
		task02.start();
		task03.start();
		
	}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

线程1

package com.gun.demo01;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


import com.gun.utils.ReadZipContent;

public class Work01 implements Runnable{

	@Override
	public void run() {
		
		System.out.println(Thread.currentThread().getName()+"子线程执行了");
		
		//读取zip文件
		List<List<String>> readZipFile = ReadZipContent.readZipFile("C:\\Users\\32802\\Desktop\\com.gun.demo01\\src\\2022-2023学年第1学期Java程序设计期末作业/sport.zip");
		
		try {
			ReadZipContent.writeFilesContent(readZipFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	
    //读取zip文件内的文件,返回文件名称列表
    public static List<String> readZipFileName(String path){
        List<String> list = new ArrayList<>();
        try {
        	// zip文件对象
            ZipFile zipFile = new ZipFile(path);
            
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            
            while (entries.hasMoreElements()) {
            	
                list.add(entries.nextElement().getName());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
    
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

线程2

package com.gun.demo01;
import java.io.IOException;
import java.util.List;

import com.gun.utils.ReadZipContent;

public class Work02 implements Runnable{

	@Override
	public void run() {
		System.out.println(Thread.currentThread().getName()+"子线程执行了");
		List<List<String>> readZipFile = null;
		try {
			// 读取,news_4.txt文件
			readZipFile = ReadZipContent.readZipFile("C:\\Users\\32802\\Desktop\\com.gun.demo01\\src\\2022-2023学年第1学期Java程序设计期末作业\\news_4.txt");
			
			ReadZipContent.writeFilesContent(readZipFile);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

线程3

package com.gun.demo01;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;



public class Work03 implements Runnable{

	/**
	 * 静态常量
	 */
	private static Thread task01;
	private static Thread task02; 


	public Work03(Thread task01,Thread task02) {
		this.task01 = task01;
		this.task02 = task02;
	}


	/**
	 * 线程三的逻辑方法
	 */
	@Override
	public void run() {
		
		try {
			task01.join();
			task02.join();
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}


		/**
		 * 创建文件输出流对象。写
		 * 一行年级 + 专业 + 名字 + 学号
		 */
		try {
			FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\Users\\32802\\Desktop\\com.gun.demo01\\src\\2022-2023学年第1学期Java程序设计期末作业\\result.txt"),true);
			fileOutputStream.write("2020级 电子商务专业 张冲  20204064\n".getBytes());
			fileOutputStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

# 测试效果

image-20230916112157970

# 读txt文件工具类

package com.gun.utils;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;


/**
 * 是用来读取zip文件的内容的,
 *
 * 读:2022-2023学年第1学期Java程序设计期末作业下面的.txt的文件。
 */
public class ReadZipContent {


	/**
	 *
	 * @param path
	 * @return 一个容器,容器放到是list容器
	 * 每一个list集合元素的字符串,
	 * 一个字符串元素:表示.txt的一行记录。 0  ==》 表示txt的第一行。
	 *
	 * 还原原来的文本就是:从0开始遍历list就行。
	 */
	//读取zip文件内的文件,返回文件内容列表
    public synchronized static List<List<String>> readZipFile(String path){
        

        // 存放的是每一个文件的内容容器
    	List< List<String> > ddlList=new ArrayList<>();
    	
    	BufferedReader br = null;
    	
    	System.out.println(path.substring(path.lastIndexOf(".")));


    	if(path.substring(path.lastIndexOf(".")+1).equals("zip")){


    		// ================   是一个压缩包
        try {
        	// 创建文件要读取的
            ZipFile zipFile = new ZipFile(path);
            InputStream in = new BufferedInputStream(new FileInputStream(path));
            ZipInputStream zin = new ZipInputStream(in);
            //文件对象
            ZipEntry ze;

			/**
			 * 遍历zip包下的文件。
			 */
			while ((ze = zin.getNextEntry()) != null) {
                if (ze.isDirectory()) {
                	//是个目录怎么办?
                } else {
                	//开始读取文件
                	System.err.println("zip包下的文件:file - " + ze.getName() + " : "+ ze.getSize() + " bytes");
                    long size = ze.getSize();
                    // 大于0  文件有内容
                    if (size > 0) {
                    	
                    	ArrayList<String> list = new ArrayList<>();
                    	// 将文夹的名字放入 容器首元素
                    	list.add(ze.getName());
                    	
                        br = new BufferedReader(new InputStreamReader(zipFile.getInputStream(ze),Charset.forName("utf-8")));
                        //循环读取每一行
                        String line;
                        while ((line = br.readLine()) != null) {
                        	list.add(line);
                        }
                        
                        list.add("**************************************");
                        // 文件 容器  放入存放内容的容器
                        ddlList.add(list);
            
                      
                    }
                }
                //处理ddlList,此时ddlList为每个文件的内容,while每循环一次则读取一个文件
            }
            zin.closeEntry();
            
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    	}else{

    		// 不是一个压缩包       如读取  news_4.txt文件
    		ArrayList<String> list = new ArrayList<>();
    		File file = new File(path);
    		try {
				 br = new 
						BufferedReader(new 
								InputStreamReader(new 
										FileInputStream(file),Charset.forName("utf-8")));
				
				String name = file.getName();
				
            	// 将文夹的名字放入 容器首元素
            	list.add(name);
                //循环读取每一行
                String line;
                while ((line = br.readLine()) != null) {
                	list.add(line);
                }
                list.add("不是zip包下的文件**************************************");
                
                // 文件 容器  放入存放内容的容器
                ddlList.add(list);
    
               
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				 try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
    		
    	}
        //此处返回无用,懒得修改了
        return ddlList;
    }
    
    
    
    public synchronized static void writeFilesContent(List<List<String>> readZipFile) throws IOException{
    	
    	FileOutputStream fileOutputStream = null;
		// 开始写入文件到result.txt中
		try {
			fileOutputStream = new FileOutputStream(new File("src/2022-2023学年第1学期Java程序设计期末作业/result.txt"),true);
			for(List<String> list:readZipFile){
				
				for(String line:list){
					System.out.println(line);
					byte[] lineBytes = line.getBytes();
					fileOutputStream.write(lineBytes);
					fileOutputStream.write("\n".getBytes());
				}
				
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			fileOutputStream.close();
		}
		
	}
    

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
Last Updated: 7/27/2024, 11:59:26 AM