Java IO流
RandomAccessFile类:
常用方法:
-
int readInt():读取一个整数并返回该整数
-
byte readByte():读取一个字节并返回该字节
-
int read(byte[]):将读取的内容存进byte数组中,返回读取的个数
-
void writeInt(int i):写入一个整数,
-
void writeBytes(String s):写入一个字符串
-
void seek(long pos):设置读指针的位置,默认为0
-
int skipBytes(int n):指针跳过指定的字节
具体使用如下:
//写入数据
public class RandomAccessFileDemo1 {
public static void main(String[] args) throws Exception {
File f = new File("d:"+File.separator+"ajava"+File.separator+"test.txt");
RandomAccessFile rdf = new RandomAccessFile(f, "rw");
String name = null;
int age = 0;
name = "zhangsan";
age = 30;
rdf.writeBytes(name); //将一个字符串按字节方式写入文件
rdf.writeInt(age); //将一个int型数据写入文件
name="lisi ";
age=31;
rdf.writeBytes(name);
rdf.writeInt(age);
name="wangwu ";
age=32;
rdf.writeBytes(name);
rdf.writeInt(age);
rdf.close();
}
}
//读取数据
public class RandomacessFileDemo2 {
public static void main(String[] args) throws Exception {
File f= new File("d:"+File.separator+"ajava"+File.separator+"test.txt");
RandomAccessFile rdf = new RandomAccessFile(f, "r");
byte[] b=new byte[8];
for (int i = 0; i < b.length; i++) {
b[i]=rdf.readByte();
}
String name = null;
int age = 0;
name = new String(b); //字节数组转字符串
age = rdf.readInt();
System.out.println("第一个人的信息--> 姓名:"+name+";年龄:"+age);
for (int i = 0; i < b.length; i++) {
b[i] = rdf.readByte();
}
name = new String(b);
age = rdf.readInt();
System.out.println("第二个人的信息--> 姓名:"+name+";年龄:"+age);
for (int i = 0; i < b.length; i++) {
b[i] = rdf.readByte();
}
name = new String(b);
age = rdf.readInt();
System.out.println("第三个人的信息--> 姓名:"+name+";年龄:"+age);
rdf.close();
}
}
字节流:
-
字节输入流(InputStream):read(byte[]]), read(int b)(读取一个字节,参数常常为byte类型)
//字节输入流 public class InputStream { public static void main(String[] args) throws Exception { File f = new File("d:"+File.separator+"ajava"+File.separator+"test1.txt"); FileInputStream is = new FileInputStream(f); // byte[] b= new byte[(int)f.length()]; // is.read(b); //一次全部读取到byte数组中 // System.out.println(new String(b)); byte[] b2 = new byte[(int)f.length()]; for (int i = 0; i < b2.length; i++) { b2[i]=(byte)is.read(); } System.out.println(new String(b2)); is.close(); } }
-
字节输出流(OutputStream)
public class OutPutreamDemo { public static void main(String[] args) throws Exception { File f =new File("d:"+File.separator+"ajava"+File.separator+"test1.txt"); FileOutputStream os = new FileOutputStream(f, true);//追加方式写入 String str = "Hello World!!!123"; byte[] b = str.getBytes(); //将字符串转换为字符数组 os.write(b); //write(byte[]); os.close(); } }
字符流:
-
字符输入流(Reader)
/字符流读取 public class FileReaderDemo { public static void main(String[] args) throws Exception { File f = new File("d:"+File.separator+"ajava"+File.separator+"test2.txt"); Reader reader = null; reader = new FileReader(f); int len = 0; char[] c = new char[1024]; int temp = 0; while ((temp = reader.read())!=-1){ c[len] = (char) temp; len++; } reader.close(); System.out.println("读取到的内容为:"+new String(c,0,len)); } }
-
字符输出流(Writer)
//字符流写入 public class FileWriterDemo { public static void main(String[] args) throws Exception { File f = new File("d:"+File.separator+"ajava"+File.separator+"test2.txt"); Writer out = null; out = new FileWriter(f,true); String str = "Hello World!!! 123 "; out.write(str); out.close(); //这一步不能遗漏,否则不会生效s } }
字节流如字符流区别:
字节流能操作任意数据类型,如二进制文件(图片,视频等),字符流只能操作纯文本文件,字节流是直接对文件进行操作的,不会用到缓冲区,而字符流是会用到缓冲区的,故字符流在操作完后一定要关闭该流才能使操作生效。