Java核心类库篇6——IO

Java核心类库篇6——IO

1、File

1.1、构造方法

方法声明功能介绍
public File(File parent, String child)从父抽象路径名和子路径名字符串创建新的 File实例
public File(String pathname)通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例
public File(String parent, String child)从父路径名字符串和子路径名字符串创建新的File实例
public File(URI uri)通过将给定的file:URI转换为抽象路径名来创建新的File实例

1.2、方法

方法声明功能介绍
public boolean exists()测试此抽象路径名表示的文件或目录是否存在
public boolean exists()测试此抽象路径名表示的文件或目录是否存在
public long length()返回由此抽象路径名表示的文件的长度
public long lastModified()用于获取文件的最后一次修改时间
public String getAbsolutePath()用于获取绝对路径信息
public boolean delete()用于删除文件,当删除目录时要求是空目录
public boolean createNewFile()用于创建新的空文件
public boolean mkdir()用于创建目录
public boolean mkdirs()用于创建多级目录
public File[] listFiles()获取该目录下的所有内容
public boolean isFile()判断是否为文件
public boolean isDirectory()判断是否为目录
public File[] listFiles(FileFilter filter)获取目录下满足筛选器的所有内容

2、IO

IO就是Input和Output的简写,也就是输入和输出的含义

2.1、IO分类

  • 按读写数据的基本单位
    • 字节流:以字节为单位进行数据读写的流,可以读写任意类型的文件
    • 字符流:以字符(2个字节)为单位进行数据读写的流,只能读写文本文件
  • 按读写数据的方向不同
    • 输入流:从文件中读取数据内容输入到程序中,也就是读文件
    • 输出流:将程序中的数据内容输出到文件中,也就是写文件
  • 按流的角色
    • 节点流:直接和输入输出源对接的流
    • 处理流:需要建立在节点流的基础之上的流

2.2、IO流的体系结构

红色为抽象类

蓝色为节点流,必须直接与指定的物理节点关联

分类字节输入流字节输出流字符输入流字符输出流
抽象基类InputStreamOutputStreamReaderWriter
访问文件FileInputStreamFileOutputStreamFileReaderFileWriter
访问数组ByteArrayInputStreamByteArrayOutputStreamCharArrayReaderCharArrayWriter
访问管道PipedInputStreamPipedOutputStreamPipedReaderPipedWriter
访问字符串————StringReaderStringWriter
缓冲流BufferedInputStreamBufferedOutputStreamBufferedReaderBufferedWriter
转换流————InputStreamReaderOutputStreamWriter
对象流ObjectInputStreamObjectOutputStream————
打印流——PrintStream——PrintWriter
FilterInputStreamFilterOutputStreamFilterReaderFilterWriter
推回输入流PushbackInputStream——PushbackReader——
特殊流DataInputStreamDataOutputStream————

2.3、FileInputStream

逐个字节读

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileInputStream fileInputStream = new FileInputStream(file);int l = 0;while ((l=fileInputStream.read())!=-1){System.out.println((char)l);}fileInputStream.close();}
}

建立缓冲区读

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileInputStream fileInputStream = new FileInputStream(file);byte[] b=new byte[1024];int l = 0;while ((l=fileInputStream.read(b))!=-1){System.out.println(new String(b,0,l));}fileInputStream.close();}
}

2.4、FileOutputStream

写入文件

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileOutputStream fileOutputStream = new FileOutputStream(file);String str="hello world, i am ruoye!";byte[] bytes = str.getBytes();fileOutputStream.write(bytes);fileOutputStream.close();}
}

追加内容

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileOutputStream fileOutputStream = new FileOutputStream(file,true);String str="hello world, i am ruoye!";byte[] bytes = str.getBytes();fileOutputStream.write(bytes);fileOutputStream.close();}
}

2.5、FileReader

逐个字符读

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileReader fileReader = new FileReader(file);int l = 0;while ((l=fileReader.read())!=-1){System.out.println((char)l);}fileReader.close();}
}

建立缓冲区读

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileReader fileReader = new FileReader(file);char[] b=new char[1024];int l = 0;while ((l=fileReader.read(b))!=-1){System.out.println(new String(b,0,l));}fileReader.close();}
}

2.6、FileWriter

写入文件

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileWriter fileWriter = new FileWriter(file);String str="你好,世界!";fileWriter.write(str);fileWriter.close();}
}

追加内容

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileWriter fileWriter = new FileWriter(file,true);String str="你好,世界!";fileWriter.append(str);fileWriter.close();}
}

2.7、转换流

public class Test {public static void main(String[] args) throws IOException {InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File("D:\\a.txt")));int l = 0;while ((l=inputStreamReader.read())!=-1){System.out.println((char)l);}inputStreamReader.close();}
}
public class Test {public static void main(String[] args) throws IOException {OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(new File("D:\\a.txt")));String str="hello wordld";outputStreamWriter.write(str);outputStreamWriter.close();}
}

