0301 打印流,commons-IO,多线程

1、打印流

打印流

字节打印流:ptintStream

字符打印流:printWriter

方法

void print(String str)输出任何数据

void println(String str)输出任何数据 并且自动换行

我们直接用打印流复制文件

	public static void main(String[] args) throws IOException {
		//明确数据源
		FileReader fr=new FileReader("F:\io1127\hello.txt");
		//添加缓冲流
		BufferedReader br=new BufferedReader(fr);
		//明确目的地
		FileWriter fw=new FileWriter("F:\io1127\hello2.txt");
		//添加打印流开启走动刷新
		PrintWriter pw=new PrintWriter(fw,true);
		//开始复制
		String line=null;
		while((line=br.readLine())!=null){
			pw.println(line);
		}
		pw.close();
		br.close();
	}

  在上述代码中出现了乱码的问题 所以我们需要加入一个转换流去指定一下码表

	public static void main(String[] args) throws IOException {
		//明确数据源
		FileInputStream fis=new FileInputStream("F:\io1127\hello.txt");
		//添加转换流
		InputStreamReader isr=new InputStreamReader(fis,"utf-8");
		//添加缓冲流
		BufferedReader br=new BufferedReader(isr);
		//明确目的地
		FileWriter fw=new FileWriter("F:\io1127\work1\hello.txt");
		//添加打印流
		PrintWriter pw=new PrintWriter(fw,true);
		String line=null;
		while((line=br.readLine())!=null){
			pw.println(line);
		}
		pw.close();
		br.close();
		
	}

  我们注意到 在添加打印流的时候传的参数会有一个布尔值的存在,这是开启刷新功能 ,就不用我们在自己手动调用flush方法去刷新了

2、commons-IO是好的一套工具类,可以直接调用其中的方法,那么在使用之前需要将jar包导入dao项目文件中

2-1、FilenameUtils

这个工具类是主要处理文件路径文件名称

常用方法

(1)getExtension(String path) 获取文件的扩展名

(2)getName(String filename)获取文件名

(3)isExtension(String fileName,String ext) 判定文件是否以 ext结尾

代码展示

	public static void main(String[] args) {
		//获取扩展名
		String ext=FilenameUtils.getExtension("F:\io1127\hello.txt");
		System.out.println(ext);
		//获取文件名
		String name=FilenameUtils.getName("F:\io1127\hello.txt");
		System.out.println(name);
		//判定文件是否以某后缀结尾
		Boolean flag=FilenameUtils.isExtension(name, "txt");
		System.out.println(flag);
		
	}

  

2-2、FileUtils

该类主要是操作文件的移动,读取,判定文件是否存在等等

常用方法

(1)、readFileToString(File file)读取文件的内容,返回一个String类型的字符串

(2)、writeStringToFile(File file,String content) 将content内容写入到file中

(3)、copyDirectoryToDirectory(File srcDir,File destDir) 复制文件夹

(4)、copyFile(File srcFile,File destFile)复制文件

	public static void main(String[] args) throws IOException {
		//复制文件夹
		FileUtils.copyDirectoryToDirectory(new File("F:\io1127"), new File("F:\demo"));
	}
	//读取文件内容
	public static void get2() throws IOException{
		String str=FileUtils.readFileToString(new File("F:\io1127\hello.txt"));
		System.out.println(str);
	}
	//写入文件内容
	public static void get3() throws IOException{
		FileUtils.write(new File("F:\io1127\hello.txt"), "123");
	}
	//复制文件
	public static void get4() throws IOException{
		FileUtils.copyFile(new File("F:\io1127\hello.txt"), new File("F:\io1127\work\hello.txt"));
	}

  

3、多线程

进程:一个程序进入内存运行 则叫一个进程

线程:线程是一个进程的执行单元,一个进程至少有一个线程,一个进程可以有好多个线程,这个应用程序也可以称之为多线程程序

单线程,所有程序依次执行,若多个任务同时执行,当一个任务执行完毕后再进行下一个任务执行

多线程:多个任务可以同时执行

多线程程序运行原理:

分时调度:所有的线程轮流使用cpu,平均分配每个线程所占用的cpu的时间

抢占式调度:先让优先级高的线程使用cpu如果优先级相同,则随机选择一个线程,java就使用的是抢占式调度

多线程举例:360图:

怎样创建多线程:

我们需要创建一个子类去继承Thread类,并且重写Thread中的run方法,run方法中就写的是除了主线程之外的另一条线程所有执行的程序

public class MyThread extends Thread{

	@Override
	public void run() {
		//描述线程任务
		for(int i=0;i<100;i++){
			System.out.println(getName()+":"+i);
		}
	}
}

  

	public static void main(String[] args) {
		//创建线程对象
		MyThread my=new MyThread();
		//开启线程
		my.start();
		//描述主线程任务
		for(int i=0;i<100;i++){
			System.out.println(Thread.currentThread().getName()+":"+i);
		}
	}

  其中thread类中的构造方法 一个是空参构造和有参构造,有参构造中可以传一个String参数来给线程起名字(但是一般情况下不用起名字)

thread类中常用方法

run()写该线程所需要执行的操作

start()开启线程

还有两个获取线程名字的方法

(1)getName()返回该线程的名称,返回值类型是String

(2)currentThread()是一个静态修饰的方法,返回当前线程对象再利用这个对象调用getname就能返回一个线程名称

多线程代码内存图解

原文地址:https://www.cnblogs.com/-gongxue/p/14462664.html