视频出处 提取码: 24s1
IO流感觉都是大同小异 会了最基本的流操作 其他的流就差不多了
直观感觉多线程的学习还是比较简单的 但是对多线程的思想要求较高 以后在多做项目 领会多线程吧
文件过滤器的简单lambda表达式写法 ->后面是过滤器的过滤条件
File[] listFiles = file.listFiles(pathname -> pathname.getName().endsWith(".java"));
//等同于
File[] listFiles = file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".java");
}
});
public void getAllDir(File pathname) {
File[] files = pathname.listFiles(pathname1 -> pathname1.isDirectory()
|| pathname1.getName().toLowerCase().endsWith(".java"));
//lambda表达式完全展开
File[] files1 = pathname.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()) {
return true;
} else {
return pathname.getName().toLowerCase().endsWith("java");
}
}
});
for (File file : files) {
if (file.isDirectory()) {
getAllDir(file);
} else {
System.out.println(file);
}
}
}
OutputStream outputStream = null;
try {
outputStream=new FileOutputStream("pathnamet",true);
outputStream.write("hello\r\nworld".getBytes());
} catch (IOException e) {
System.out.println(e.getMessage());
throw new RuntimeException("输出文件失败");
}finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
throw new RuntimeException("输出文件失败");
}
InputStream inputStream = new FileInputStream("pathname");
int length = 0;
while ((length = inputStream.read()) != -1) {
System.out.print((char) length);
}
InputStream inputStream = new FileInputStream("pathname");
byte[] bytes = new byte[10];
int length=0;
while ((length=inputStream.read(bytes))!=-1){
System.out.print(new String(bytes,0,length));
/*当字节数组没有被装满时,会输出打印上一次数组未被覆盖的内容
用length来限定打印的数组长度,正好打印到接受的字节数*/
}
其本质就是在源文件出读取,在目标文件写入
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new FileInputStream("inputPathName");
outputStream = new FileOutputStream("outputPathName");
byte[] bytes = new byte[1024*50];
int length = 0;
while ((length=inputStream.read(bytes))!=-1){
outputStream.write(bytes,0,length);
}
} catch (IOException e) {
new RuntimeException("文件复制异常");
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
new RuntimeException("输出流释放异常");
} finally {
try {
if (inputStream != null) {
outputStream.close();
}
} catch (IOException e) {
new RuntimeException("输入流释放异常");
}
}
}
字符流只能用于文本文件 尤其对于有中文汉字的文本文件更为适合
也是抽象类 需要使用其子类FileWriter 和 FileReader
两个转换流的类 都是继承于字符流 Writer 和 Reader类
inputStream = new FileInputStream("inputPathName");
outputStream = new FileOutputStream("outputPathName");
InputStreamReader ipsw = new InputStreamReader(inputStream,"GBK");
OutputStreamWriter opsw = new OutputStreamWriter(outputStream,"UTF-8");
char[] cbff= new char[2];
int length = 0;
while ((length = ipsw.read(cbff))!=-1){
opsw.write(cbff,0,length);
opsw.flush();
}
ipsw.close();
opsw.close();
注意: 当创建一个txt是GBK格式的时候 inputStreamReader的编码是GBK 但是要输出一个UTF-8格式时 需要在输出加“UTF-8” 其他和字符流 字节流差不多。
缓冲流的目的就是提高文件读写的顺序
除了构造方法不一样外 其余和不同字节流是一样的 主要是通过缓冲流对字节流进行缓冲 高效读写
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("outputPathname"));
BufferedInputStream bis= new BufferedInputStream(new FileInputStream("inputPathname"));
构造方法如下所示
BufferedWriter bw= new BufferedWriter( new FileWriter("writerPathNane"));
BufferedReader br= new BufferedReader( new FileReader("readerPathNane"));
BufferedWriter bw= new BufferedWriter( new OutputStreamWriter(new FileOutputStream(
"E:\\......\\b.txt"),"UTF-8"));
BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(
"E:\\......\\a.txt"),"GBK"));
BufferedWriter bw= new BufferedWriter( new OutputStreamWriter(new FileOutputStream(
"E:\\......\\b.txt"),"UTF-8"));
BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(
"E:\\......\\a.txt"),"GBK"));
String str=null;
while ((str= br.readLine())!=null){
bw.write(str);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
小结
注
继承自Hashtable ,实现了Map接口
在.properties文件中 用#表示注释 # 1=a
序列化是对象的序列化 对于静态的成员变量无法将其序列化
InputStream inputStream = new FileInputStream("FileInputPathName");
OutputStream outputStream = new FileOutputStream("FileOutputPathName");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(new Person("zhangsan", 23));
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
Object obj = objectInputStream.readObject();
System.out.println(obj);
objectInputStream.close();
objectOutputStream.close();
private static final long serialVersionUID = -1499574590977726397L;
继承Thread 重写了run()方法
public class Subthread01 extends Thread{
@Override
public void run() {
运行的方法内容
}
}
Subthread01 subthread01 = new Subthread01();
subthread01.setName("SbuThread01");
subthread01.start();
for (int i = 0; i < 100; i++) {
Thread td=Thread.currentThread();
System.out.println(td.getName());
}
创建方法与继承Thread类似
public class SubThread02 implements Runnable{
@Override
public void run() {
运行的方法内容
}
}
主要实现了线程类与对象的解耦
Thread thread = new Thread(new SubThread02());
thread.start();
for (int i = 0; i < 100; i++) {
Thread td=Thread.currentThread();
System.out.println(td.getName());
}
通过匿名内部类的方式
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Thread");
}
}
});
Thread thread = new Thread(() -> {
for (int i = 0; i < 100; i++) {
System.out.println("Thread");
}
});
ExecutorService executorService = Executors.newFixedThreadPool(3);
executorService.submit(() -> {
for (int i = 0; i < 100; i++)
System.out.println(Thread.currentThread().getName()+"...."+i);});
executorService.submit(() -> {
for (int i = 0; i < 100; i++)
System.out.println(Thread.currentThread().getName()+"...."+i);});
executorService.submit(() -> {
for (int i = 0; i < 100; i++)
System.out.println(Thread.currentThread().getName()+"...."+i);});
private Object object = new Object();
synchronized (object){
if (ticketsTotal>0){
System.out.println(
Thread.currentThread().getName()+"还有"+ticketsTotal--+"张票");
}
}
()中object被称为同步锁 只有得到同步锁的线程才能去执行同步代码块的程序 即在同代码块中的线程是单线程的 无法多线程同时执行
public synchronized void sailTickets() {
if (ticketsTotal > 0) {
System.out.println(
Thread.currentThread().getName() + "还有" + ticketsTotal-- + "张票");
}
}
通过Lock() 接口的方法 代替同步代码块 进一步提高其安全性 主要防止当线程出现异常的时候 锁不会被释放的情况
在不管是否出现异常 都会在finally中释放锁
private int ticketsTotal = 1000;
Lock lock = new ReentrantLock();
@Override
public void run() {
while (true) {
lock.lock();
if (ticketsTotal > 0) {
try {
Thread.sleep(20);System.out.println(
Thread.currentThread().getName() +
"还有" + ticketsTotal-- + "张票");
} catch (InterruptedException e) {
}finally {
lock.unlock();
}
}
}
}
(感觉这个需要在后面真的做项目才遇到 看了视频的案例感觉有点空洞)
public class Resource {
private String name;
private String sax;
private boolean flag = true;
private Resource() {
}
public static final Resource RESOURCE = new Resource();
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
public String getSax() {
return sax;
}
public void setSax(String sax) {
this.sax = sax;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Input implements Runnable {
public Resource resource;
public Input(Resource resource) {
this.resource = resource;
}
@Override
public void run() {
int i = 0;
while (true) {
synchronized (resource) {
if (!resource.isFlag()) {
try {
resource.wait();
} catch (InterruptedException e) {
}
}
if ((i % 2) == 0) {
resource.setName("张三");
resource.setSax("男");
} else {
resource.setName("lisi");
resource.setSax("nv");
}
resource.setFlag(!resource.isFlag());
resource.notify();
}
i++;
}
}
}
public class Output implements Runnable {
public Resource resource;
public Output(Resource resource) {
this.resource = resource;
}
@Override
public void run() {
while (true) {
synchronized (resource) {
if (resource.isFlag()) {
try {
resource.wait();
} catch (InterruptedException e) {
}
}
System.out.println( resource.getName() + "......" + resource.getSax());
resource.setFlag(!resource.isFlag());
resource.notify();
}
}
}
}
public static void main(String[] args) {
Resource resource=Resource.RESOURCE;
Thread threadInput = new Thread(new Input(resource));
Thread threadOutput = new Thread(new Output(resource));
threadInput.start();
threadOutput.start();
}
注