2.8、缓冲流

public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileInputStream fileInputStream = new FileInputStream(file);BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);byte[] bytes=new byte[1024];int l = 0;while ((l=fileInputStream.read(bytes))!=-1){System.out.println(new String(bytes,0,l));}fileInputStream.close();}
}
public class Test {public static void main(String[] args) throws IOException {String path="D:\\a.txt";File file = new File(path);FileOutputStream fileOutputStream = new FileOutputStream(file);BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);String str="hello world, i am ruoye!";byte[] bytes = str.getBytes();bufferedOutputStream.write(bytes);bufferedOutputStream.close();fileOutputStream.close();}
}

writer和reader同上

2.9、Data流

2.9.1、DataInputStream

  • read(byte b[])—从数据输入流读取数据存储到字节数组b中
  • read(byte b[],int off,in len)—从数据输入流中读取数据存储到数组b里面,位置从off开始,长度为len个字节
  • readFully(byte b[])—从数据输入流中循环读取b.length个字节到数组b中
  • readFully(byte b[],int off,in len )—从数据输入流中循环读取len个字节到字节数组b中.从b的off位置开始
  • skipBytes(int b)—跳过n个字节
  • readBoolean()—从数据输入流读取布尔类型的值
  • readByte()—从数据输入流中读取一个字节
  • readUnsignedByte()—从数据输入流中读取一个无符号的字节,返回值转换成int类型
  • readShort()—从数据输入流读取一个short类型数据
  • readUnsignedShort()—从数据输入流读取一个无符号的short类型数据
  • readChar()—从数据输入流中读取一个字符数据
  • readInt()—从数据输入流中读取一个int类型数据
  • readLong()—从数据输入流中读取一个long类型的数据
  • readFloat()—从数据输入流中读取一个float类型的数据
  • readDouble()—从数据输入流中读取一个double类型的数据
  • readUTF()—从数据输入流中读取用UTF-8格式编码的UniCode字符格式的字符串
public class Test {public static void main(String[] args) throws IOException {DataInputStream dataInputStream = new DataInputStream(new FileInputStream("D:\\a.txt"));System.out.println(dataInputStream.readUTF());System.out.println(dataInputStream.readInt());System.out.println(dataInputStream.readBoolean());System.out.println(dataInputStream.readShort());System.out.println(dataInputStream.readLong());System.out.println(dataInputStream.readDouble());dataInputStream.close();}
}

2.9.2、DataOutputStream

  • intCount(int value)—数据输出流增加的字节数
  • write(int b)—将int类型的b写到数据输出流中
  • write(byte b[],int off, int len)—将字节数组b中off位置开始,len个长度写到数据输出流中
  • flush()—刷新数据输出流
  • writeBoolean()—将布尔类型的数据写到数据输出流中,底层是转化成一个字节写到基础输出流中
  • writeByte(int v)—将一个字节写到数据输出流中(实际是基础输出流)
  • writeShort(int v)—将一个short类型的数据写到数据输出流中,底层将v转换2个字节写到基础输出流中
  • writeChar(int v)—将一个charl类型的数据写到数据输出流中,底层是将v转换成2个字节写到基础输出流中
  • writeInt(int v)—将一个int类型的数据写到数据输出流中,底层将4个字节写到基础输出流中
  • writeLong(long v)—将一个long类型的数据写到数据输出流中,底层将8个字节写到基础输出流中
  • writeFloat(flloat v)—将一个float类型的数据写到数据输出流中,底层会将float转换成int类型,写到基础输出流中
  • writeDouble(double v)—将一个double类型的数据写到数据输出流中,底层会将double转换成long类型,写到基础输出流中
  • writeBytes(String s)—将字符串按照字节顺序写到基础输出流中
  • writeChars(String s)—将字符串按照字符顺序写到基础输出流中
  • writeUTF(String str)—以机器无关的方式使用utf-8编码方式将字符串写到基础输出流中
  • size()—写到数据输出流中的字节数
public class Test {public static void main(String[] args) throws IOException {DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream("D:\\a.txt"));dataOutputStream.writeUTF("α");dataOutputStream.writeInt(1234567);dataOutputStream.writeBoolean(true);dataOutputStream.writeShort((short)123);dataOutputStream.writeLong((long)456);dataOutputStream.writeDouble(99.98);dataOutputStream.close();}
}

2.10、Zip流

压缩文件

