Commons-io-2.4.jar中的方法:
获取路径扩展名:
static String getExtension(String filename)
举例:
public static void fun () {
String extension = FilenameUtils.getExtension("/Users/lanou/Desktop/Test/znb.txt" );
System.out .println(extension);
}
获取文件名:
static String getExtension(String filename)
举例:
public static void fun () {
String name = FilenameUtils.getName("/Users/lanou/Desktop/Test/znb.txt" );
System.out .println(name);
}
判断是否是文件扩展名:
static boolean isExtension(String filename,String extension)
举例:
public static void fun () {
boolean extension = FilenameUtils.isExtension("/Users/lanou/Desktop/Test/znb.txt" , "png" );
System.out .println(extension);
}
复制文件夹:
static void copyDirectoryToDirectory(File src,File desc )
举例:
public static void fun () throws IOException {
File src = new File("/Users/lanou/Desktop/Test" );
File dest = new File("/Users/lanou/Desktop/XTest" );
FileUtils.copyDirectoryToDirectory(src, dest);
}
复制文件:
static void copyFile(File src,File desc )
举例:
public static void fun () throws IOException {
File old = new File("/Users/lanou/Desktop/Test/znb.txt" );
File new = new File("/Users/lanou/Desktop/Test/znbz.txt" );
FileUtils.copyFile(old, new );
}
文件写入字符串:
static void writeStringToFile(File src,String date )
举例:
public static void fun () throws IOException {
File file = new File("/Users/lanou/Desktop/Test/znb.txt" );
FileUtils.writeStringToFile(file, "zhansan" );
}
以字符串形式读取文件:
static String readFileToString(File src)
举例:
public static void fun () throws IOException {
File file = new File("/Users/lanou/Desktop/Test/znb.txt" );
String string = FileUtils.readFileToString(file);
System.out .println(string );
}
以字符串形式读取文件到集合:
static List <String > readLines(InputStream input)
举例:
public static void fun () throws IOException {
File file = new File("/Users/lanou/Desktop/Test/znb.txt" );
IOUtils.write("haha" , new FileOutputStream(file));
List<String> readLines = IOUtils.readLines(new FileInputStream(file));
for (String string : readLines) {
System.out .println(string );
}
}
定义:
将多个文件(流)读成(合并成)一个文件
构造方法:
SequenceInputStream(Enumeration<? extends InputStream > e)
参数:泛型为InputStream (输入字节流)的Vector集合的迭代器
SequenceInputStream(InputStream s1, InputStream s2)
参数:两个输入字节流
举例:
需求:将图片(大小7M)以1M大小分割,再对分割后的文件进行合并
public static void cut () throws IOException {
File file = new File("/Users/lanou/Desktop/XTest/dp.png" );
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = null ;
int num = 1 ;
int len = 0 ;
byte [] b = new byte [1024 * 1024 ];
while ((len = fis.read(b)) != -1 ){
fos = new FileOutputStream("/Users/lanou/Desktop/XTest/dp" + num + ".png" );
fos.write(b, 0 , len);
fos.close();
num++;
}
fis.close();
}
public static void merge () throws IOException {
Vector<FileInputStream> vector = new Vector<>();
for (i = 1 ; i < 8 ; i++) {
File file = new File("/Users/lanou/Desktop/XTest/dp" + i + ".png" );
FileInputStream fis = new FileInputStream(file);
vector.add(fis);
}
Enumeration<FileInputStream> elements = vector.elements();
SequenceInputStream sis = new SequenceInputStream(elements);
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/XTest/dpNew.png" );
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的执行路径(大大提升了处理效率),一个独立运行单元
线程运行过程:
JVM调用main方法,找操作系统(CPU),CPU开启一个执行路径
该执行路径叫main执行路,main是线程名字,该线程又称主线程
单线程程序:
概论:一个进程中只有一个线程
特点:绝对安全,但因为程序由上至下执行,所以效率不高
举例:
public static void add () {
for (int i = 0 ; i < 100 ; i++) {
System.out .println(i);
}
}
public static void remove () {
System.out .println("删除" );
}
public static void main (String[] args) {
add();
remove();
System.out .println("程序执行完毕" );
}
多线程程序:
一个进程中有多个线程
实现多线程的方法:
继承Thread类重写run 方法,该方法中run 方法非抽象方法;
实现接口Runnable重写fun方法,该方法中run 方法是抽象方法。
继承Thread类:
注意:
1. Thread类中getName()被final修饰,不能被重写,因修改set /get 方法改名;
2. Thread(String name )该方法可以在创建线程的同时给线程命名;
3. 创建线程对象后,切勿用对象调用run 方法(该做法只是调用了一个成员方法,无法开启线程)
因调用线程的启动方法start。
举例:
class NameThread extends Thread {
private String name;
public NameThread () {
}
public NameThread (String name, String myName) {
super (name);
this .name = myName;
}
public String getMyName () {
return name;
}
public void setMyName (String name) {
this .name = name;
}
@Override
@Override
public void run () {
for (int i = 0 ; i < 50 ; i++) {
System.out.println(Thread.currentThread().getName() + "--run方法--" + i);
}
}
}
public static void main (String[] args) {
NameThread nameThread = new NameThread("线程1" );
nameThread.start();
for (int i = 0 ; i < 50 ; i++) {
System.out .println(Thread.currentThread().getName() + "--main--" + i);
}
}
实现Runnable接口:
举例:
class RunnableImpl implements Runnable {
@Override
public void run() {
for (int i = 0 ; i < 50 ; i++) {
System.out.println(Thread.currentThread().getName() + "--" + i);
}
}
}
public static void main (String[] args) {
RunnableImpl rImpl = new RunnableImpl();
Thread thread = new Thread(rImpl);
thread.start();
}