合并流
1.合并功能(可以把多个文件合并成差一个文件)
2.SequenceInputStream 合并功能(多个流合并成一个流)
3.构造方法
1.参数是迭代器 是Vector特有的子类
2.SequenceInputStream(Enumeration<? extends InputStream> e)
参数:传入两个流
SequenceInputStream(Input s1,inputStream s2)
2.合并文件
1.将三个文件合并成一个
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
public class text {
public static void main(String[] args) throws IOException {
File file1 = new File("/Users/lanou/Desktop/test/kk.txt");
File file2 = new File("/Users/lanou/Desktop/test/kke.txt");
File file3 = new File("/Users/lanou/Desktop/test/kkle.txt");
Vector<FileInputStream> vector = new Vector<>();
vector.add(new FileInputStream(file1));
vector.add(new FileInputStream(file2));
vector.add(new FileInputStream(file3));
Enumeration<FileInputStream> element = vector.elements();
SequenceInputStream sis = new SequenceInputStream(element);
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/test/4.txt");
byte[] b = new byte[1024];
int len = 0;
while((len = sis.read(b)) != -1) {
fos.write(b,0,len);
}
fos.close();
sis.close();
}
}
3.将文件拆成碎片
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class text {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/test/dp.png");
FileOutputStream os = null;
BufferedInputStream bfo = new BufferedInputStream(fis);
byte[] b = new byte[1024 * 1024];
int len = 0;
int num = 1;
while ((len = bfo.read(b)) != -1) {
os = new FileOutputStream("/Users/lanou/Desktop/test/" + num + ".哈哈哈");
os.write(b,0,len);
num++;
}
fis.close();
os.close();
}
}
4.合并碎片文件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
public class text {
public static void main(String[] args) throws IOException {
Vector<FileInputStream>vector = new Vector<>();
for (int i = 1; i < 19; i++) {
File file= new File("/Users/lanou/Desktop/day28作业/" + i + ".suipian");
vector.add(new FileInputStream(file));
}
Enumeration<FileInputStream>element = vector.elements();
SequenceInputStream sis = new SequenceInputStream(element);
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/day28作业/"+"1.txt");
byte[] b = new byte[1024];
int len = 0;
while ((len = sis.read(b)) != -1) {
fos.write(b,0,len);
}
fos.close();
sis.close();
}
}
1.线程
1.线程与进程
1.进程:一个正在运行的程序(独立运行的程序)
一个进程中有一个线程(单线程)
一个进程中有多个线程(多线程)
2.线程:一个线程相当于一个CPU的执行路径(大大提升了处理效率)
3.标准的单线程程序绝对安全 但是程序由上至下执行 效率不高
1.1单线程
public class Demo01 {
public static void main(String[] args) {
add();
remove();
System.out.println("程序执行完毕");
}
public static void add() {
for (int i = 0; i < 100; i++) {
System.out.println(i);
}
}
public static void remove() {
System.out.println("删除");
}
}
1.1代码是如何开启线程
-->-->
public class Demo02 {
public static void main(String[] args) {
System.out.println(10/0);
add();
System.out.println("程序完成");
}
public static void add() {
for (int i = 0; i < 100; i++) {
System.out.println(i);
}
}
}
2.多线程
开启一个线程并开启
1.需要继承Thread
2.重写父类中的方法
线程默认名字:Thread - 0 Thread-1...
public class text {
public static void main(String[] args) {
SubThread subThread1 = new SubThread("线程1");
SubThread subThread = new SubThread();
subThread.setName("线程2");
subThread.start();
subThread1.start();
Thread currentThread = Thread.currentThread();
System.out.println(currentThread);
for (int i = 0; i < 50; i++) {
System.out.println(currentThread.getName() + "--main--" + i);
}
}
}
class SubThread extends Thread{
public SubThread() {
}
public SubThread(String name) {
super(name);
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(getName() + "--run方法--" + i);
}
super.run();
}
}
3.接口Runnable
public class Demo05 {
public static void main(String[] args) {
RunnableImpl rImpi = new RunnableImpl();
Thread thread = new Thread(rImpi);
thread.start();
}
}
class RunnableImpl implements Runnable{
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + "--" + i);
}
}
}
4.创建Thread 子类 NameThread1
Runnable 接口应该由那些打算通过某一线程执行其实例的类来实现。
类必须定义一个称为 run 的无参数方法。
public class text {
public static void main(String[] args) {
NameThread1 nt = new NameThread1();
NameThread1 nt1 = new NameThread1();
nt1.setMyName("线程2");
nt.run();
nt1.run();
}
}
class NameThread1 extends Thread{
private String name;
public NameThread1() {
}
public NameThread1(String name,String myname) {
super(name);
this.name = myname;
}
public String getMyName() {
return name;
}
public void setMyName(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(getName() + name + "--" + i );
}
}
}