public class Test {public static void main(String[] args) throws IOException {File file = new File("D:\\a.txt");FileInputStream fileInputStream = new FileInputStream(file);File zip = new File("D:\\a.zip");FileOutputStream fileOutputStream = new FileOutputStream(zip);ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);int l = 0;zipOutputStream.putNextEntry(new ZipEntry(file.getName()));while ((l=fileInputStream.read())!=-1){//压缩算法zipOutputStream.write(l);}fileInputStream.close();zipOutputStream.close();fileOutputStream.close();}
}

解压文件

public class Test {public static void main(String[] args) throws IOException {File file = new File("D:\\a.txt");File zip = new File("D:\\a.zip");ZipFile zipFile= new ZipFile(zip);ZipEntry entry = zipFile.getEntry("a.txt");InputStream input = zipFile.getInputStream(entry);FileOutputStream fileOutputStream = new FileOutputStream(file);int l;while ((l=input.read())!=-1){//解压算法fileOutputStream.write(l);}fileOutputStream.close();input.close();}
}

压缩多个文件

待补

解压多个文件

待补

2.11、Object流

想要使用ObjectOutputStream和ObjectInputStream,对象一定要序列化

import java.io.Serializable;public class Person implements Serializable {private String name;private int age;public Person() {}public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}
}

当我们使用Serializable接口实现序列化操作的时候,如果一个对象的某一个属性不想被序列化保存下来,那么我们可以使用transient关键字进行说明

private transient String name;

2.11.1、ObjectOutputStream

public class Test {public static void main(String[] args) throws IOException {File file = new File("D:\\a.txt");ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));objectOutputStream.writeObject(new Person("zhangsan",20));objectOutputStream.close();}
}

2.11.2、ObjectInputStream

public class Test {public static void main(String[] args) throws IOException, ClassNotFoundException {File file = new File("D:\\a.txt");ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));Person person = (Person) objectInputStream.readObject();System.out.println(person);}
}

2.12、RandomAccessFile

