简介
👨💻个人主页:@云边牧风
👨🎓小编介绍:欢迎来到云边牧风破烂的小星球🌝
📋专栏:Java基础知识
🔑本章内容:输入输出流
记得 评论📝 +点赞👍 +收藏😽 +关注💞哦~
这一节的主要内容有:
随机存取文件
RandomAccessFile类
标准输入输出的重定向
序列化与对象克隆
一、随机存取文件
概念:
很多情况下,要求程序能够迅速、直接地访问文件中的特定信息,这时就需要用到“随机存取文件”——可以理解为插入式地进行读写
①随机存取文件必须要指定格式规则
②RandomAccessFile类用于随机存取文件
二、RandomAccessFile类
RandomAccessFile类的特点
有个位置指示器,指向当前读写处的位置。刚打开文件时,文件指示器指向文件的开头处。对文件指针显式操作的方法有:
public int skipBytes (int n)// 把文件指针 向前移动指定的 n 个字节
public void seek (long)// 移动文件指针到指定的位置
public native long getFilePointer ()// 得到当前的文件指针
RandomAccessFile类的构造方法
RandomAccessFile(File file,String mode) throws FileNotFoundException
RandomAccessFile(String name, String mode) throws FileNotFoundException
RandomAccessFile类的常用方法
length () :返回 文件的长度 ,即字节数read () :从文件中读取一 字节, 如 遇到结尾,则返回 -1readDouble () :读取八个字节 ( double 数据 )writeChar (int v) :写入一个 字符 ,两个字节,高位先写入writeInt (int v) :写入四个字节的 int 型数字
如:将10个整数写入tom.dat文件中,然后反序读出
import java.io.*;
public class Main {
public static void main(String args[]) {
RandomAccessFile inAndOut=null;
int data[]={1,2,3,4,5,6,7,8,9,10};
try{ inAndOut=new RandomAccessFile("tom.dat","rw");
for(int i=0;i<data.length;i++) {
inAndOut.writeInt(data[i]);
}
for(long i=data.length-1;i>=0;i--) {
inAndOut.seek(i*4);
System.out.printf("\t%d",inAndOut.readInt());
}
inAndOut.close();
}
catch(IOException e){}
}
}
对于 inAndOut.seek(i*4); 有👇:
一个int型数据占4个字节,inAndOut从文件的第37个字节读取最后面的一个整数,每隔4个字节往前读取一个整数
三、标准输入输出的重定向
标准输入输出的重定向
——可以通过“重定向”改变in、out、err默认对应的输入/输出设备:
setIn ( InputStream ) : 设置标准 输入 流
setOut ( PrintStream ) :设置标准 输出 流
setErr ( PrintStream ) :设置标准 错误输出 流
例如:
import java.io.*;
public class Main {
public static void main(String args[]) {
BufferedInputStream inn = new BufferedInputStream(new FileInputStream(
"d:\\1234567890\\input.java" ));
PrintStream outt = new PrintStream(new BufferedOutputStream(new
FileOutputStream("d:\\1234567890\\test.out") ) );
System.setIn(inn);
System.setOut(outt);
System.setErr(outt);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
System.out.println(s);
outt.close();
}
}
//首先从input.java中读取一行;然后输出到test.out文件中。
四、序列化与对象克隆
CloneClass a=new CloneClass();
a.aInt=10;
CloneClass b=(CloneClass)a.clone();
System.out.println(b.aInt);
五、练习
练习1
在D:\1234567890\grade.txt中保存了一个学生的多门课程,需要从该文件读取学生的成绩,并将课程成绩按照从高到低的顺序排列,将排序后的成绩保存到D:\1234567890\sortgrade.txt中。
可以参考如下代码:
练习2
在D:\1234567890\newFile文件夹下存放有两类文件:.txt文本文件和.jpg图片文件。现在需要将D:\1234567890\newFile文件夹中的.txt文件中的内容读出并显示到屏幕,将D:\1234567890\newFile文件夹中的.jpg图片文件复制到D:\newFile文件夹中。然后删除D:\1234567890\newFile文件夹中的所有文件。
public void MoveFiles() throws SQLException, IOException{
File file=new File(“D:\\1234567890\\newFile");
File[] filenames=file.listFiles();
for(int i=0;i<filenames.length;i++){
if(filenames[i].getName().endsWith(“.txt”)){ //输出txt文件内容
BufferedReader read=new BufferedReader(
new FileReader(file+"\\"+filenames[i].getName()));
String newline=read.readLine();
while(newline!=null){
System.out.println(newline);
newline=read.readLine();
}
read.close();
}
else{ //移动jpg文件
BufferedInputStream input=new BufferedInputStream(
new FileInputStream("D:\\1234567890\\newFile\\"+filenames[i].getName()));
BufferedOutputStream output=new BufferedInputStream(
new FileOutputStream("D:\\newFile\\"+filenames[i].getName()));
int in=input.read();
while(in!=-1){
output.write(in);
in=input.read();
}
output.flush();
output.close();
input.close();
}
}
for(int j=0;j<filenames.length;j++){ //删除文件夹中所有文件
filenames[j].delete();
}
}
六、本章总结:
I/O流类
读写文本文件
读写二进制文件
随机文件读写
结束语:
以上是Jav第六章的全部内容 希望大家喜欢
下一节讲第7章——线程与JDBC
喜欢的可以点赞+关注哈 ❤