java基础-线程/IO

线程

并发:单核cpu,交替执行
并行: 多核cpu,同时执行

创建线程

方法1:继承Thread,重写run方法
cat.start(); 启动线程,如果直接调用run()方法,则仍然是main方法的进程,会造成阻塞
在这里插入图片描述

主线程结束后,子线程仍然可以进行
在这里插入图片描述

方法2:实现Runnable接口实现多线程

Thread thread=new Thread(Dog);
thread.start();//调用run()方法
class Dog implements Runnable{
    public void run(){}
    }

线程方法

setName() 设置线程名称

getName() 返回线程名称

start() JVM调用线程的start0方法,开启线程

run() 方法

setPriority() 更改线程的优先级

getPriority() 获取线程的优先级

sleep(time) 线程休眠指定时间

interrupt() 线程中断

线程礼让和插队:

在这里插入图片描述
守护线程
在这里插入图片描述

线程状态

在这里插入图片描述

线程同步synchronized

在这里插入图片描述

//同步方法
public synchronize void f(){} 
//同步代码块
synchronize(this){} 

死锁:
互相占用资源
释放锁:
在这里插入图片描述

IO流

创建文件

public class creatFile {
    /*
    创建一个文件夹temp和文件hello.txt
     */
    public static void main(String[] args) throws IOException {
        String directoryPath="e:\\temp";
        File file=new File(directoryPath);
        if(!file.exists()){
            //不存在时就创建
            if(file.mkdir()){
                System.out.println("文件夹创建成功");
            }else{
                System.out.println("文件夹已经存在");
            }
        }
        
        String filePath=directoryPath+"\\hello.txt";
        file=new File(filePath);
        if(!file.exists()){
            //不存在时就创建
            if(file.createNewFile()){
                //写入内容
                BufferedWriter bw = new BufferedWriter(new FileWriter(file));
                bw.write("hello world");
                bw.close();
                System.out.println("文件创建成功");
            }else{
                System.out.println("文件创建失败");
            }
        }else{
            System.out.println("already exists!");
        }

    }
}

文件方法

在这里插入图片描述

配置文件

配置文件的读取

public class Properties_read {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        //加载文件
        properties.load(new FileReader("D:\\javafile\\biji\\src\\File\\mysql.properties"));
        //显示k-v
        properties.list(System.out);
        //根据key读取value
        String user=properties.getProperty("user");
        String pwd=properties.getProperty("pwd");
        System.out.println("用户名="+user);
        System.out.println("密码="+pwd);
    }
}

配置文件的修改

//配置文件的修改
//Properties底层是HashTable
public class Properties_change {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        //存在就修改,不存在就创建
        properties.setProperty("user","wenjiefeng");
        properties.setProperty("pwd","654321");
        //存入文件
        properties.store(new FileOutputStream("D:\\javafile\\biji\\src\\mysql2.properties"),null);
        System.out.println("修改成功");
    }
}

文件格式

输出

/**
*   指定字符集的输出
*/
public class Transform_Output {
    public static void main(String[] args) throws IOException {
        String filePath="d:\\javafile\\fwj2.txt";
        String charSet="gbk";
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filePath), charSet);
        osw.write("hello,fengwj~");
        osw.close();
        System.out.println("文件保存成功");
    }
}

输入

//解决中文乱码问题
public class Transform_Input {
    public static void main(String[] args) throws IOException {
        String filePath="d:\\javafile\\fwj.txt";
        //gbk中文编码
        InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "gbk");
        BufferedReader br = new BufferedReader(isr);
        String s=br.readLine();
        System.out.println("文本信息:"+s);
        br.close();
    }
}

序列化和反序列化

序列化

public class Serializable_ {
    public static void main(String[] args) throws Exception {
        String filePath="d:\\data.dat";
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
        //序列化数据
        oos.writeInt(100);
        oos.writeChar('a');
        oos.writeBoolean(true);
        oos.writeDouble(1.1);
        oos.writeUTF("fengwenjie");
        oos.writeObject(new Dog("旺仔",1));
        oos.close();
        System.out.println("序列化完成");
    }
}
class Dog implements Serializable {
    private String name;
    private int age;
    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

反序列化

/*
1.读写顺序一致
2.static / transit 无法序列化
3.序列化对象时,属性类型也必须实现Serializable接口
4.序列化具有继承性
 */
public class UnSerializable_ {

    public static void main(String[] args) throws Exception {
        String filePath="d:\\data.dat";
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
        //反序列化
        System.out.println(ois.readInt());
        System.out.println(ois.readChar());
        System.out.println(ois.readBoolean());
        System.out.println(ois.readDouble());
        System.out.println(ois.readUTF());
        System.out.println(ois.readObject());
        ois.close();
        System.out.println("反序列完成");
    }
}

InputStream

FileInputStream
从文件读取到程序
在这里插入图片描述

FileOutStream
从程序写到文件
在这里插入图片描述
FileReader

//单个字符读取
FileReader  fileReader=null;
fileReader=new FileReader(path);
int data=0;
while(data=(fileReader.read())!=-1);
print(data);

//一组读取
int len=0;
char[] buf=new char[8];
while(len=(fileReader.read())!=-1);
print(new String(buf,0,len);

FileWriter

fileWriter=new FileWriter(path);
char[]chars={};
fileWriter.write(chars);
fileWriter.write("String");
fileWriter.write("String",0,len-1);
fileWriter.write("String".toCharArray(),0,len-1);

//一定要关闭流
fileWriter.close();

包装流Buffered

BufferedReader
字符流,按照字符读取

//从文件中读取
String path="xx";
BufferedReader bf=new BufferedReader(new FileReader(path));
String s;
while((s=bf.readLine()!)=null) //按行读取
    print(s);
   bf.close();//关闭外层流

BufferedWriter

//向文件写入
String path="xx";
BufferedWriter bf=new BufferedWriter(new FileWriter(path,true));//true表示追加至末尾
bf.write("String");
bf.newline();//换行
bf.close();

文件拷贝

public class BufferedCopy {
    public static void main(String[] args) {
        String srcFilePath="d:\\javafile\\bg.png";
        String destFilePath="d:\\javafile\\fwj.png";
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;
        try {
            bis=new BufferedInputStream(new FileInputStream(srcFilePath));
            bos=new BufferedOutputStream(new FileOutputStream(destFilePath));
            byte[]buff=new byte[1024];
            int readLen=0;
            //文件读取完毕,返回-1
            while((readLen=bis.read(buff))!=-1){
                bos.write(buff,0,readLen);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(bis!=null){
                    bis.close();
                }
                if(bos!=null){
                    bos.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值