方法声明功能介绍
public RandomAccessFile(String name, String mode)根据参数指定的名称和模式构造对象 r: 以只读方式打开 rw:打开以便读取和写入 rwd:打开以便读取和写入,同步文件内容的更新 rws:打开以便读取和写入,同步文件内容和元数据 的更新
int read()读取单个字节的数据
void seek(long pos)用于设置从此文件的开头开始测量的文件指针偏移量
void write(int b)将参数指定的单个字节写入
void close()用于关闭流并释放有关的资源
public class Test {public static void main(String[] args) throws IOException {RandomAccessFile r = new RandomAccessFile("D:\\a.txt", "rw");r.seek(1);System.out.println((char) r.read());r.write('b');r.close();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/557512.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux搭建web服务器原理,【LINUX】linux搭建web服务器

linux httpd假设服务器地址为192.168.80.20/241.将准备安装的httpd软件包共享给everyone,(1)在linux上mount.cifs //真机IP地址/共享文件夹名/media /ls /meidia/查看tar xjvf httpd-2.4.10.tar.bz2 -C /usr/src解压至/usr/src下下面两个插件是httpd2.4以…

Java核心类库篇7——多线程

Java核心类库篇7——多线程 1、程序、进程和线程 程序 - 数据结构 算法,主要指存放在硬盘上的可执行文件进程 - 主要指运行在内存中的可执行文件线程就是进程内部的程序流 操作系统内部支持多 进程的,而每个进程的内部又是支持多线程的 2、线程的创…

Java核心类库篇8——网络编程

Java核心类库篇8——网络编程 1、七层网络模型 OSI(Open System Interconnect),即开放式系统互联,是ISO(国际标准化组织)组织在1985 年研究的网络互连模型。 当发送数据时,需要对发送的内容按…

linux网站如何添加swf支持,linux下安装swftools和openOffice

1.openOffice下载地址:http://download.openoffice.org/all_rc.html#untested-full 下载 Linux 32-bit Intel rpm 包 版本自选 2.安装openOffice 1】 tar -zxvf OOo_3.2.1_Linux_x86_install-rpm-wJRE_zh-CN.tar.gz 2】 cd OOO320_m18_native_packed-1_zh-CN1.openO…

Java番外篇1——正则表达式

Java番外篇1——正则表达式 1、什么是正则表达式 正则表达式定义了字符串的模式正则表达式可以用来搜索、编辑或处理文本正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别 2、正则表达式规则 2.1、普通字符 普通字符包括没有显式指定为元字符的所…

linux 1号硬盘不能用,linux 挂载硬盘的疑问 : IDE1 上的接口无法使用.

状况说明:我将在linux系统上挂载多块硬盘(目前是redhat9).我通过3块硬盘试验.问题出现:无论如何链接 IDE1 上的硬盘, /dev/hdc 都无法 mount.数据:1. 使用 fdisk -l : 不会显示接到 IDE1 上的硬盘(目前只试验了在 IDE1 上接 1个硬盘,用 master 端口).2. 使用 fdisk /dev/hdc : …

Java番外篇2——jdk8新特性

Java番外篇2——jdk8新特性 1、Lambda 1.1、无参无返回值 public class Test {interface Print{void print();}public static void main(String[] args) { // Print printnew Print() { // Override // public void print() { // …

linux同花顺乱码,打开同花顺软件全是问号

官方答案:字体库字体乱码【原因分析】:系统字体缺失,损坏。【解决方案】方案一:使用360电脑门诊进行修复1.打开【360安全卫士】—【电脑专家】搜索乱码,然后会弹出如下六个解决方案,根据当前计算机的故障现…

Java番外篇3——线程池

Java番外篇3——线程池 1、多线程产生的问题 多次创建并销毁线程。而创建并销毁线程的过程势必会消耗内存 2、线程池 降低系统资源消耗,通过重用已存在的线程,降低线程创建和销毁造成的消耗提高系统响应速度,当有任务到达时,通…

嵌入式linux组件,嵌入式Linux系统的几大组件!

原标题:嵌入式Linux系统的几大组件!本文概述了Linux系统的几大组件,描述了这些组件之间的关系。文章解释了术语,并描述看似很基础的细节。每个Linux系统都有许多主要组件。其中一个组件(引导加载程序)从技术上讲是Linux之外的&…

linux iptables找不到,centos /etc/sysconfig/下找不到iptables文件解决方法

本想做些防火墙策略。防火墙策略都是写在/etc/sysconfig/iptables文件里面的。可我发现我也没有这个文件。[rootxiaohuai /]# cd /etc/sysconfig/[rootxiaohuai sysconfig]# lsatd firstboot irqbalance network-scripts rhn sysstatauditd grub kdump ntpd rngd sysstat.iocon…

Java番外篇4——BigInteger与BigDecimal

Java番外篇4——BigInteger与BigDecimal 为了解决大数运算的问题 操作整型:BigInteger操作小数:BigDecimal 1、BigInteger 方法声明功能介绍public BigInteger abs()返回大整数的绝对值public BigInteger add(BigInteger val)返回两个大整数的和publ…

linux cd 命令案例,15个关于Linux的‘cd’命令的练习例子

命令名称:cd代表:切换目录使用平台:所有Linux发行版本执行方式:命令行权限:访问自己的目录或者其余指定目录级别:基础/初学者1.从当前目录切换到/usr/local avitecmint:~$ cd /usr/local avitecmint:/usr/l…

c语言字符串strchr,Strchr()C语言字符串处理功能

strchr()函数不如strcpy(),strcat(),strcmp(),strupr(),strlwr(),strlen()直观c strchr函数,因此需要代码理解:代码来自C语言开发入门和项目实战书:#include#includeint main(){字符字符串[50];char * str&…

Java数据库篇1——数据库配置

Java数据库篇1——数据库配置 1、数据库 数据库(DataBase) 就是存储和管理数据的仓库本质是一个文件系统, 还是以文件的方式,将数据保存在电脑上 2、数据库的优点 存储方式优点缺点内存速度快不能够永久保存,数据是临时状态的文件数据是可以永久保存的使用IO流操作文件, 不…

C语言中输入123求位权,数反转 - it610.com

32位系统c语言中:char取值范围:-128~127unsigned char取值范围:0~255int取值范围:-2147483648~2147483647unsigned int取值范围:0~429496729564位系统下C语言中int还是占4字节,32位,与32位系统中没有区别64位系统下,采用64位编译器…

Java数据库篇2——数据库基本操作

Java数据库篇2——数据库基本操作 1、启动、停止、服务 net start mysqlnet stop mysql2、登入登出 本地 Mysql -u用户名 -p密码Mysql -u用户名 -p回车 密码远程 Mysql -hIP地址 -u用户名 -p密码Mysql -hIP地址 -u用户名 -p回车 密码退出 Quit Exit

c语言加密shell脚本,shell脚本加密

如何保护自己编写的shell程序要保护自己编写的shell脚本程序,方法有很多,最简单的方法有两种:1、加密 2、设定过期时间,下面以shc工具为例说明:一、下载安装shc工具shc是一个加密shell脚本的工具.它的作用是把shell脚本…

Java数据库篇3——SQL

Java数据库篇3——SQL 结构化查询语言(Structured Query Language)简称SQL,是一种特殊目的的编程语言,是一种数据库 查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统 1、SQL分类 分类说明数据定义语言简称DDL(Data De…

c语言分配飞机10个座位,leetcode1227(飞机座位分配)--C语言实现

对于第一个乘客来说 他有三种选择坐在正确的(自己的位置), 那么后面的乘客都不会乱,所以第n个乘客可以坐到自己的位置, 1/n * 1.坐在第n个乘客的位置,那么第n个乘客肯定无法坐到自己的位置, 1/n * 0.坐在[1,n-1]之间的某个位置K.对于第K个乘客而言&#…