迁安做网站教育培训机构设计图
news/
2025/10/3 17:34:27/
文章来源:
迁安做网站,教育培训机构设计图,下载教学设计的网站,上海创意型网站建设Java核心类库篇6——IO
1、File
1.1、构造方法
方法声明功能介绍public File(File parent, String child)从父抽象路径名和子路径名字符串创建新的 File实例public File(String pathname)通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例public File(String pa…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——PrintWriterFilterInputStreamFilterOutputStreamFilterReaderFilterWriter推回输入流PushbackInputStream——PushbackReader——特殊流DataInputStreamDataOutputStream————
2.3、FileInputStream
逐个字节读
public class Test {public static void main(String[] args) throws IOException {String pathD:\\a.txt;File file new File(path);FileInputStream fileInputStream new FileInputStream(file);int l 0;while ((lfileInputStream.read())!-1){System.out.println((char)l);}fileInputStream.close();}
}建立缓冲区读
public class Test {public static void main(String[] args) throws IOException {String pathD:\\a.txt;File file new File(path);FileInputStream fileInputStream new FileInputStream(file);byte[] bnew byte[1024];int l 0;while ((lfileInputStream.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 pathD:\\a.txt;File file new File(path);FileOutputStream fileOutputStream new FileOutputStream(file);String strhello 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 pathD:\\a.txt;File file new File(path);FileOutputStream fileOutputStream new FileOutputStream(file,true);String strhello 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 pathD:\\a.txt;File file new File(path);FileReader fileReader new FileReader(file);int l 0;while ((lfileReader.read())!-1){System.out.println((char)l);}fileReader.close();}
}建立缓冲区读
public class Test {public static void main(String[] args) throws IOException {String pathD:\\a.txt;File file new File(path);FileReader fileReader new FileReader(file);char[] bnew char[1024];int l 0;while ((lfileReader.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 pathD:\\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 pathD:\\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 ((linputStreamReader.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 strhello wordld;outputStreamWriter.write(str);outputStreamWriter.close();}
}
2.8、缓冲流
public class Test {public static void main(String[] args) throws IOException {String pathD:\\a.txt;File file new File(path);FileInputStream fileInputStream new FileInputStream(file);BufferedInputStream bufferedInputStream new BufferedInputStream(fileInputStream);byte[] bytesnew byte[1024];int l 0;while ((lfileInputStream.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 pathD:\\a.txt;File file new File(path);FileOutputStream fileOutputStream new FileOutputStream(file);BufferedOutputStream bufferedOutputStream new BufferedOutputStream(fileOutputStream);String strhello 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 ((lfileInputStream.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 ((linput.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/926133.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!