RandomAccessFile :随机读取类,可以随机的读取一个文件中指定位置的数据
rw 读写 r 只读 w 只写
package File_test;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class Test_RandomAccessFile {
public static void main(String[] args) throws IOException {
File f = new File("d:" + File.separator + "text.txt");
RandomAccessFile rdf = null;
rdf = new RandomAccessFile(f, "rw");
// String name = "zhangsan";
// int age = 20;
// rdf.writeBytes(name);
// rdf.writeInt(age);
//
// name = "lisi ";
// age = 21;
// rdf.writeBytes(name);
// rdf.writeInt(age);
//
// name = "wangwu ";
// age = 18;
// rdf.writeBytes(name);
// rdf.writeInt(age);
//
// rdf.close();
//读文件
String name = null;
int age = 0;
byte []b = new byte[8];//读取姓名,姓名空间最长为8
rdf.skipBytes(24);//跳过12个长度
for (int i = 0;i<b.length;i++){
b[i] = rdf.readByte();//循环读取8个内容
}
name = new String(b);
age = rdf.readInt();
System.out.println(name+" "+age);
rdf.seek(0);//指针返回到文件开头
for (int i = 0;i<b.length;i++){
b[i] = rdf.readByte();//循环读取8个内容
}
name = new String(b);
age = rdf.readInt();
System.out.println(name+" "+age);
}
}
随机读取时对文件内容的操作,不太常用,一般情况用字节流和字符流
流: 字节(输入、输出),字符(输入、输出)
流是一种有序的,有起点有终点的字节的集合。是对计算机中数据传输的总称或抽象。
流的分类:
传输方向: 输入/输出流
按照基本功能的不同:节点流/过滤流
数据类型分类: 字节/字符
处理文本时用字符流,处理其他时用字节流
字节流和字符流的区别
字节流没有用到缓冲区,字符流用到缓冲区
操作完成后关闭流,清空缓冲区,防止文件写入失败。
应尽可能使用缓冲区,来减少io次数,提高性能
能用字符流处理的不用字节流
字节输入流:inputStream
字节输出流:OutputStream
字符输入流:reader
字符输出流:writer
输入输出流是以 “ 应用程序 ” 为参照物
输入流:外部读入内存
流操作的步骤:
- 使用File类打开一个文件
- 通过字节流/字符流的子类指定输出的位置
- 进行读写操作
- 关闭输入/输出流
复制图片代码:
package File_test;
import java.io.*;
public class Copy_pict {
//图片的复制,(输入输出字节流)
public static void main(String[] args) throws IOException {
File file = new File("d:"+File.separator+"12.jpg");
DataInputStream dis=null;
DataOutputStream dos = null;
FileInputStream fis = null;
FileOutputStream fos = null;
//创建输入流对象
fis =new FileInputStream(file);
dis = new DataInputStream(fis);
//创建输出流对象
fos = new FileOutputStream("e://test.jpg");
dos = new DataOutputStream(fos);
//读写文件
int temp;
while((temp=dis.read())!=-1){
dos.write(temp);
}
if (fis!=null)
fis.close();
if(fos!=null)
fos.close();
if(dos!=null)
dos.close();
if(dis!=null)
dis.close();
}
}
//复制文件代码:
package File_test;
import javax.annotation.processing.Filer;
import java.io.*;
public class Copy_file {
public static void copyFile(String oldPath,String newPath) throws IOException {
File f = new File(oldPath);
if(f.exists()){
File d1 = new File(newPath);
FileReader fr = new FileReader(f);
FileWriter fw = new FileWriter(d1);
char c [] = new char[1024];
int n;
while ((n = fr.read(c))!=-1 ){
fw.write(c,0,n);
}
fw.close();
fr.close();
}
}
public static void main(String[] args) throws IOException {
copyFile("d://stu.txt","e://a.txt");
}
}
//按字节复制文件
package File_test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy_File_Byte {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("d://stu.txt");
FileOutputStream fos =new FileOutputStream("e://789.txt");
byte [] b = new byte[1024];
int len = -1;
while ((len = fis.read(b))!=-1){
fos.write(b,0,len);
}
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
内存操作流
//使用内存操作流进行大写转小写
package File_test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ByteArray_stream {
public static void main(String[] args) throws IOException {
String str = "ABCDRFGG";
ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int temp = 0;
while ((temp = bis.read())!=-1){
char ch = (char)temp;
bos.write(Character.toLowerCase(ch));
}
String ss = bos.toString();
System.out.println(ss);
bos.close();
bis.close();
}
}
//管道流:主要作用是进行;两个线程之间的通信,主要分为pipedinputstream pipedoutputstream
package File_test;
import java.io.IOException;
import java.io.PipedOutputStream;
public class Send implements Runnable{
PipedOutputStream pos = new PipedOutputStream();
@Override
public void run() {
String str = "发送程序--654321321-";
try {
pos.write(str.getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
if(pos!=null) {
try {
pos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
-------------------------------
package File_test;
import java.io.IOException;
import java.io.PipedInputStream;
public class Receive implements Runnable{
PipedInputStream pis = new PipedInputStream();
@Override
public void run() {
byte [] b = new byte[1024];
int len = 0;
try {
len = pis.read(b);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("datas: "+new String(b,0,len));
}
}
package File_test;
import java.io.IOException;
public class TestPiped {
public static void main(String[] args) throws IOException {
Send send = new Send();
Receive receive = new Receive();
//沟通
send.pos.connect(receive.pis);
new Thread(send).start();
new Thread(receive).start();
}
}
//打印流:PrintStream
可格式化输入到文件之中
package File_test;
import java.io.*;
public class PrintTest {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("d:"+File.separator+"123.txt");
OutputStream os = new FileOutputStream(file);
PrintStream ps = new PrintStream(os);
ps.print("32154456798aslkdja ");
ps.close();
}
}
System 对流的支持:
System.setOut()重定向,输出到对应的流当中(文件流,等)
package File_test;
import java.io.*;
public class System_test {
public static void main(String[] args) throws IOException {
// //system对流的支持 out in err
// OutputStream out = System.out;
// String str = "321654789";
//
// out.write(str.getBytes());
// out.close();
//
// String str = "fasd";
// try {
// System.out.println(Integer.parseInt(str));
// }catch (Exception e){
// System.err.println(e);
// }
/*
System.out 把内容呈现给用户
System.err 把输出的内容不给用户看,给程序员后台提示
*/
InputStream input = System.in;
byte [] b = new byte[1024];
int len = input.read(b);
System.out.println(new String(b,0,len));
System.setOut(new PrintStream(new FileOutputStream("d:"+File.separator+"123.txt")));
System.out.println("321321321321321321321");
input.close();
}
}