美文网首页
2022-11-04

2022-11-04

作者: RobertLiu123 | 来源:发表于2022-11-03 17:29 被阅读0次

一、同步

1、同步代码块
把要实现线程安全的代码放到同步代码块中

java中任何对象内部都有一个开关,你可以理解为标志位,1就表示关,0表示开

2、同步方法

二、io

input 输入
output 输出
计算机中的文件以及文件中的内容
File控制计算机中的文件和文件夹


image.png

三、File常用的方法

String getName():获取文件名称
String getPath():获取文件路径
String getAbsolutePath():获取绝对路径
File getParentFile():获取上级目录
boolean exists():判断文件是否存在
boolean isFile() :是否是文件
boolean isDirectory():判断是否是目录
boolean delete() :删除文件
boolean mkdirs():创建当前目录和上级目录
File[] listFiles() :列出所有文件对象

四、编码格式

五、流

管道,java语言操作文件中的内容就需要用流
根据流向划分:
输入流
输出流
向内存走,叫输入
从内存往外走,叫输出

根据传输内容划分
字节流
字符流
字节型文件、字符型文件
txt打开乱码就是字节型文件,不乱码就是字符型文件

根据依赖其他流的程度划分
低级流
高级流

字节输入流 InputStream
字节输出流 OutputStream

字符输入流 Reader
字符输出流 Writer

六、读取文本内容

File file = new File("d:/a/haha.txt");
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            int num = -1;
            StringBuilder sb = new StringBuilder();
            while((num = fis.read()) != -1) {
                sb.append((char)num);
            }
            System.out.println(sb.toString());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

七、向文本中写入内容

File file = new File("d:/a/hehe.txt");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            fos.write('a');
            fos.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

八、缓冲

File file = new File("d:/a/haha.txt");
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            byte[] buff = new byte[1024];
            int length = -1;
            StringBuilder sb = new StringBuilder();
            while((length = fis.read(buff)) != -1) {
                String str = new String(buff,0,length);
                sb.append(str);
            }
            System.out.println(sb.toString());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

九、向文本中写入很多内容

File file = new File("d:/a/hehe.txt");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file,true);
            fos.write("gdasgdystdsgdhwgdyyuw".getBytes());
            fos.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

作业!!!从c盘拷贝wbb.jpg粘贴到d盘,叫mfs.jpg


image.png

十、自带缓存

File file = new File("d:/a/haha.txt");
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        BufferedInputStream bis = new BufferedInputStream(fis);

十一、对象序列化

存到文件中

Human human = new Human(1, "能能");
        File file = new File("d:/a/haha.txt");
FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(human);
        oos.flush();

从文件取

File file = new File("d:/a/haha.txt");
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Human human1 = (Human)ois.readObject();
        System.out.println(human1.getId());
        System.out.println(human1.getUsername());
image.png
image.png
image.png
image.png
image.png

相关文章

  • 0367| 「改变」的本质

    2022-11-04 北京 晴天永澄老师最近在讲「改变」~ 改变的关键:我决定!! 你真的想改变吗?是 你真的下...

  • 2022-11-04

    2022-11-04小猪日更第10天 这段日子,看书很累,情绪有起伏,其实不大快乐,但是基本可以克制,笑眯眯还是可...

  • 2022-11-07

    教法丨基于学习者视角开展高中语文“大单元教学” 语文建设杂志 2022-11-04 15:38 发表于北京 以下文...

  • 未来日记

    2022-11-04,周五,晴朗 清晨五点半,我睁开朦胧眼,走到窗边,推开窗帘,伸个懒腰,望着我的点赞小院,绿草茵...

  • 外出务工的父子(七十二房租客之八)

    外出务工的父子(七十二房租客之八) 2022-11-04 当时我们实习的地方有一位叫小蒋的司机同事,原本仅仅只是部...

  • 2022-11-04

    1、读一本书,好比往怀里揣一个铜子,一个一个地攒,看起来无足轻重。等到需要的那一天,你就是那个腰缠万贯的人。 ——...

  • 2022-11-04

    十一点,约了打球。

  • 2022-11-04

    今天开会的间隙,小美女莹发给我一个视频,六分多钟的一个视频,又是未*成*年*人*欺*凌。看视频是中学的孩子,主要涉...

  • 2022-11-04

    周五是挺累的一天,我的钱还差一点点,才能凑齐。 明天也是很累的一天吧,早晨起来睡得还不错。 谈到今后发展问题,为什...

  • 2022-11-04

    敞亮话谁都会说,可真正能做到的人却是没几个。 一个兔子两个胆,想这也想那,怎么行呢。

网友评论

      本文标题:2022-11-04

      本文链接:https://www.haomeiwen.com/subject/burytdtx.html