Commons-io jar包中的方法 功能流 以及 线程基础
2018-02-05 21:44
309 查看
Commons-io
获取路径 扩展名.txt .png static String getExtension(String filename) 获取文件的名字 static String getName(String filename) 判断是不是这个扩展名 static boolean isExtension(String filename,String extension) 复制文件夹 static void copyDirectoryToDirectory(File src,File desc) 复制文件 static void copyFile(File src,File desc) 写入字符串到文件 static void writeStringToFile(File src,String date) 按字符串读取文件 static String readFileToString(File src) 写入文件 可以选取用什么字节流写入 static void write(String data, OutputStream output) 读取文件到集合中以字符串形式 static List<String> readLines(InputStream input) ```
学习方法:
1. 先找类 找到对应的类(先看后缀)
2. 看该类如何创建对象(获取对象 怎么调方法)
3. 看方法名 揣测 方法的用意
4. 测试看结果 然后整理在方法集中
# 功能流(合并流) ## SequenceInputStream ### 合并功能(可以把 多个文件读成(合并)成一个流)
构造方法:
参数 是迭代器 是Vector特有的
该Vector要保存的是 InputStream 的子类
SequenceInputStream(Enumeration
// 将三个文件写成一个文件 public static void main(String[] args) throws IOException { File f1 = new File("文件一路径"); File f2 = new F a587 ile("文件二路径"); File f3 = new File("文件三路径"); // 创建集合 保存的是 FileInputStream Vector<FileInputStream> vector = new Vector<>(); vector.add(new FileInputStream(f1)); vector.add(new FileInputStream(f2)); vector.add(new FileInputStream(f3)); // 获取迭代器 Enumeration<FileInputStream> elements = vector.elements(); // 构建合并流 SequenceInputStream sis = new SequenceInputStream(elements); // 写到一个文件 FileOutputStream fos = new FileOutputStream("合并的文件路径"); // 数组方式写入 byte[] b = new byte[1024]; int len = 0; while ((len = sis.read(b)) != -1) { fos.write(b, 0, len); } sis.close(); fos.close(); }
1. 将图片切割成多个1M的文件 2. 然后将这些文件合并成一个图片 public static void fun1() throws FileNotFoundException, IOException { // 先读取文件 File file = new File("要被分割的文件"); FileInputStream fis = new FileInputStream(file); FileOutputStream fos = null; int len = 0; // 保存文件的名字 int num = 1; byte[] b = new byte[1024 * 1024]; while ((len = fis.read(b)) != -1) { // 写入多个文件 拼接文件的名字 fos = new FileOutputStream(new File("分割的文件路径"+ num +".后缀")); fos.write(b, 0, len); fos.close(); // 名字自增 num++; } fis.close(); } Vector<FileInputStream> vector = new Vector<>(); for (int i = 1; i < 19; i++) { File file = new File("路径" + i + ".后缀"); FileInputStream fis = new FileInputStream(file); vector.add(fis); } // 获取迭代器 Enumeration<FileInputStream> elements = vector.elements(); // 创建合并流 SequenceInputStream sis = new SequenceInputStream(elements); // 写到新路径下 FileOutputStream fos = new FileOutputStream("合并的文件路径"); byte[] b = new byte[1024 * 1024]; int len = 0; while ((len = sis.read(b)) != -1) { fos.write(b, 0, len); } sis.close(); fos.close();
# 线程 ### 进程: 一个正在运行的程序(独立) #### 进程只有一个线程的叫单线程程序 #### 有多个线程的叫多线程程序 ### 线程: 一个线程 相当于一个cpu的执行路径 独立的运行单元
单线程程序 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. 需要继承Thread类 ### 2. 重写父类中的 run方法 #### 线程默认名字: Thread - 0 Tread - 1 .....
public static void main(String[] args) { // 创建一个线程 SubThread subThread = new SubThread("ww"); // 直接调用run方法 相当于 就调用一个普通的成员方法 // subThread.run(); // 开启线程 // 注意: 直接调用run方法不能开启线程 需要调用start方法开启线程 subThread.start(); for (int i = 0; i < 50; i++) { System.out.println("- main - " + i); } // 获取主线程的名字 // 获取当前执行的线程对象 静态方法 Thread currentThread = Thread.currentThread(); String name = currentThread.getName(); System.out.println(name); }
// Thread(String name) 该方法在创建线程的同时给线程起名
class SubThread extends Thread{
// 构造方法
public SubThread() {
}
public SubThread(String string) {
super(string);
}
@Override public void run() { for (int i = 0; i < 50; i++) { // 获取线程名的方法 getName System.out.println("---- run 方法 ----" + i); } System.out.println(Thread.currentThread().getName()); }
}
class NameThread extends Thread {
private String name; public NameThread() { super(); } public NameThread(String name, String string) { super(name); // 给自己类的name赋值 // 建议: 不要使用name这个名字 避免麻烦 this.name = string; } // 把set/get方法改名 public String getNewName() { return name; } public void setNewName(String name) { this.name = name; } // 父类中的已经有getName()方法 并且用final修饰 不能重写 所以报错 /* public String getName() { return name; } public void setName(String name) { this.name = name; } */ @Override public void run() { super.run(); }
}
## 接口创建线程
public static void main(String[] args) { RunnableImpl rImpl = new RunnableImpl(); Thread thread = new Thread(rImpl); // 开启线程 thread.start(); }
// 接口中 run方法是 抽象方法
class RunnableImpl implements Runnable {
@Override public void run() { for (int i = 0; i < 50; i++) { System.out.println(Thread.currentThread().getName() + "--" + i); } }
}
“`
相关文章推荐
- Java中Commons-io-jar包中的方法、合并流、线程
- 多线程基础四(三)、yield方法、线程优先级以及守护线程
- 文件上传(使用commons-fileupload.jar以及commons-io.jar这两个jar文件)
- commns-io jar包中的方法及合并流及线程
- java线程基础巩固---线程生命周期以及start方法源码剖析
- 使用commons-lang.jar包以及反射机制实现javabean的equals,hashCode和toString三个方法重写
- Java基础(高级)——多线程的理解和Synchronized实例,以及线程间通信,wait,notify等方法
- 【搞懂Java多线程之一】多线程相关概念,线程生命周期以及线程创建方法
- 在程序中实现邮件发送,电话拨打,sms,以及浏览器功能能的方法整理
- java多线程之守护线程以及Join方法
- Java基础-方法区以及static的内存分配图
- JAVA基础(3) -- 线程常用方法
- 黑马程序员——基础学习--- Io异常处理方法
- C语言中strtok使用方法与原理,以及自实现函数功能
- java基础学习__多线程(停止线程,守护线程,join方法)
- 【JAVA基础知识总结】Java I/0流概述以及使用方法
- commons-io.jar的使用
- commons-io功能汇总
- 多线程——线程的创建以及操作线程的相关方法简介